贪心,根据t2从小到大排序,用堆维护。
·剩余时间足够完成当前任务,则插入当前任务
·剩余时间不足完成当前任务,若当前任务的t1比堆中t1的最大值大,则不插入,否则插入
#include<cstdio>
#include<algorithm>
#define MAXN 150010
struct pro{int t1, t2;}p[MAXN];
using namespace std;
bool cmp(pro a, pro b){return a.t2<b.t2;}
int heap[MAXN],cnt=0;
void add(int x)
{
heap[++cnt]=x;
for(int i = cnt, j = cnt>>1; j; i=j, j>>=1)
{
if(heap[j]<heap[i])
swap(heap[i],heap[j]);
else break;
}
}
void del()
{
heap[1]=heap[cnt--];
for(int i = 1, j = 1<<1; j <= cnt; i=j, j<<=1)
{
if(j<cnt && heap[j]<heap[j|1])
j|=1;
if(heap[i]<heap[j])
swap(heap[i],heap[j]);
else break;
}
}
int main()
{
int n;
scanf("%d",&n);
for(int i =1; i <=n;i ++)
scanf("%d%d",&p[i].t1,&p[i].t2);
sort(p+1,p+n+1,cmp);
int last=0;
for(int i =1; i <=n;i ++)
{
last+=p[i].t2-p[i-1].t2;
if(p[i].t1<=last)
{
last-=p[i].t1;
add(p[i].t1);
}
else if(heap[1]>p[i].t1)
{
last+=heap[1]-p[i].t1;
del();
add(p[i].t1);
}
}
printf("%d\n",cnt);
return 0;
}