Codeforces Round #618 (Div. 2)-Non-zero
Guy-Manuel and Thomas have an array a of n integers [a1,a2,…,an]. In one step they can add 1 to any element of the array. Formally, in one step they can choose any integer index i (1≤i≤n) and do ai:=ai+1.
If either the sum or the product of all elements in the array is equal to zero, Guy-Manuel and Thomas do not mind to do this operation one more time.
What is the minimum number of steps they need to do to make both the sum and the product of all elements in the array different from zero? Formally, find the minimum number of steps to make a1+a2+ … +an≠0 and a1⋅a2⋅ … ⋅an≠0.
Input
Each test contains multiple test cases.
The first line contains the number of test cases t (1≤t≤103). The description of the test cases follows.
The first line of each test case contains an integer n (1≤n≤100) — the size of the array.
The second line of each test case contains n integers a1,a2,…,an (−100≤ai≤100) — elements of the array .
Output
For each test case, output the minimum number of steps required to make both sum and product of all elements in the array different from zero.
Example
input
4
3
2 -1 -1
4
-1 0 0 1
2
-1 2
3
0 -2 1
output
1
2
0
2
Note
In the first test case, the sum is 0. If we add 1 to the first element, the array will be [3,−1,−1], the sum will be equal to 1 and the product will be equal to 3.
In the second test case, both product and sum are 0. If we add 1 to the second and the third element, the array will be [−1,1,1,1], the sum will be equal to 2 and the product will be equal to −1. It can be shown that fewer steps can't be enough.
In the third test case, both sum and product are non-zero, we don't need to do anything.
In the fourth test case, after adding 1 twice to the first element the array will be [2,−2,1], the sum will be 1 and the product will be −4.
这个题是说通过最小的修改次数,是数列和不能为0,乘积不能为0;
那么也即数列中不存在0,如果存在0的一定要改,存在0的只能变成1,那我们考虑变成1之后,的和是否等于0,如果等于,就在修改1个,即cnt+1。
#include<bits/stdc++.h>
using namespace std;
const int N=5e5;
#define read(a) scanf("%d",&a);
int a[N];
int main()
{
int t;
read(t);
while(t--){
int n;
read(n);
long long sum=0;
int cnt=0;
for(int i=1;i<=n;i++){
cin>>a[i];
sum+=(long long)a[i];
if(a[i]==0) cnt++;
}
if(cnt==0)
{
if(sum!=0) cout<<0<<endl;
else cout<<1<<endl;
}
else {
if(cnt+sum==0) cout<<cnt+1<<endl;
else cout<<cnt<<endl;
}
}
}
Codeforces Round #618 (Div. 2)-Non-zero的更多相关文章
- Codeforces Round #618 (Div. 2)
题库链接 https://codeforces.ml/contest/1300 A. Non-zero 一个数组,每次操作可以给某个数加1,让这个数组的积和和不为0的最小操作数 显然如果有0的话,必须 ...
- Codeforces Round #618 (Div. 1)C(贪心)
把所有数看作N块,后面的块比前面的块小的话就合并,这个过程可能会有很多次,因为后面合并后会把前面的块均摊地更小,可能会影响更前面地块,像是多米诺骨牌效应,从后向前推 #define HAVE_STRU ...
- Codeforces Round #618 (Div. 1)B(几何,观察规律)
观察猜测这个图形是中心对称图形是则YES,否则NO #define HAVE_STRUCT_TIMESPEC #include<bits/stdc++.h> using namespace ...
- Codeforces Round #618 (Div. 1)A(观察规律)
实际上函数值为x&(-y) 答案仅和第一个数字放谁有关 #define HAVE_STRUCT_TIMESPEC #include <bits/stdc++.h> using na ...
- Codeforces Round #618 (Div. 2)A. Non-zero
Guy-Manuel and Thomas have an array aa of nn integers [a1,a2,…,an ]. In one step they can add 11 to ...
- Codeforces Round #618 (Div. 2)C. Anu Has a Function
Anu has created her own function ff : f(x,y)=(x|y)−y where || denotes the bitwise OR operation. For ...
- Codeforces Round #618 (Div. 2) 小号上紫之路
这一场涨了不少,题也比较偏思维,正好适合我 A. Non-zero 我们记录这些数字的总和sum,并且记录0的个数zero,显然答案应该是这些0的个数,注意如果sum+zero==0的话答案要额外加一 ...
- [CF百场计划]#2 Codeforces Round #618 (Div. 2)
A. Non-zero Description: Guy-Manuel and Thomas have an array \(a\) of \(n\) integers [\(a_1, a_2, \d ...
- Codeforces Round #618 (Div. 2)-B. Assigning to Classes
Reminder: the median of the array [a1,a2,-,a2k+1] of odd number of elements is defined as follows: l ...
随机推荐
- 给定一个整数数组 nums 和一个目标值 target,求nums和为target的两个数的下表
这个是来自力扣上的一道c++算法题目: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案 ...
- MTK Android 源码目录分析
Android 源码目录分析 Android 4.0 |-- abi (application binary interface:应用二进制接口)|-- art (average retrieval ...
- vscode如何安装eslint插件 代码自动修复
ESlint:是用来统一JavaScript代码风格的工具,不包含css.html等. 方法和步骤: 通常情况下vue项目都会添加eslint组件,我们可以查看webpack的配置文件package. ...
- centos7安装jmeter + ant
1.xshell链接上centos7服务器 先安装jmeter 使用wget jmeter-xxxxxxxxxxxx进行联网自动下载(先进入jmeter官网,然后找到要下载的.tgz压缩包,然后右键 ...
- tf.nn.softmax_cross_entropy_with_logits 分类
tf.nn.softmax_cross_entropy_with_logits(logits, labels, name=None) 参数: logits:就是神经网络最后一层的输出,如果有batch ...
- 搭建vue2.0开发环境及手动安装vue-devtools工具
安装vue脚手架 1.安装node.js,如果安装成功输入 node -v ,查看node版本号,输入npm -v查看npm版本 https://nodejs.org/en/ 2.注册淘宝镜像,定制的 ...
- 发布公开的pod
发布公开的pod 方便项目 通过cocoapods 使用,便于版本版本管理,下面是简单步奏: 0.首次操作先要注册Trunk: pod trunk register zhujin001xb@163.c ...
- Ajax 简述与基础语法
目录 Ajax 1. 原生 JS 实现 Ajax 2. 使用 Ajax 实现异步通信 a. Ajax 的基础语法 b. 用 Ajax 传递数据 i. 传递字符串数据 ii. 传递 JSON 数据 3. ...
- Daily Scrum 12/16/2015
Process: Dong&Minlong : 继续对Oxford Speech 接口进行调试,并且完成了相应工作的转接. Yandong@Zhaoyang: 完成了对一些Bug的修复工作,程 ...
- 我是如何一步步的在并行编程中将lock锁次数降到最低实现无锁编程
在并行编程中,经常会遇到多线程间操作共享集合的问题,很多时候大家都很难逃避这个问题做到一种无锁编程状态,你也知道一旦给共享集合套上lock之后,并发和伸缩能力往往会造成很大影响,这篇就来谈谈如何尽可能 ...