题目传送门:http://codeforces.com/problemset/problem/725/D
题意:队伍按气球数量进行排名,每个队伍还有一个重量,你可以将你的气球给别的队,使他们的气球数量大于其队伍重量,他们就会被淘汰
思路:建一个优先队列,将气球数量大于自己的队伍扔如队列中。队头是w-t较小的队伍,然后将气球给队头队伍,给气球后,可能又有新的队伍比自己的气球多就将他们也扔入队列。直到队列为空或自己队的气球无法再给任何队。记录在给气球过程中,自己队伍的最佳排名
代码如下:
#include <iostream>
#include <algorithm>
#include <cstring>
#include <stdio.h>
#include <string>
#include <cmath>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include <bitset>
#include <cstdlib>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define ll long long
#define ull unsigned long long
#define mem(n,v) memset(n,v,sizeof(n))
#define MAX 300005
#define MAXN 10005
#define PI 3.1415926
#define E 2.718281828459
#define opnin freopen("text.in.txt","r",stdin)
#define opnout freopen("text.out.txt","w",stdout)
#define clsin fclose(stdin)
#define clsout fclose(stdout)
#define haha1 cout << "haha1"<< endl
#define haha2 cout << "haha2"<< endl
#define haha3 cout << "haha3"<< endl
const int INF = 0x3f3f3f3f;
const ll INFF = 0x3f3f3f3f3f3f3f3f;
const double pi = 3.141592653589793;
const double inf = 1e18;
const double eps = 1e-8;
const ll mod = 1e18;
const ull mx = 133333331;
/**************************************************************************/
int n;
struct team{
ll t,w;
}teams[MAX];
bool cmp(const struct team a,const struct team b){
return a.t < b.t;
}
struct cmp1
{
bool operator()(const struct team a,const struct team b){
return a.w-a.t > b.w-b.t;
}
};
priority_queue <struct team,vector<struct team>,cmp1> q;
int main()
{
// opnin;
// opnout;
cin >> n;
n--;
ll t,w;
cin >> t >> w;
for(int i=0;i<n;i++){
scanf("%lld %lld",&teams[i].t,&teams[i].w);
// cout << teams[i].t << ' ' << teams[i].w << endl;
}
sort(teams,teams+n,cmp);
// for(int i = 0;i < n;i++) cout << teams[i].t << ' ' << teams[i].w << endl;
int cnt = 0;
while(cnt < n && t >= teams[cnt].t) cnt++;
for(int i=cnt;i<n;i++) q.push(teams[i]);
int Min = n - cnt + 1;
// haha1;
// cout << Min << endl;
while(!q.empty()){
struct team temp = q.top();
// cout << "temp: " << temp.t << ' ' << temp.w << endl;
if(t >= temp.w - temp.t){
t -= (temp.w - temp.t + 1);
q.pop();
n--;
while(cnt > 0 && t < teams[cnt-1].t){
q.push(teams[cnt-1]);
cnt--;
}
Min = min(Min,n - cnt + 1);
}
else break;
// cout << "Min: " << Min << endl;
}
cout << Min << endl;
// clsin;
// clsout;
return 0;
}