CF1300A.Non-zero

入门

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Guy-Manuel and Thomas have an array aa of nn integers [a1,a2,…,ana_1, a_2, \dots, a_n]. 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≤n1 \le i \le n) and do ai:=ai+1a_i := a_i + 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+a_1 + a_2 + …\dots +an≠0+ a_n \ne 0 and a1⋅a2⋅a_1 \cdot a_2 \cdot …\dots ⋅an≠0\cdot a_n \ne 0.

盖伊-曼努埃尔和托马斯有一个包含 nn 个整数的数组 aa:[a1,a2,…,an][a_1, a_2, \dots, a_n]。每一步中,他们可以将数组中任意一个元素加 11。形式化地说,每一步中他们可以选择任意下标 ii(满足 1≤i≤n1 \le i \le n),并执行操作 ai:=ai+1a_i := a_i + 1。

如果数组所有元素的和或积等于零,盖伊-曼努埃尔和托马斯并不介意再多执行一次该操作。

那么,他们至少需要多少步,才能使得数组所有元素的和与积均不为零?即:求最小操作步数,使得

a1+a2+⋯+an≠0且a1⋅a2⋅⋯⋅an≠0。a_1 + a_2 + \dots + a_n \ne 0 \quad \text{且} \quad a_1 \cdot a_2 \cdot \dots \cdot a_n \ne 0。

输入格式

Each test contains multiple test cases.

The first line contains the number of test cases tt (1≤t≤1031 \le t \le 10^3). The description of the test cases follows.

The first line of each test case contains an integer nn (1≤n≤1001 \le n \le 100) — the size of the array.

The second line of each test case contains nn integers a1,a2,…,ana_1, a_2, \dots, a_n (−100≤ai≤100-100 \le a_i \le 100) — elements of the array .

每个测试包含多个测试用例。

第一行包含测试用例的数量 tt(1≤t≤1031 \le t \le 10^3)。随后是测试用例的描述。

每个测试用例的第一行包含一个整数 nn(1≤n≤1001 \le n \le 100)—— 数组的大小。

每个测试用例的第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \dots, a_n(−100≤ai≤100-100 \le a_i \le 100)—— 数组的元素。

输出格式

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.

对于每个测试用例,输出使数组中所有元素的和与积均不为零所需的最少操作步数。

输入输出样例

  • 输入#1

    4
    3
    2 -1 -1
    4
    -1 0 0 1
    2
    -1 2
    3
    0 -2 1

    输出#1

    1
    2
    0
    2

说明/提示

In the first test case, the sum is 00. If we add 11 to the first element, the array will be [3,−1,−1][3,-1,-1], the sum will be equal to 11 and the product will be equal to 33.

In the second test case, both product and sum are 00. If we add 11 to the second and the third element, the array will be [−1,1,1,1][-1,1,1,1], the sum will be equal to 22 and the product will be equal to −1-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 11 twice to the first element the array will be [2,−2,1][2,-2,1], the sum will be 11 and the product will be −4-4.

在第一个测试用例中,和为 00。如果我们将 11 加到第一个元素上,则数组变为 [3,−1,−1][3,-1,-1],此时和为 11,积为 33。

在第二个测试用例中,积与和均为 00。如果我们将 11 分别加到第二个和第三个元素上,则数组变为 [−1,1,1,1][-1,1,1,1],此时和为 22,积为 −1-1。可以证明,无法用少于该次数的操作达成目标。

在第三个测试用例中,和与积均非零,因此无需进行任何操作。

在第四个测试用例中,将 11 两次加到第一个元素后,数组变为 [2,−2,1][2,-2,1],此时和为 11,积为 −4-4。

输入解题思路,AI测评打分。不知道怎么写?

首页