题目链接:codeforces 725D . Contest Balloons
先按气球数从大到小排序求出初始名次,并把名次排在第一队前面的队放入优先队列,按w-t-1值从小到大优先,然后依次给气球给排名在前面的的队,给完后自己的气球数减少,继续跟之前排在第一队后面的队比较,考虑是否加入队列,每次记录最好的名次。

1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<queue> 5 using namespace std; 6 typedef long long ll; 7 const int N = 300001; 8 struct team{ 9 ll t, w, wa; 10 team(ll t = 0, ll w = 0):t(t),w(w) { wa = w - t + 1; } 11 bool operator < (const team &r)const{ 12 return r.wa < wa; 13 } 14 }a[N]; 15 bool cmp(team x, team y){ 16 return x.t > y.t; 17 } 18 priority_queue<team>q; 19 int main(){ 20 int n, i, j; 21 ll ans, a1; 22 scanf("%d", &n); 23 for(i = 0; i < n; ++i) 24 scanf("%lld%lld", &a[i].t, &a[i].w); 25 sort(a+1, a+n, cmp); 26 for(i = 1; i < n; ++i){ 27 if(a[i].t > a[0].t) 28 q.push(team(a[i].t, a[i].w)); 29 else break; 30 } 31 a1 = ans = i;//初始名次 32 while(!q.empty()){ 33 team tt = q.top(); q.pop(); 34 a[0].t -= tt.wa; 35 if(a[0].t < 0) break; 36 for(j = i; j < n; ++j){ 37 if(a[j].t > a[0].t){ 38 q.push(team(a[j].t , a[j].w));} 39 else break; 40 } 41 a1 += j - i - 1; 42 i = j; 43 ans = min(ans, a1); 44 } 45 printf("%lld ", ans); 46 return 0; 47 }