贪心,前几天刚做过类似的题,以为能变形出来,结果还是差一点成功,哎,还是我太渣
#include <iostream> #include <cstring> #include <cstdio> #include <queue> #include <algorithm> using namespace std; const int maxn=300005; typedef struct note { int x,y; bool operator <(const note &p) { return x<p.x; } }; note a[maxn]; priority_queue< int,vector<int>,greater<int> > q; int n,m; int main() { while(~scanf("%d%d",&n,&m)) { int ans=0; for(int i=0;i<n;i++) { scanf("%d%d",&a[i].x,&a[i].y); a[i].y+=a[i].x; } sort(a,a+n); while(q.size()) q.pop(); for(int i=0;i<n;i++) { while(q.size()&&q.top()+m<a[i].x)//最神奇地方,当时我就是在这里想用for,结果超时,但一个while的队列就解决了让我很难受 q.pop();//下个人来的时候,电脑都锁了 if(q.empty()||q.top()>a[i].x) { ans++; q.push(a[i].y);//下个人来的时候电脑有人在用,再开一台 } else { q.pop(); q.push(a[i].y);//下个人来的时候人走了,但电脑没锁 } } printf("%d\n",n-ans); } return 0; }
原文:http://www.cnblogs.com/Wangwanxiang/p/6675941.html