标签:case Division weight Fair candies int test YES
Fair Division
Alice and Bob received n candies from their parents. Each candy weighs either 1 gram or 2 grams. Now they want to divide all candies among themselves fairly so that the total weight of Alice’s candies is equal to the total weight of Bob’s candies.
Check if they can do that.
Note that candies are not allowed to be cut in half.
#include <stdio.h>
#include <string.h>
#define maxn 11111
int a[maxn];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n;
int sum=0;
int count1=0,count2=0;
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
if(a[i]==1){
count1++;
}
else
{
count2++;
}
sum+=a[i];
}
if(sum%2==0)
{
if(count1==0)
{
if(count2%2==0)
printf("YES\n");
else
printf("NO\n");
}
else
printf("YES\n");
}
else
printf("NO\n");
}
return 0;
}
INPUT
The first line contains one integer t (1≤t≤104) — the number of test cases. Then t test cases follow.
The first line of each test case contains an integer n (1≤n≤100) — the number of candies that Alice and Bob received.
The next line contains n integers a1,a2,…,an — the weights of the candies. The weight of each candy is either 1 or 2.
It is guaranteed that the sum of n over all test cases does not exceed 105.
OUTPUT
For each test case, output on a separate line:
“YES”, if all candies can be divided into two sets with the same weight;
“NO” otherwise.
You can output “YES” and “NO” in any case (for example, the strings yEs, yes, Yes and YES will be recognized as positive)
Example
input
5
2
1 1
2
1 2
4
1 2 1 2
3
2 2 2
3
2 1 2
output
YES
NO
YES
NO
NO
Note
In the first test case, Alice and Bob can each take one candy, then both will have a total weight of 1.
In the second test case, any division will be unfair.
In the third test case, both Alice and Bob can take two candies, one of weight 1 and one of weight 2.
In the fourth test case, it is impossible to divide three identical candies between two people.
In the fifth test case, any division will also be unfair.
标签:case,Division,weight,Fair,candies,int,test,YES 来源: https://blog.csdn.net/qq_33962143/article/details/112912433
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。