CF1748C.Zero-Sum Prefixes

普及/提高-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

The score of an array v1,v2,…,vnv_1,v_2,\ldots,v_n is defined as the number of indices ii (1≤i≤n1 \le i \le n) such that v1+v2+…+vi=0v_1+v_2+\ldots+v_i = 0.

You are given an array a1,a2,…,ana_1,a_2,\ldots,a_n of length nn. You can perform the following operation multiple times:

  • select an index ii (1≤i≤n1 \le i \le n) such that ai=0a_i=0;
  • then replace aia_i by an arbitrary integer.

What is the maximum possible score of aa that can be obtained by performing a sequence of such operations?

数组 v1,v2,…,vnv_1,v_2,\ldots,v_n 的得分定义为满足 v1+v2+…+vi=0v_1+v_2+\ldots+v_i = 0 的下标 ii(其中 1≤i≤n1 \le i \le n)的个数。

给定一个长度为 nn 的数组 a1,a2,…,ana_1,a_2,\ldots,a_n。你可以执行以下操作任意多次:

  • 选择一个下标 ii(1≤i≤n1 \le i \le n),满足 ai=0a_i = 0;
  • 然后将 aia_i 替换为任意整数。

通过执行若干次上述操作,aa 数组所能达到的最大可能得分是多少?

输入格式

Each test contains multiple test cases. The first line contains a single integer tt (1≤t≤1041 \le t \le 10^4) — the number of test cases.

The first line of each test case contains one integer nn (1≤n≤2⋅1051 \le n \le 2 \cdot 10^5) — the length of the array aa.

The second line of each test case contains nn integers a1,a2,…,ana_1,a_2,\ldots,a_n (−109≤ai≤109-10^9 \le a_i \le 10^9) — array aa.

It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052 \cdot 10^5.

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

每个测试用例的第一行包含一个整数 nn(1≤n≤2⋅1051 \le n \le 2 \cdot 10^5)—— 数组 aa 的长度。

每个测试用例的第二行包含 nn 个整数 a1,a2,…,ana_1,a_2,\ldots,a_n(−109≤ai≤109-10^9 \le a_i \le 10^9)—— 数组 aa。

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

输出格式

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

对于每个测试用例,输出执行一系列操作后数组 aa 的最大可能得分。

输入输出样例

  • 输入#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 a2a_2 to −2-2 in one operation.

The resulting array aa will be [2,−2,1,−1,0][2,-2,1,-1,0], with a score of 33:

  • a1+a2=2−2=0a_1+a_2=2-2=0;
  • a1+a2+a3+a4=2−2+1−1=0a_1+a_2+a_3+a_4=2-2+1-1=0;
  • a1+a2+a3+a4+a5=2−2+1−1+0=0a_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 a3a_3 to −2 000 000 000-2\,000\,000\,000, giving us an array with a score of 11.

In the third test case, it is not necessary to perform any operations.

在第一个测试用例中,最优策略是通过一次操作将 a2a_2 的值修改为 −2-2。

得到的数组 aa 将为 [2,−2,1,−1,0][2,-2,1,-1,0],其得分为 33:

  • a1+a2=2−2=0a_1+a_2=2-2=0;
  • a1+a2+a3+a4=2−2+1−1=0a_1+a_2+a_3+a_4=2-2+1-1=0;
  • a1+a2+a3+a4+a5=2−2+1−1+0=0a_1+a_2+a_3+a_4+a_5=2-2+1-1+0=0。

在第二个测试用例中,最优策略是将 a3a_3 的值修改为 −2 000 000 000-2\,000\,000\,000,从而得到一个得分为 11 的数组。

在第三个测试用例中,无需执行任何操作。

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

首页