14行代码AC_Zero Array(思维)
生活随笔
收集整理的這篇文章主要介紹了
14行代码AC_Zero Array(思维)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
立志用更少的代碼做更高效的表達
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”.
Input
4
1 1 2 2
Output
YES
Input
6
1 2 3 4 5 6
Output
NO
思路: 考查思維,判斷和是否是偶數或最大值大于和的一半。
代碼展示
#include<bits/stdc++.h> using namespace std; int main() {long long n; while(cin>>n) {long long sum = 0;long long Max = -1;for(int i = 0; i < n; i++) {long long x; cin>>x;sum += x;Max = max(Max, x);}cout << (sum%2==1||Max>sum/2 ? "NO" : "YES") << '\n';} return 0; }每日一句
先努力讓自己發光,對的人才能迎著光而來。
總結
以上是生活随笔為你收集整理的14行代码AC_Zero Array(思维)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C语言满分代码:L1-059 敲笨钟 (
- 下一篇: include指令与动作的区别【贴心,简