Codeforces Round #693 (Div. 3) B. 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.

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
inputCopy
5
2
1 1
2
1 2
4
1 2 1 2
3
2 2 2
3
2 1 2
outputCopy
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.
问能不能分成等数量的两组,没组里面只会有1或者2,那么我们统计1和2的个数来判断当前是否可以分成完全相等的两份就可以了,,

#include<map>
#include<stack>
#include<queue>
#include<string>
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#define ls (k<<1)
#define rs (k<<1|1)
#define pb push_back
#define mid ((l+r)>>1)
#include<bits/stdc++.h>
using namespace std;
const int p=1e4+7;
const int mod=1e9+7;
const int maxn=1e6+1;
typedef long long ll;
const int inf=0x3f3f3f3f;
int a[maxn],b[maxn];
void solve(){
    
    
      int t;
    cin>>t;
    while(t--)
    {
    
    
        int n;
        cin>>n;
        int one1=0;
        int two2=0;
        for(int i=1;i<=n;i++){
    
    
            int tmp;
            cin>>tmp;
            if(tmp==1){
    
    
                one1++;
            }
            else{
    
    
                two2++;
            }
        }
        if(one1%2==1){
    
    
            cout<<"NO"<<endl;
        }
        else{
    
    
            if(two2%2==1){
    
    
                if(one1>=1){
    
    
                    cout<<"YES"<<endl;
                }
                else{
    
    
                    cout<<"NO"<<endl;
                }
            }
            else{
    
    
                cout<<"YES"<<endl;
            }
    }
    }
}
 
signed main()
{
    
    
    ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	solve();
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_45891413/article/details/112297785
今日推荐