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 any element of the array. Formally, in one step they can choose any integer index ii (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+an≠0 and a1⋅a2⋅an≠0 .
Each test contains multiple test cases.
The first line contains the number of test cases tt (1≤t≤103). The description of the test cases follows.
The first line of each test case contains an integer nn (1≤n≤100) — the size of the array.
The second line of each test case contains nn integers a1,a2,…,an (−100≤ai≤100 ) — elements of the array .
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.
4
3
2 -1 -1
4
-1 0 0 1
2
-1 2
3
0 -2 1
1
2
0
2 水题。首先起码要满足每个数都不等于0,遍历时遇到为0的数先对其+1,同时ans++(进行了一次操作),同时sum(数列和)加上更新后的数(如果没更新就加上原数)。遍历完成后如果sum等于0的话,可以随便找一个数+1,同时ans++,这样能满足操作最少且sum不为0。最后输出ans即可。
#include <bits/stdc++.h>
using namespace std;
int t;
int main()
{
cin>>t;
while(t--)
{
int n;
cin>>n;
long long ans=;
int i;int temp; long long sum=;
for(i=;i<=n;i++)
{
scanf("%d",&temp);
if(temp==)
{
temp++;
ans++;
}
sum+=temp;
}
if(sum==)ans++;
cout<<ans<<endl;
}
}
Codeforces Round #618 (Div. 2)A. 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)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 ...
- 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 ...
随机推荐
- i5+GT730+B85安装OSX10.10.5 (Yosemite Install(14F27).cdr)
1.用windows磁盘管理工具分出10G分区,指定盘符,但不格式化 2.管理员身份打开“硬盘安装助手” 3.选择cdr文件,取消3个选择框,然后开始写入 4.有可能需要重置安装分区的磁盘标识为AF ...
- EAC3 Adaptive Hybrid Transform (AHT)
adaptive hybrid transform 由两个linear transforms级联组成. 第一个transform为MDCT,MDCT使用KBD window产生256个transfor ...
- 【Python】摄氏度与华氏度互相转化
Python入门程序,大家可以举一反三,进行各种转换,比如单位转化,货币转化等等,自行发挥即可! 原理: 代码: Tempstr=input("请输入带有符号的温度值:\n")# ...
- 爬虫入门 beautifulsoup库(一)
先贴一个beautifulsoup的官方文档,https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html#id12 requ ...
- java把带小数点的字符串转换成int类型
String number ="1.0000"; int num =Double.valueOf(number).intValue();//转换为Int类型
- 【PAT甲级】1104 Sum of Number Segments (20 分)
题意:输入一个正整数N(<=1e5),接着输入N个小于等于1.0的正数,输出N个数中所有序列的和. AAAAAccepted code: #define HAVE_STRUCT_TIMESPEC ...
- PHP基础学习笔记2
一.数组 1.1 数值数组 1)自动分配 ID 键 ) ); ?> 即二维数组: 100 100 96 20 60 59 40 110 100 上面创建的二维数组自动分配ID键:下面以指定键的方 ...
- SQL - 各种joins
- HTML连载59-子绝父相
一.子绝父相 1.只使用相对定位,对图片的位置进行精准定位. <!DOCTYPE html> <html lang="en"> <head> & ...
- Spring Boot 操作 Excel
Excel 在日常操作中经常使用到,Spring Boot 中使用 POI 操作 Excel 本项目源码 github 下载 1 新建 Spring Boot Maven 示例工程项目 注意:本示例是 ...