C. Alice and the Cake
题意
你现在有一块蛋糕,你可以切n-1刀,且完后的两块,一块是x/2,一块是(x+1)/2,现在告诉你切完之后的有n块,并告诉你每一块的大小,现在问你能否得到蛋糕的初始大小,并且执行了n-1次操作后变为给你的蛋糕大小。
思路
首先,我们可以根据给你的蛋糕得到初始的蛋糕大小,然后我们现在可以模拟切蛋糕的方式,假设蛋糕的初始大小为sum,并且出现的大小为mp[x],那么我们可以从最初的大小开始切,没切一刀,判断且完后的两块蛋糕是否出现过,如果出现过,我们就不动这一块,转战另一块,否则就切这一块蛋糕,一分为二。
#include <bits/stdc++.h>
#define int long long
using namespace std;
void solve()
{
int n; cin >> n;
int sum = 0;
vector<int> now, tmp;
map<int, int> mp;
int len = n;
while (n --)
{
int x; cin >> x;
sum += x;
mp[x] ++;
}
now.push_back(sum);
while (now.size() && len >= now.size() && len)
{
for (auto it : now)
if (mp[it]) { mp[it] --; len --; }
else
{
if (it >= 2) { tmp.push_back(it / 2); tmp.push_back((it + 1) / 2); }
else tmp.push_back(1);
}
now = tmp;
tmp.clear();
}
if (len) cout << "NO" << endl;
else cout << "YES" << endl;
}
signed main()
{
std::ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t; cin >> t;
while (t --) solve();
return 0;
}