A15537 | Make Nonzero Sum (hard version)
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
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 $[a_1, a_2, \ldots a_n]$ consisting of integers $-1$ , $0$ 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:
- 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$ .
- The sum of $s_i$ over all segments of partition should be equal to zero.
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.
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.
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.
You are given an array $[a_1, a_2, \ldots a_n]$ consisting of integers $-1$ , $0$ 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:
- 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$ .
- The sum of $s_i$ over all segments of partition should be equal to zero.
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.
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.
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 \le t \le 10\,000$ ). Description of the test cases follows.
The first line of each test case contains an integer $n$ ( $1 \le n \le 200\,000$ ) — the length of array $a$ .
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ( $a_i$ 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 $200\,000$ .
The first line of each test case contains an integer $n$ ( $1 \le n \le 200\,000$ ) — the length of array $a$ .
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ( $a_i$ 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 $200\,000$ .
输出格式
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 $l_i$ and $r_i$ — description of the $i$ -th segment. The following conditions should be satisfied:
- $l_i \le r_i$ for each $i$ from $1$ to $k$ .
- $l_{i + 1} = r_i + 1$ for each $i$ from $1$ to $(k - 1)$ .
- $l_1 = 1, r_k = n$ .
If there are multiple correct partitions of the array, print any of them.
If partition exists, 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:
- $l_i \le r_i$ for each $i$ from $1$ to $k$ .
- $l_{i + 1} = r_i + 1$ for each $i$ from $1$ to $(k - 1)$ .
- $l_1 = 1, r_k = n$ .
If there are multiple correct partitions of the array, print any of them.
输入输出样例
输入 #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 $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.
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.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted