题意:给你n个整数,问能不能一开始通过用一个整数n通过产生他的和n-,然后可以通过这些数不断进行操作之后构造给的n个数。
思路:可以先把所有的数先标记一下存在的次数然后把他们加起来,用优先队列(递减的)把最大的不断分解到两个较小的数,如果在标记中就标记减减,否则就继续往小的分解,如果这个数不被标记中就往小分解,当队列中的数量超过n了就说明有不应该存在的数或者去除的标记数不够就说明了并不能通过一开始的值分解成原序列。
代码:
/**
* ┏┓ ┏┓+ +
* ┏┛┻━━━┛┻┓ + +
* ┃ ┃
* ┃ ━ ┃ ++ + + +
* ████━████+
* ◥██◤ ◥██◤ +
* ┃ ┻ ┃
* ┃ ┃ + +
* ┗━┓ ┏━┛
* ┃ ┃ + + + +Code is far away from
* ┃ ┃ + bug with the animal protecting
* ┃ ┗━━━┓ 神兽保佑,代码无bug
* ┃ ┣┓
* ┃ ┏┛
* ┗┓┓┏━┳┓┏┛ + + + +
* ┃┫┫ ┃┫┫
* ┗┻┛ ┗┻┛+ + + +
*/
#include<cstdio>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <string>
#include <math.h>
#include<vector>
#include<queue>
#include<map>
#define sc_int(x) scanf("%d", &x)
#define sc_ll(x) scanf("%lld", &x)
#define pr_ll(x) printf("%lld", x)
#define pr_ll_n(x) printf("%lld\n", x)
#define pr_int_n(x) printf("%d\n", x)
#define ll long long
using namespace std;
const int N=1000000+100;
int n ,m,h;
ll s[N];
void solve()
{
priority_queue <ll,vector<ll>,less<ll> >cnt;
map<int,int>q;
cin>>n;
ll res=0;
for(int i=1;i<=n;i++){
cin>>s[i];
q[s[i]]++;
res+=s[i];// 2 3 3 4 4 4//
}
sort(s+1,s+1+n);
cnt.push(res);
int num=1;
if(n==1){
cout<<"YES\n";
return ;
}
int sum=0;
while(cnt.size()<=n&&cnt.size())
{
ll k=cnt.top();
cnt.pop();
if(k==1)continue;
ll x,y;
y=x=k/2;
if(k%2!=0)y++;
if(q[x]){ q[x]--,sum++;}
else cnt.push(x);
if(q[y]) {q[y]--,sum++;}
else cnt.push(y);
}
if(sum!=n)printf("NO\n");
else printf("YES\n");
}
int main()
{
int t;
sc_int(t);
while(t--)solve();
return 0;
}
今天还是喜欢粉毛的一天~