正题
很明显可以知道只通过T1贪或者T2都是不行的。
如果让你做,你会怎么做?
首先把T2排好序。
其次从前往后,如果可以做的肯定要做,这个没有什么问题。
如果做不了呢?我们就看一下前面有没有什么占用空间更大的,把它删掉。
这样做贡献不变,剩余时间变多,不断调整我们的策略,到最后就是最优的,而且找不出更优的策略。
为了实现,我们在这里用了大根堆找出一个最大的。
/**************************************************************
Problem: 1029
User: Deep_Kevin
Language: C++
Result: Accepted
Time:436 ms
Memory:2852 kb
****************************************************************/
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
int n;
struct node{
int t1,t2;
bool operator<(const node a)const{
return t2<a.t2;
}
}s[150010];
priority_queue<int> f;
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%d %d",&s[i].t1,&s[i].t2);sort(s+1,s+1+n);
int now=1;
while(s[now].t1>s[now].t2 & now<=n) now++;
int t=1;
if(now==n+1) t=0;
else{
f.push(s[now].t1);
int tot=s[now].t1;
for(int i=now+1;i<=n;i++){
if(tot+s[i].t1<=s[i].t2) f.push(s[i].t1),tot+=s[i].t1,t++;
else if(f.top()>=s[i].t1) tot+=s[i].t1-f.top(),f.pop(),f.push(s[i].t1);
}
}
printf("%d\n",t);
}