首先将每个队伍按照气球量从大到小排序,将大于XXX的队伍压入队列,在队列中按照 体重-气球量+1 从小到大排序。 当XXX的气球减少时,就需要把之前没进入队列但此时气球量大于XXX的队伍压入队列,不断更新最小ans。
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#define ll long long
using namespace std;
priority_queue<ll,vector<ll>,greater<ll> >H;
pair<ll,ll> in[300001];
int main()
{
int n, i, p=1, m=0;
scanf("%d",&n);
for (int i=0;i<n;i++) scanf("%lld%lld",&in[i].first,&in[i].second);
sort(in+1,in+n,greater<pair<ll,ll> >());
for (;p<n&&in[0].first<in[p].first;p++)
{
H.push(in[p].second-in[p].first+1);
}
m=H.size();
while (!H.empty())
{
in[0].first -= H.top(); H.pop();
if (in[0].first<0) break;
for (;p<n&&in[0].first<in[p].first; p++)
{
H.push(in[p].second-in[p].first+1);
}
if (m>H.size()) m=H.size();
}
printf("%d",m+1);
return 0;
}