- 初看觉得有点像哈夫曼,但实际有差别。
- 不过使用优先队列的思路是对的,但使用一个还不行,要用两个!!
代码如下
#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;
}