把所有的队伍分成两种,一种是气球数多于自己的(当前排名比自己高的),一种是气球数不大于自己的
首先求出初始排名,然后肯定是要每次把气球给当前排名高于自己队的队伍(助人飞升),显然可以用堆维护,每次选出(t-w+1)最小的队伍
然后自己队伍的气球会减少,刷新自己的排名刷新答案并且把气球数多于自己队伍的队伍放入堆中
#include<cstdio>
#include<algorithm>
#include<queue>
#define N 300005
#define LL long long
#define RG register
using namespace std;
inline LL read()
{
LL a=0;int f=1;char c=getchar();
while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
while(c>='0'&&c<='9'){a=a*10+c-'0';c=getchar();}
return a*f;
}
struct node
{
LL t,w;
bool operator < (const node &c) const {return t>c.t;}
}team[N];
LL n,t,w;
int ans,_d;
priority_queue<LL,vector<LL>,greater<LL> >heap;
int main()
{
n=read()-1;
int i;
t=read(),w=read();
for(i=1;i<=n;++i)
team[i].t=read(),team[i].w=read();
sort(team+1,team+n+1);
i=1;
while(i<=n&&team[i].t>t) heap.push(team[i].w-team[i].t+1),++i;
ans=i;
while(!heap.empty())
{
LL x=heap.top();heap.pop();
if(x>t) break;
t-=x;++_d;
while(i<=n&&team[i].t>t) heap.push(team[i].w-team[i].t+1),++i;
ans=min(ans,i-_d);
}
printf("%d\n",ans);
return 0;
}