Description
修理建筑需要T1秒,如果在T2秒之内还没有修理完成,这个建筑就报废了。给出若干条信息,求最多能修理多少建筑.
Sample Input
4
100 200
200 1300
1000 1250
2000 3200
Sample Output
3
贪心
先按结束时间排序,按次序处理。
如果当前的时间加上需要的时间没有超过结束时间,直接进堆,更新答案;(使最终结果变大)
而如果超过了,把堆顶取出预制比较,若堆顶所花时间较多,取出堆顶,将当前建筑进堆。(在最终结果不变的情况下,减少总时间)
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<set>
using namespace std;
struct node
{
int t,ed;
}a[210000];
int cmp(const void *xx,const void *yy)
{
node n1=*(node *)xx;
node n2=*(node *)yy;
return n1.ed-n2.ed;
}
multiset<int> s;
multiset<int>::iterator it;
int main()
{
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)scanf("%d%d",&a[i].t,&a[i].ed);
qsort(a+1,n,sizeof(node),cmp);
int head=1,tail=0,k=0,ans=0;
for(int i=1;i<=n;i++)
{
if(a[i].t+k<=a[i].ed)
{
k+=a[i].t;
ans++;
s.insert(a[i].t);
}
else
{
it=s.end();it--;
int q=*it;
if(a[i].t<q)
{
int ss=s.count(q);
s.erase(q);
for(int j=1;j<ss;j++)s.insert(q);
s.insert(a[i].t);
k+=a[i].t-q;
}
}
}
printf("%d\n",ans);
return 0;
}