Codeforces Round #577 (Div. 2), problem: (B)
问题链接:https://codeforces.com/contest/1201/problem/B
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".
题目大意
给定一个数组a1 a2…an。
在一个操作中,您可以选择两个元素ai和aj (i≠j),并将它们分别减少1。
您需要检查是否有可能使所有元素都为零。
输入
第一行包含一个整数n(2≤n≤105)——数组的大小。
第二行包含n个整数a1,a2,…,an(1≤ai≤109)——数组的元素。
输出
如果可以使所有元素为零,则打印“YES”,否则打印“NO”。
分析
读完题目感觉很简单 就是 看总数能否减2。所以我直接开始sum 并对每个cin都%2 但是test9就GG了
最后阅读大佬的 代码
大佬加入了 sum>=2$*$max(cin) 的判断 还是有点理解不了
代码
package cf;
import java.util.Scanner;
public class BZeroArray {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
int n = cin.nextInt();
int m = 0;
long sum = 0;
for (int i = 0; i < n; i++) {
int a = cin.nextInt();
sum += a;
if (a > m) m = a;
}
if (sum % 2 == 0 && sum - m >= m) {
System.out.println("Yes");
} else {
System.out.println("NO");
}
}
}
本文地址:https://dxoca.cn/Algorithm/221.html 百度已收录
版权说明:若无注明,本文皆为“Dxoca's blog (寒光博客)”原创,转载请保留文章出处。
最大数如果不过sum两倍,不是就没得其他数减了?
打错了,不是
不过
,是过
你打字是怎么这么快的。厉害
,,,,
先同时从两边取,哪边少哪边先取,取到只剩中间一个元素
现在左边有一些元素,右边有一些元素,并且左右两边的差已经是尽量小了
这时候首先要用中间的这个补齐这个差距,于是中间的损失了一份两边的差值,同时两边都同时变为两边的最小值
这时候可以用两边的齐心合力消除中间的这一条,此时两边的总和已经相等,只要中间的是个偶数并且不超过两边之和,两边就可以拿出相同的数量把中间的消除,并且之后两边互相湮灭
其实只需要想两点就行吧!1.能整除2
2.取最大数,如果没有相应的量供最大数减,那么不成立。如果成立,那么次大数可被减
话说你打字怎么这么快呀!别告我是复制的
你这么一说 似乎明白了。
很精彩,,
我,,还是 不清楚,, 刚看到大佬这样说
哦!为什大佬一个我的问题都不回答୧(๑•̀⌄•́๑)૭
啊哈qwq 对是 复制粘贴呐 可以是没得减了
OωO好吧!话说全insert怎么修好的
一个js 的问题 顺便刚刚把 评论一言再优化了一下 点击评论框 insert不变