C. Alice and the Cake(优先队列)

传送门

题意:给你n个整数,问能不能一开始通过用一个整数n通过产生他的\frac{n}{2}和n-\frac{n}{2},然后可以通过这些数不断进行操作之后构造给的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;
}

今天还是喜欢粉毛的一天~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值