码迷,mamicode.com
首页 > 其他好文 > 详细

B-Fair Division

时间:2021-01-26 11:57:06      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:ISE   bsp   out   math   www   scanf   guarantee   code   title   

Alice and Bob received nn 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.

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 nn (1≤n≤100) — the number of candies that Alice and Bob received.

The next line contains nn integers a1,a2,,ana1,a2,…,an — the weights of the candies. The weight of each candy is either 11 or 22.

It is guaranteed that the sum of nn over all test cases does not exceed 105105.

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
 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 
 4 int main()
 5 {
 6     int t;
 7     scanf("%d",&t);
 8     while(t--)
 9     {
10         int n,i,a[101],sum=0;
11         scanf("%d",&n);
12         for(i=0; i<n; i++)
13         {
14             scanf("%d",&a[i]);
15             sum=sum+a[i]; //求出糖果的总质量
16         }
17         int a1=0,a2=0;
18         for(i=0; i<n; i++)
19         {
20             if(a[i]==1)
21                 a1++;//统计1g糖果的质量
22             else
23                 a2++;//统计2g糖果的质量
24         }
25         if(sum%2==0)
26         {
27             if(a2==n)
28             {
29                 if(a2%2==0)
30                     printf("YES\n");
31                 else
32                     printf("NO\n");
33             }
34             else
35                 printf("YES\n");
36         }
37         else
38             printf("NO\n");
39     }
40 
41 }

要均匀分给两个人,同时糖果不能分成两半,那么糖果总质量为偶数。这里要注意糖果总质量为偶数意味着1g糖果的数量是偶数个,那么就只需要讨论

2g糖果数量的奇偶,为偶数时必定可以均分,为奇数可以将2个1g糖果视为1个2g糖果,剩下的偶数个1g糖果仍可均分,那么奇数个2g糖果也可均分。这

里要注意,当全部是2g糖果时奇数个不能均分。

B-Fair Division

标签:ISE   bsp   out   math   www   scanf   guarantee   code   title   

原文地址:https://www.cnblogs.com/eagle2/p/14320921.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!