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≤10e5) — the size of the array.e
The second line contains n integers a1,a2,…,an (1≤ai≤10e9) — the elements of the array.e
ouput:
Print “YES” if it is possible to make all elements zero, otherwise print “NO”.
在这里插入图片描述
题目大意:
给出一个数组(大小不限但是在10e5内=>所以暴力不能解决问题),随机取两个数减1,求达到一定次数后这组数能不能变成全0.
题解:两个数随机取然后减一,我们可以反过来思考。
先一个一个的放(逆向思维),已知数组的大小,假设为n,这相当于有n个洞,随机选两个洞各放一个小球(不可能同时放一个洞中),因此每次放的时候,总数额加2,因此若是这个数组满足题目条件,这数组给个数字的和一定为偶数,那么第一个个条件便出来了。
继续丢小球,我们可以知道最满的那个洞,球最多,因为球是两个洞一起丢的,所以那个洞中的球数一定小于或等于总球数的一半,即总球数—最多洞的球数>=最多洞的球数,则另一个条件就是此。
总结——满足两个条件:
1:总数和为偶数;
2:总数和-最大数>=最大数;
代码如下:

#include<cmath>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cstdio>
#include<map>
using namespace std;
#define INE 10e8
#define ll long long
int main(void)
{
	int n;
	cin>>n;
	ll m=0,sum=0;
	ll p;
	for(int i=0;i<n;i++)
	{
		cin>>p;
		m=max(p,m);
		sum+=p;
	}
	if(sum%2==0&&(sum-m)>=m)
	{
		cout<<"YES";
	}else{
		cout<<"NO";
	}
	return 0;
 } 

如有不足,欢迎指出。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值