贪心
题目大意:
给你n支队伍,每支队伍都有一个重量和一个气球数。气球数越多排名越前。如果一支队伍的气球数大于这支队伍的重量,就会飞起来。你的队伍可以给别的队伍气球使自己的排名靠前。求能达到的最好名次。
思路:
刚开始按照气球数排序,建一个小根堆维护在自己前面的队伍中所需最少气球能够飞走的队伍,每次取出一个后让它飞走,然后把气球数比当前多的队伍加入堆中,记录最小答案即可。
代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#define MAXN 300000
#define LL long long
using namespace std;
struct haha{
LL we;
LL num;
bool operator < (const haha &x) const{
return (we-num>x.we-x.num);
};
};
priority_queue <haha> que;
int point,n;
LL rem;
haha a[MAXN+5];
bool comp(haha x,haha y){
return x.num>y.num;
}
int main(){
scanf("%d",&n);
for (int i=1;i<=n;i++)
scanf("%lld%lld",&a[i].num,&a[i].we);
rem=a[1].num;
sort(a+2,a+n+1,comp);
while (!que.empty())
que.pop();
for (int i=2;i<=n;i++)
if (a[i].num<=rem){
point=i;
break;
}
else{
que.push(a[i]);
if (i==n)
point=i+1;
}
int bst=point-1;
while (rem>0&&que.empty()!=true){
haha x=que.top();
que.pop();
rem-=(x.we-x.num+1);
if (rem<0)
break;
while (point<=n&&a[point].num>rem){
que.push(a[point]);
point++;
}
int kk=que.size();
bst=min(bst,kk+1);
}
printf("%d\n",bst);
return 0;
}