309867: CF1748C. Zero-Sum Prefixes

Memory Limit:256 MB Time Limit:1 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

Zero-Sum Prefixes

题意翻译

给定一个长为 $n$ 的数列 $a$,你可以将其中的每个 $0$ 分别改成任意整数。求出你最多能让多少个 $k$ 满足 $a_1+\dots+a_k=0$。

题目描述

The score of an array $ v_1,v_2,\ldots,v_n $ is defined as the number of indices $ i $ ( $ 1 \le i \le n $ ) such that $ v_1+v_2+\ldots+v_i = 0 $ . You are given an array $ a_1,a_2,\ldots,a_n $ of length $ n $ . You can perform the following operation multiple times: - select an index $ i $ ( $ 1 \le i \le n $ ) such that $ a_i=0 $ ; - then replace $ a_i $ by an arbitrary integer. What is the maximum possible score of $ a $ that can be obtained by performing a sequence of such operations?

输入输出格式

输入格式


Each test contains multiple test cases. The first line contains a single integer $ t $ ( $ 1 \le t \le 10^4 $ ) — the number of test cases. The first line of each test case contains one integer $ n $ ( $ 1 \le n \le 2 \cdot 10^5 $ ) — the length of the array $ a $ . The second line of each test case contains $ n $ integers $ a_1,a_2,\ldots,a_n $ ( $ -10^9 \le a_i \le 10^9 $ ) — array $ a $ . It is guaranteed that the sum of $ n $ over all test cases does not exceed $ 2 \cdot 10^5 $ .

输出格式


For each test case, print the maximum possible score of the array $ a $ after performing a sequence of operations.

输入输出样例

输入样例 #1

5
5
2 0 1 -1 0
3
1000000000 1000000000 0
4
0 0 0 0
8
3 0 2 -10 10 -30 30 0
9
1 0 0 1 -1 0 1 0 -1

输出样例 #1

3
1
4
4
5

说明

In the first test case, it is optimal to change the value of $ a_2 $ to $ -2 $ in one operation. The resulting array $ a $ will be $ [2,-2,1,-1,0] $ , with a score of $ 3 $ : - $ a_1+a_2=2-2=0 $ ; - $ a_1+a_2+a_3+a_4=2-2+1-1=0 $ ; - $ a_1+a_2+a_3+a_4+a_5=2-2+1-1+0=0 $ . In the second test case, it is optimal to change the value of $ a_3 $ to $ -2\,000\,000\,000 $ , giving us an array with a score of $ 1 $ . In the third test case, it is not necessary to perform any operations.

Input

题意翻译

给定一个长为 $n$ 的数列 $a$,你可以将其中的每个 $0$ 分别改成任意整数。求出你最多能让多少个 $k$ 满足 $a_1+\dots+a_k=0$。

Output

**题意翻译**

给定一个长度为 $ n $ 的数列 $ a $,你可以将每个 $ 0 $ 分别替换成任意整数。求出你最多能让多少个 $ k $ 满足 $ a_1+\dots+a_k=0 $。

**题目描述**

数列 $ v_1,v_2,\ldots,v_n $ 的分数定义为满足 $ v_1+v_2+\ldots+v_i = 0 $ 的下标 $ i $ 的数量。

给定一个长度为 $ n $ 的数列 $ a_1,a_2,\ldots,a_n $。你可以多次执行以下操作:

- 选择一个下标 $ i $($ 1 \le i \le n $)使得 $ a_i=0 $;
- 然后将 $ a_i $ 替换为任意整数。

通过执行一系列这样的操作,求 $ a $ 可以获得的最大分数。

**输入输出格式**

**输入格式**

每个测试包含多个测试用例。第一行包含一个整数 $ t $($ 1 \le t \le 10^4 $)——测试用例的数量。

每个测试用例的第一行包含一个整数 $ n $($ 1 \le n \le 2 \cdot 10^5 $)——数列 $ a $ 的长度。

每个测试用例的第二行包含 $ n $ 个整数 $ a_1,a_2,\ldots,a_n $($ -10^9 \le a_i \le 10^9 $)——数列 $ a $。

保证所有测试用例的 $ n $ 之和不超过 $ 2 \cdot 10^5 $。

**输出格式**

对于每个测试用例,输出执行一系列操作后数列 $ a $ 可以获得的最大分数。

**输入输出样例**

**输入样例 #1**

```
5
5
2 0 1 -1 0
3
1000000000 1000000000 0
4
0 0 0 0
8
3 0 2 -10 10 -30 30 0
9
1 0 0 1 -1 0 1 0 -1
```

**输出样例 #1**

```
3
1
4
4
5
```

**说明**

在第一个测试用例中,最优的操作是将 $ a_2 $ 的值变为 $ -2 $。

得到的数列 $ a $ 为 $ [2,-2,1,-1,0] $,分数为 $ 3 $:

- $ a_1+a_2=2-2=0 $;
- $ a_1+a_2+a_3+a_4=2-2+1-1=0 $;
- $ a_1+a_2+a_3+a_4+a_5=2-2+1-1+0=0 $。

在第二个测试用例中,最优的操作是将 $ a_3 $ 的值变为 $ -2,000,000,000 $,得到一个分数为 $ 1 $ 的数列。

在第三个测试用例中,不需要执行任何操作。**题意翻译** 给定一个长度为 $ n $ 的数列 $ a $,你可以将每个 $ 0 $ 分别替换成任意整数。求出你最多能让多少个 $ k $ 满足 $ a_1+\dots+a_k=0 $。 **题目描述** 数列 $ v_1,v_2,\ldots,v_n $ 的分数定义为满足 $ v_1+v_2+\ldots+v_i = 0 $ 的下标 $ i $ 的数量。 给定一个长度为 $ n $ 的数列 $ a_1,a_2,\ldots,a_n $。你可以多次执行以下操作: - 选择一个下标 $ i $($ 1 \le i \le n $)使得 $ a_i=0 $; - 然后将 $ a_i $ 替换为任意整数。 通过执行一系列这样的操作,求 $ a $ 可以获得的最大分数。 **输入输出格式** **输入格式** 每个测试包含多个测试用例。第一行包含一个整数 $ t $($ 1 \le t \le 10^4 $)——测试用例的数量。 每个测试用例的第一行包含一个整数 $ n $($ 1 \le n \le 2 \cdot 10^5 $)——数列 $ a $ 的长度。 每个测试用例的第二行包含 $ n $ 个整数 $ a_1,a_2,\ldots,a_n $($ -10^9 \le a_i \le 10^9 $)——数列 $ a $。 保证所有测试用例的 $ n $ 之和不超过 $ 2 \cdot 10^5 $。 **输出格式** 对于每个测试用例,输出执行一系列操作后数列 $ a $ 可以获得的最大分数。 **输入输出样例** **输入样例 #1** ``` 5 5 2 0 1 -1 0 3 1000000000 1000000000 0 4 0 0 0 0 8 3 0 2 -10 10 -30 30 0 9 1 0 0 1 -1 0 1 0 -1 ``` **输出样例 #1** ``` 3 1 4 4 5 ``` **说明** 在第一个测试用例中,最优的操作是将 $ a_2 $ 的值变为 $ -2 $。 得到的数列 $ a $ 为 $ [2,-2,1,-1,0] $,分数为 $ 3 $: - $ a_1+a_2=2-2=0 $; - $ a_1+a_2+a_3+a_4=2-2+1-1=0 $; - $ a_1+a_2+a_3+a_4+a_5=2-2+1-1+0=0 $。 在第二个测试用例中,最优的操作是将 $ a_3 $ 的值变为 $ -2,000,000,000 $,得到一个分数为 $ 1 $ 的数列。 在第三个测试用例中,不需要执行任何操作。

加入题单

上一题 下一题 算法标签: