CF1753A2.Make Nonzero Sum (hard version)

普及/提高-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

This is the hard version of the problem. The difference is that in this version the array contains zeros. You can make hacks only if both versions of the problem are solved.

You are given an array [a1,a2,…an][a_1, a_2, \ldots a_n] consisting of integers −1-1, 00 and 11. You have to build a partition of this array into the set of segments [l1,r1],[l2,r2],…,[lk,rk][l_1, r_1], [l_2, r_2], \ldots, [l_k, r_k] with the following property:

  • Denote the alternating sum of all elements of the ii-th segment as sis_i: sis_i = ali−ali+1+ali+2−ali+3+…±aria_{l_i} - a_{l_i+1} + a_{l_i+2} - a_{l_i+3} + \ldots \pm a_{r_i}. For example, the alternating sum of elements of segment [2,4][2, 4] in array [1,0,−1,1,1][1, 0, -1, 1, 1] equals to 0−(−1)+1=20 - (-1) + 1 = 2.
  • The sum of sis_i over all segments of partition should be equal to zero.

Note that each sis_i does not have to be equal to zero, this property is about sum of sis_i over all segments of partition.

The set of segments [l1,r1],[l2,r2],…,[lk,rk][l_1, r_1], [l_2, r_2], \ldots, [l_k, r_k] is called a partition of the array aa of length nn if 1=l1≤r1,l2≤r2,…,lk≤rk=n1 = l_1 \le r_1, l_2 \le r_2, \ldots, l_k \le r_k = n and ri+1=li+1r_i + 1 = l_{i+1} for all i=1,2,…k−1i = 1, 2, \ldots k-1. In other words, each element of the array must belong to exactly one segment.

You have to build a partition of the given array with properties described above or determine that such partition does not exist.

Note that it is not required to minimize the number of segments in the partition.

这是该问题的困难版本。区别在于,在此版本中,数组包含零。只有当该问题的两个版本均被解决时,你才可以进行 hack。

给定一个由整数 −1-1、00 和 11 组成的数组 [a1,a2,…,an][a_1, a_2, \ldots, a_n]。你需要将该数组划分为若干段 [l1,r1],[l2,r2],…,[lk,rk][l_1, r_1], [l_2, r_2], \ldots, [l_k, r_k],满足如下性质:

  • 记第 ii 段所有元素的交错和为 sis_i:si=ali−ali+1+ali+2−ali+3+…±aris_i = a_{l_i} - a_{l_i+1} + a_{l_i+2} - a_{l_i+3} + \ldots \pm a_{r_i}。例如,在数组 [1,0,−1,1,1][1, 0, -1, 1, 1] 中,段 [2,4][2, 4] 的交错和为 0−(−1)+1=20 - (-1) + 1 = 2。
  • 所有段的 sis_i 之和必须等于零。

注意:每个 sis_i 本身不必为零,此处要求的是所有段的 sis_i 的总和为零。

若满足 1=l1≤r1,  l2≤r2,  …,  lk≤rk=n1 = l_1 \le r_1,\; l_2 \le r_2,\; \ldots,\; l_k \le r_k = n,且对所有 i=1,2,…,k−1i = 1, 2, \ldots, k-1 均有 ri+1=li+1r_i + 1 = l_{i+1},则称集合 [l1,r1],[l2,r2],…,[lk,rk][l_1, r_1], [l_2, r_2], \ldots, [l_k, r_k] 为长度为 nn 的数组 aa 的一个划分。换言之,数组中的每个元素必须且仅属于一个段。

你需要为给定数组构造一个满足上述性质的划分,或判定这样的划分不存在。

注意:不要求最小化划分中段的数量。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤10 0001 \le t \le 10\,000). Description of the test cases follows.

The first line of each test case contains an integer nn (1≤n≤200 0001 \le n \le 200\,000) — the length of array aa.

The second line of each test case contains nn integers a1,a2,…,ana_1, a_2, \ldots, a_n (aia_i is −1-1, 00, or 11) — the elements of the given array.

It's guaranteed that the sum of nn over all test cases does not exceed 200 000200\,000.

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤10 0001 \le t \le 10\,000)。随后是各测试用例的描述。

每个测试用例的第一行包含一个整数 nn(1≤n≤200 0001 \le n \le 200\,000)—— 数组 aa 的长度。

每个测试用例的第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \ldots, a_n(每个 aia_i 为 −1-1、00 或 11)—— 给定数组的元素。

保证所有测试用例的 nn 之和不超过 200 000200\,000。

输出格式

For each test case print an integer kk — the number of segments in the partition. If required partition does not exist, print −1-1.

If partition exists, in the ii-th of the following kk lines print two integers lil_i and rir_i — description of the ii-th segment. The following conditions should be satisfied:

  • li≤ril_i \le r_i for each ii from 11 to kk.
  • li+1=ri+1l_{i + 1} = r_i + 1 for each ii from 11 to (k−1)(k - 1).
  • l1=1,rk=nl_1 = 1, r_k = n.

If there are multiple correct partitions of the array, print any of them.

对于每个测试用例,输出一个整数 kk —— 分割出的段的数量。如果所需的分割不存在,则输出 −1-1。

如果分割存在,则在接下来的 kk 行中,第 ii 行输出两个整数 lil_i 和 rir_i —— 描述第 ii 个段。需满足以下条件:

  • 对每个 ii(从 11 到 kk),有 li≤ril_i \le r_i;
  • 对每个 ii(从 11 到 k−1k - 1),有 li+1=ri+1l_{i + 1} = r_i + 1;
  • l1=1l_1 = 1,rk=nr_k = n。

若数组存在多种正确的分割方式,输出任意一种即可。

输入输出样例

  • 输入#1

    5
    4
    0 0 0 0
    7
    -1 1 0 1 0 1 0
    5
    0 -1 1 0 1
    3
    1 0 1
    1
    1

    输出#1

    4
    1 1
    2 2
    3 3
    4 4
    4
    1 1
    2 2
    3 5
    6 7
    -1
    2
    1 1
    2 3
    -1

说明/提示

In the first test case we can build a partition of 44 segments — each of them will contain only one element of the array equals to 00. So the sum will be equal to 0+0+0+0=00 + 0 + 0 + 0 = 0.

In the second test case we can build a partition of 44 segments. The alternating sum of the first segment will be equal to −1-1, the alternating sum of the second segment will be equal to 11, of the third segment — 0−1+0=−10 - 1 + 0 = -1, of the fourth segment — 1−0=11 - 0 = 1. The sum will be equal to −1+1−1+1=0-1 + 1 -1 + 1 = 0.

In the third test case it can be proved that the required partition does not exist.

在第一个测试用例中,我们可以构造一个包含 44 个区间的划分——每个区间仅包含数组中一个值为 00 的元素。因此总和为 0+0+0+0=00 + 0 + 0 + 0 = 0。

在第二个测试用例中,我们可以构造一个包含 44 个区间的划分。第一个区间的交错和为 −1-1,第二个区间的交错和为 11,第三个区间的交错和为 0−1+0=−10 - 1 + 0 = -1,第四个区间的交错和为 1−0=11 - 0 = 1。因此总和为 −1+1−1+1=0-1 + 1 -1 + 1 = 0。

在第三个测试用例中,可以证明所要求的划分不存在。

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

首页