题目大意:有一位员工想要利用喝咖啡来休息,他给了一个数组表示他想要喝咖啡的时间点(假设他喝咖啡用时1分钟),老板规定每次喝咖啡的时间间隔必须要大于d。问:他将给定数组的时间点都经
历一遍最少(贪心所在)需要多长时间,并输出每个时间点是在第几天经历的;
解题思路:贪心地选取喝咖啡的时间,我们尽量选取喝咖啡时间靠前的,然后贪心寻找能放在他后面最靠前的时间点,如果放不开就新开一天。
用map和队列的做法:
1 /* */ 2 # include <iostream> 3 # include <stdio.h> 4 # include <string.h> 5 # include <string> 6 # include <iomanip> 7 # include <algorithm> 8 # include <ctime> 9 # include <cmath> 10 # include <climits> 11 # include <cstdlib> 12 # include <utility> 13 # include <bitset> 14 # include <cctype> 15 # include <cassert> 16 # include <set> 17 # include <map> 18 # include <deque> 19 # include <queue> 20 # include <stack> 21 # include <vector> 22 # include <functional> 23 using namespace std; 24 25 typedef long long ll; 26 const int maxn=2e5+50; 27 const ll mod=1e9+7; 28 const int eps=1e-8; 29 const double pi=acos(-1.0); 30 # define mem(a,x) memset((a),(x),sizeof((a))) 31 # define gcd(a,b) (__gcd(a, b)) 32 # define lcm(a,b) (a*b/__gcd(a, b)) 33 # define lson l,m,rt<<1 34 # define rson m+1,r,rt<<1|1 35 # define lowbit(x)(x&(-x)) 36 37 int origin[maxn], after[maxn]; 38 map<int, int>m;///存某时间喝咖啡是在哪一天 39 //priority_queue<int, vector<int>, greater<int> > q; 40 queue<int>q; 41 int main() 42 { 43 int n, mm, d, cnt=1; 44 cin>>n>>mm>>d; 45 46 for(int i=1; i<=n; i++ ) 47 { 48 scanf("%d", &origin[i]); 49 after[i] = origin[i]; 50 } 51 52 sort(after+1, after+1+n); 53 m[after[1]] = 1;//时间点最靠前的在第一天 54 q.push(1); 55 for(int i=2; i<=n; i++ ) 56 { 57 int top=q.front(); 58 if( after[i]-after[top]>d )//可以在同一天 59 { 60 m[after[i]]=m[after[top]]; 61 q.pop(); 62 } 63 64 else 65 { 66 cnt++; 67 m[after[i]] = cnt; 68 } 69 70 q.push(i); 71 } 72 cout<<cnt<<endl; 73 for(int i=1; i<=n; i++ ) 74 { 75 if( i==1 ) 76 printf("%d", m[origin[i]]); 77 78 else 79 printf(" %d", m[origin[i]]); 80 } 81 cout<<endl; 82 return 0; 83 }
set的做法:
/* */ # include <iostream> # include <stdio.h> # include <string.h> # include <string> # include <iomanip> # include <algorithm> # include <ctime> # include <cmath> # include <climits> # include <cstdlib> # include <utility> # include <bitset> # include <cctype> # include <cassert> # include <set> # include <map> # include <deque> # include <queue> # include <stack> # include <vector> # include <functional> using namespace std; typedef long long ll; const int maxn=2e5+50; const ll mod=1e9+7; const int eps=1e-8; const double pi=acos(-1.0); # define mem(a,x) memset((a),(x),sizeof((a))) # define gcd(a,b) (__gcd(a, b)) # define lcm(a,b) (a*b/__gcd(a, b)) # define lson l,m,rt<<1 # define rson m+1,r,rt<<1|1 # define lowbit(x)(x&(-x)) int a[maxn]; set<int>s; map<int,int>mp;//喝咖啡的时间点在第几天 int n, m, d; int main() { int i; cin>>n>>m>>d; s.clear();//清空set for( i=1; i<=n; i++ ) { cin>>a[i]; s.insert(a[i]);//插入时间点 } set<int>::iterator iter;//迭代器指针iter int cnt=0; int ans=1; while( s.size() )//当set容器中的元素个数不为0 { iter = s.lower_bound(cnt);//在set中查找第一个大于等于cnt的数的所在位置,如果不在返回s.end() if( iter==s.end()) { ans++; cnt=0; } else { mp[*iter] = ans; s.erase(*iter);//删除set中值为*iter的数 cnt = *iter+d+1; } } cout<<ans<<endl; for(int i=1; i<=n; i++ ) { if( i==1 ) printf("%d", mp[a[i]]); else printf(" %d", mp[a[i]]); } cout<<endl; return 0; }