题目链接
http://codeforces.com/contest/725/problem/D
思路
贪心,每次选择气球比自己多,并且飘起来需要最小气球的队伍,将气球给他
但是每个给完气球后之前比自己气球少的队伍会更新,因此用优先队列来维护每次状态
代码
#include <iostream>
#include <cstring>
#include <stack>
#include <vector>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <sstream>
#include <iomanip>
#include <fstream>
#include <cstdio>
#include <cstdlib>
#include <climits>
#include <deque>
#include <bitset>
#include <algorithm>
using namespace std;
#define PI acos(-1.0)
#define LL long long
#define PII pair<int, int>
#define PLL pair<LL, LL>
#define mp make_pair
#define IN freopen("in.txt", "r", stdin)
#define OUT freopen("out.txt", "wb", stdout)
#define scan(x) scanf("%d", &x)
#define scan2(x, y) scanf("%d%d", &x, &y)
#define scan3(x, y, z) scanf("%d%d%d", &x, &y, &z)
#define sqr(x) (x) * (x)
const int maxn = 300000 + 5;
struct node {
LL t, w, p;
bool operator < (const node& b) const {
return t < b.t || (t == b.t && p > b.p);
}
} a[maxn];
int n, pos;
LL b[maxn];
struct cmp {
bool operator() (node a, node b) {
return (b.w - b.t) < (a.w - a.t);
}
};
priority_queue<LL, vector<LL>, greater<LL> > q;
priority_queue<node> q2;
int main() {
scan(n);
for (int i = 0; i < n; i++) {
scanf("%I64d%I64d", &a[i].t, &a[i].w);
a[i].p = i;
}
LL tn = a[0].t, wn = a[0].w;
sort(a, a + n);
for (int i = 0; i < n; i++)
if (a[i].p == 0) pos = i;
for (int i = pos + 1; i < n; i++) q.push(a[i].w - a[i].t);
for (int i = 0; i < pos; i++) q2.push(a[i]);
int res = pos, k = 0, tres = pos;
while (!q.empty() && tn > 0) {
if (tn > q.top()) {
tn -= q.top() + 1; q.pop();
tres++;
while (!q2.empty() && q2.top().t > tn) {
node _t = q2.top(); q2.pop();
q.push(_t.w - _t.t);
tres--;
}
res = max(res, tres);
} else {
break;
}
}
cout << n - res << endl;
return 0;
}