Codeforces725D-Contest Balloons(优先队列+模拟)

14 篇文章 0 订阅
7 篇文章 0 订阅

题目链接

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值