1. 程式人生 > 實用技巧 >Codeforces Round #509 (Div. 2) C. Coffee Break

Codeforces Round #509 (Div. 2) C. Coffee Break

題目連結:https://codeforc.es/contest/1041/problem/C

題意:給定n個不同的數, 代表喝咖啡的時間,每次喝咖啡的間隔必須為d秒 每天只有m秒能喝,問最少多少天喝完

思路:考慮直接模擬,從1開始 用上set的二分搜尋,每次找上一個數+d+1的位置

找到滿足的就刪去 時間複雜度o(nlogn)

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define ll long long
 4 #define pb push_back
 5 const int maxn =2e5+10;
 6 const
int mod=1e9+7; 7 map<int,int>mp; 8 int ans[maxn]; 9 10 11 12 int main() 13 { 14 ios::sync_with_stdio(false); 15 cin.tie(0); 16 int n,m,d; 17 cin>>n>>m>>d; 18 set<int>s; 19 for(int i=1;i<=n;i++) 20 { 21 int x; 22 cin>>x;
23 mp[x]=i; 24 s.insert(x); 25 } 26 d++; 27 int day=1; 28 int now=1; 29 set<int>::iterator it; 30 while(s.size()) 31 { 32 it=s.lower_bound(now); 33 if(it==s.end()) 34 { 35 now=1; 36 day++; 37 } 38
else 39 { 40 ans[mp[*it]]=day; 41 s.erase(*it);// +不+ *都可 42 now=*it+d; 43 } 44 } 45 cout<<day<<'\n'; 46 for(int i=1;i<=n;i++) 47 { 48 cout<<ans[i]<<" "; 49 } 50 51 52 53 54 55 56 57 58 59 60 }
View Code