【优先队列】Alice and the Cake

Alice and the Cake

  • 初看觉得有点像哈夫曼,但实际有差别。
  • 不过使用优先队列的思路是对的,但使用一个还不行,要用两个!!
代码如下
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define LL long long
#define UIT unsigned int
#define DB double
#define PII pair<int, int>
#define MKPR(a, b) make_pair(a, b)
#define MMST(a) memset(a, 0, sizeof(a))
#define PB push_back
#define FOR(i, beg, ed, s) for (int i = beg; i < ed; i += s)
#define FORE(i, beg, ed, s) for (int i = beg; i <= ed; i += s)
#define LOOP while (true)
#define IN scanf
#define OUT printf
using namespace std;

priority_queue<LL, vector<LL>> pq, qq;

void solve() {
    int t = 0, n = 0;
    LL tt = 0, sum = 0;
    bool ok = false;
    IN("%d", &t);
    while (t--) {
        IN("%d", &n);
        while (!pq.empty()) pq.pop();
        while (!qq.empty()) qq.pop();
        sum = 0;
        FOR(i, 0, n, 1) {
            IN("%lld", &tt);
            pq.push(tt);
            sum += tt;
        }

        ok = true;
        qq.push(sum);
        while ((!pq.empty()) && (!pq.empty())) {
            tt = qq.top();
            if (tt > pq.top()) {
                qq.pop();
                qq.push(tt / 2);
                qq.push(tt - tt / 2);
            } else if (tt == pq.top()) {
                pq.pop();
                qq.pop();
            } else {
                ok = false;
                break;
            }
        }

        OUT(ok ? "YES\n" : "NO\n");
    }
}

int main(void) {
    solve();
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

木又可可

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值