CF1753A1.Make Nonzero Sum (easy version)
普及-
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
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.
You are given an array [a1,a2,…an] consisting of integers −1 and 1. You have to build a partition of this array into the set of segments [l1,r1],[l2,r2],…,[lk,rk] with the following property:
- Denote the alternating sum of all elements of the i-th segment as si: si = ali−ali+1+ali+2−ali+3+…±ari. For example, the alternating sum of elements of segment [2,4] in array [1,0,−1,1,1] equals to 0−(−1)+1=2.
- The sum of si over all segments of partition should be equal to zero.
Note that each si does not have to be equal to zero, this property is about sum of si over all segments of partition.
The set of segments [l1,r1],[l2,r2],…,[lk,rk] is called a partition of the array a of length n if 1=l1≤r1,l2≤r2,…,lk≤rk=n and ri+1=li+1 for all i=1,2,…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 组成的数组 [a1,a2,…,an]。你需要将该数组划分为若干段 [l1,r1],[l2,r2],…,[lk,rk],满足如下性质:
- 记第 i 段所有元素的交错和为 si:si=ali−ali+1+ali+2−ali+3+…±ari。例如,在数组 [1,0,−1,1,1] 中,段 [2,4] 的交错和为 0−(−1)+1=2。
- 所有段的 si 之和必须等于零。
注意:每个 si 本身不必为零;本题所要求的是所有 si 的总和为零。
若满足 1=l1≤r1,l2≤r2,…,lk≤rk=n,且对所有 i=1,2,…,k−1 均有 ri+1=li+1,则称集合 [l1,r1],[l2,r2],…,[lk,rk] 是长度为 n 的数组 a 的一个划分。换言之,数组中的每个元素必须恰好属于一个段。
你需要为给定数组构造一个满足上述性质的划分,或判断这样的划分不存在。
注意:不要求最小化划分中段的数量。
输入格式
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤10000). Description of the test cases follows.
The first line of each test case contains an integer n (1≤n≤200000) — the length of the array a.
The second line of each test case contains n integers a1,a2,…,an (ai is −1 or 1) — the elements of the given array.
It's guaranteed that the sum of n over all test cases does not exceed 200000.
每个测试包含多个测试用例。第一行包含测试用例的数量 t(1≤t≤10000)。随后是各测试用例的描述。
每个测试用例的第一行包含一个整数 n(1≤n≤200000)—— 数组 a 的长度。
每个测试用例的第二行包含 n 个整数 a1,a2,…,an(每个 ai 为 −1 或 1)—— 给定数组的元素。
保证所有测试用例的 n 之和不超过 200000。
输出格式
For each test case, if required partition does not exist, print −1. Otherwise, print an integer k — the number of segments in the partition.
Then in the i-th of the following k lines print two integers li and ri — description of the i-th segment. The following conditions should be satisfied:
- li≤ri for each i from 1 to k.
- li+1=ri+1 for each i from 1 to (k−1).
- l1=1,rk=n.
If there are multiple correct partitions of the array, print any of them.
对于每个测试用例,如果所要求的划分不存在,则输出 −1。否则,输出一个整数 k —— 划分中区段的数量。
随后,在接下来的 k 行中,第 i 行输出两个整数 li 和 ri —— 描述第 i 个区段。需满足以下条件:
- 对每个 i(从 1 到 k),有 li≤ri。
- 对每个 i(从 1 到 k−1),有 li+1=ri+1。
- l1=1,rk=n。
若数组存在多种合法划分,输出任意一种即可。
输入输出样例
输入#1
4 4 1 1 1 1 6 -1 1 1 1 1 1 3 1 -1 1 1 1
输出#1
1 1 4 2 1 3 4 6 -1 -1
说明/提示
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.
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.
In the third and in the fourth test cases it can be proved that there are no required partition.
在第一个测试用例中,我们可以构建一个长度为 4 的区段。该区段的和为 1−1+1−1=0。
在第二个测试用例中,我们可以构建两个长度均为 3 的区段。第一个区段的和为 −1−1+1=−1,第二个区段的和为 1−1+1=1。因此,总和为 −1+1=0。
在第三和第四个测试用例中,可以证明不存在满足要求的划分。
输入解题思路,AI测评打分。不知道怎么写?