1753A1.md (3268B)
1 Source: https://codeforces.com/problemset/problem/1753/A1 2 3 A1. Make Nonzero Sum (easy version) 4 5 time limit per test 6 7 2 seconds 8 9 memory limit per test 10 11 256 megabytes 12 13 input 14 15 standard input 16 17 output 18 19 standard output 20 21 This is the easy version of the problem. The difference is that in this version the array can not contain zeros. You can make hacks only if both versions of the problem are solved. 22 23 You are given an array $[a_1, a_2, \ldots a_n]$ consisting of integers $-1$ and $1$. You have to build a partition of this array into the set of segments $[l_1, r_1], [l_2, r_2], \ldots, [l_k, r_k]$ with the following property: 24 25 * Denote the alternating sum of all elements of the $i$-th segment as $s_i$: $s_i$ = $a_{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]$ in array $[1, 0, -1, 1, 1]$ equals to $0 - (-1) + 1 = 2$. 26 * The sum of $s_i$ over all segments of partition should be equal to zero. 27 28 Note that each $s_i$ does not have to be equal to zero, this property is about sum of $s_i$ over all segments of partition. 29 30 The set of segments $[l_1, r_1], [l_2, r_2], \ldots, [l_k, r_k]$ is called a partition of the array $a$ of length $n$ if $1 = l_1 \le r_1, l_2 \le r_2, \ldots, l_k \le r_k = n$ and $r_i + 1 = l_{i+1}$ for all $i = 1, 2, \ldots k-1$. In other words, each element of the array must belong to exactly one segment. 31 32 You have to build a partition of the given array with properties described above or determine that such partition does not exist. 33 34 Note that it is not required to minimize the number of segments in the partition. 35 36 Input 37 38 Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10\,000$). Description of the test cases follows. 39 40 The first line of each test case contains an integer $n$ ($1 \le n \le 200\,000$) — the length of the array $a$. 41 42 The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($a_i$ is $-1$ or $1$) — the elements of the given array. 43 44 It's guaranteed that the sum of $n$ over all test cases does not exceed $200\,000$. 45 46 Output 47 48 For each test case, if required partition does not exist, print $-1$. Otherwise, print an integer $k$ — the number of segments in the partition. 49 50 Then in the $i$-th of the following $k$ lines print two integers $l_i$ and $r_i$ — description of the $i$-th segment. The following conditions should be satisfied: 51 52 * $l_i \le r_i$ for each $i$ from $1$ to $k$. 53 * $l_{i + 1} = r_i + 1$ for each $i$ from $1$ to $(k - 1)$. 54 * $l_1 = 1, r_k = n$. 55 56 If there are multiple correct partitions of the array, print any of them. 57 58 Example 59 60 Input 61 62 ``` 63 4 64 65 4 66 67 1 1 1 1 68 69 6 70 71 -1 1 1 1 1 1 72 73 3 74 75 1 -1 1 76 77 1 78 79 1 80 ``` 81 82 Output 83 84 ``` 85 1 86 1 4 87 2 88 1 3 89 4 6 90 -1 91 -1 92 ``` 93 94 Note 95 96 In the first test case we can build a partition of one segment of length $4$. The sum of this segment will be equal to $1 - 1 + 1 - 1 = 0$. 97 98 In the second test case we can build a partition of two segments of length $3$. The sum of the first segment will be equal to $-1 -1 + 1 = -1$, and the sum of the second segment: $1 - 1 + 1 = 1$. So, the total sum will be equal to $-1 + 1 = 0$. 99 100 In the third and in the fourth test cases it can be proved that there are no required partition.