C1. Make Nonzero Sum (easy version)

C1. Make Nonzero Sum (easy version)

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=aliali+1+ali+2ali+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=l1r1,l2r2,,lkrk=n and ri+1=li+1 for all i=1,2,k1. 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.

Input

Each test contains multiple test cases. The first line contains the number of test cases t (1t10000). Description of the test cases follows.

The first line of each test case contains an integer n (1n200000) — 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.

Output

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:

  • liri for each i from 1 to k.
  • li+1=ri+1 for each i from 1 to (k1).
  • l1=1,rk=n.

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

Example

input

复制代码
4
4
1 1 1 1
6
-1 1 1 1 1 1
3
1 -1 1
1
1
复制代码

output

1
1 4
2
1 3
4 6
-1
-1

Note

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 11+11=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 11+1=1, and the sum of the second segment: 11+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.

 

解题思路

  如果序列的长度为奇数,那么一定无解。将序列的每一个数加起来得到总和s,我们对任意一个数取反:

  • 如果是1,则取反得到1,此时序列的总和变成s1+(1)=s2s的奇偶性不变。
  • 如果是1,则取反得到1,此时序列的总和变成s(1)+1=s+2s的奇偶性不变。

  因此可以发现,对序列中任意的数取反,总和s的奇偶性都不会发生改变。由于当序列长度为奇数时,任意的11相加得到的总和必定时奇数,因此无论怎么改变序列中的数都不会使得总和变偶数,因此总和无法变成0,即无解。

  那么当序列长度为偶数时就一定有解了吗?只要我们能构造出一种合理的划分方案就能说明有解。

  由于此时的序列长度为偶数,因此我们可以先对相邻的两个元素分成一组,得到(1,2),(3,4),(n1,n)。考虑第i(2i1,2i),如果a2i1=a2i,那么就将这组作为答案,此时这组的总和为a2i1a2i=0。否则如果a2i1a2i,那么就分成两组,分别是[2i1,2i1][2i,2i],第一组的和为a2i1,第二组的和为a2i,因此这两组加起来和就为a2i1+a2i=0

  可以发现按照上面这种规则对序列进行分组后元素总和必定为0

  AC代码如下:

复制代码
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 const int N = 2e5 + 10;
 5 
 6 int a[N];
 7 
 8 void solve() {
 9     int n;
10     scanf("%d", &n);
11     for (int i = 1; i <= n; i++) {
12         scanf("%d", a + i);
13     }
14     if (n & 1) {
15         printf("-1\n");
16         return;
17     }
18     vector<pair<int, int>> ans;
19     for (int i = 1; i <= n; i += 2) {
20         if (a[i] == a[i + 1]) ans.push_back({i, i + 1});
21         else ans.push_back({i, i}), ans.push_back({i + 1, i + 1});
22     }
23     printf("%d\n", ans.size());
24     for (auto &it : ans) {
25         printf("%d %d\n", it.first, it.second);
26     }
27 }
28 
29 int main() {
30     int t;
31     scanf("%d", &t);
32     while (t--) {
33         solve();
34     }
35     
36     return 0;
37 }
复制代码

 

参考资料

  Codeforces Round #829 Editorial:https://codeforces.com/blog/entry/108336

posted @   onlyblues  阅读(71)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效
Web Analytics
点击右上角即可分享
微信分享提示