4484: A2. Make Nonzero Sum (hard version)

内存限制:256 MB 时间限制:2 S 标准输入输出
题目类型:传统 评测方式:Special Judge 上传者:
提交:6 通过:1

题目描述

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] consisting of integers -1, 0 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:
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.

输入格式

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 array a.
The second line of each test case contains n integers a1, a2, ..., an (ai is -1, 0, or 1) — the elements of the given array.
It's guaranteed that the sum of n over all test cases does not exceed 200000.

输出格式

For each test case print an integer k — the number of segments in the partition. If required partition does not exist, print -1.
If partition exists, 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:
If there are multiple correct partitions of the array, print any of them.

输入样例 复制

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

输出样例 复制

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 4 segments — each of them will contain only one element of the array equals to 0. So the sum will be equal to 0 + 0 + 0 + 0 = 0.
In the second test case we can build a partition of 4 segments. The alternating sum of the first segment will be equal to -1, the alternating sum of the second segment will be equal to 1, of the third segment — 0 - 1 + 0 = -1, of the fourth segment — 1 - 0 = 1. The sum will be equal to -1 + 1 -1 + 1 = 0.
In the third test case it can be proved that the required partition does not exist.