Codeforces Round #577 (Div. 2)--B. Zero Array

菜鸡我认为的好题
题意:这里有n个数,两两相互减1,执行n次,问有没有可能这n个数全都变为0;
做法:首先和是奇数是不可能的,然后和为偶数的时候:比如数据
2
2 6 那么结果是不可以的,变 -> 0 4;那么说明了 最大值大于sum/2的话,那么把非最大值的数都抵消掉了,然而抵消过后的最大值就没有地抵消了;相反,最大值小于sum/2的话,那么和也是偶数,最大值是会被其他非最大值一直抵消,知道最大值为0;

B. Zero Array
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given an array a1,a2,…,an.

In one operation you can choose two elements ai and aj (i≠j) and decrease each of them by one.

You need to check whether it is possible to make all the elements equal to zero or not.

Input
The first line contains a single integer n (2≤n≤105) — the size of the array.

The second line contains n integers a1,a2,…,an (1≤ai≤109) — the elements of the array.

Output
Print “YES” if it is possible to make all elements zero, otherwise print “NO”.

Examples
inputCopy
4
1 1 2 2
outputCopy
YES
inputCopy
6
1 2 3 4 5 6
outputCopy
NO
Note
In the first example, you can make all elements equal to zero in 3 operations:

Decrease a1 and a2,
Decrease a3 and a4,
Decrease a3 and a4
In the second example, one can show that it is impossible to make all elements equal to zero.

AC_Code

#include 
using namespace std;

int a[100000+10];
int n;
int main(){
	ios::sync_with_stdio(false);
	while(cin >> n){
		long long sum = 0;
		for(int i = 0;i< n;i++){
			cin >> a[i];
			sum += a[i];
		}
		if(sum & 1){
			cout << "NO" << endl;
			continue; 
		}
		sort(a,a+n);          //两两相互减 
		if(a[n-1] > sum/2){    //最大值比 sum/2 大,说明最大值是自己与自己相互抵消的 
			cout << "NO" << endl;
		}else{
			cout << "YES" << endl;
		}
	}
}

你可能感兴趣的:(codeforces)