经典贪心
先按h小到大排
然后搞个极大堆
把所有伤员的t一个一个加进堆中
如果 堆总和+现在伤员的t<=现在伤员的h 就把现在伤员的t加入堆中并更新堆总和
否则如果 现在伤员的t<堆中最大值 就把最大值换成现在伤员的t 维护堆 和总和
最后堆元素数就是答案了
#include<cstdio>
#include<queue>
#include<algorithm>
using namespace std;
struct ntype
{
int h,t;
};
ntype a[50010];
priority_queue <int> q;
bool cmp(const ntype &x,const ntype &y)
{
return(x.h<y.h);
}
int main()
{
int n,time=0;
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d %d",&a[i].h,&a[i].t);
sort(a+1,a+n+1,cmp);
for(int i=1;i<=n;i++)
{
if(time+a[i].t<=a[i].h)
{
time+=a[i].t;
q.push(a[i].t);
continue;
}
if(q.top()>a[i].t)
{
time=time-q.top();
q.pop();
q.push(a[i].t);
time+=a[i].t;
continue;
}
}
printf("%d",q.size());
return 0;
}