309887: CF1753A1. Make Nonzero Sum (easy version)

Memory Limit:256 MB Time Limit:2 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

Make Nonzero Sum (easy version)

题意翻译

本题目是[CF1753A2](https://www.luogu.com.cn/problem/CF1753A2)的简单版本,不同之处为困难(hard)版本中$a$数组包含$0$。 **题目大意** 给你一个数组 $[a_1,a_2,...a_n]$ ,其中每一项 $a_i$ 都为 $1$ 或 $-1$ ,你需要构造一个划分 $[l_1,r_1],[l_2,r_2],[l_3,r_3],...[l_k,r_k]$ 使得: - 将每一个区间内的数按照以下方法计算出$s_i=a_{l_i}-a_{l_i+1}+a_{l_i+2}-a_{l_i+3}+...\pm a_{r_i}$ - 对于一个合法的划分,所有的 $s_i$ 之和为 $0$ 如果存在这样的划分,输出任何一个,否则输出 `-1` ,代表无解。 称一组区间 $[l_1,r_1],[l_2,r_2],[l_3,r_3],...[l_k,r_k]$ 为数组 $[a_1,a_2,...a_n]$ 的划分当且仅当 $1=l_1\leq r_1,l_2\leq r_2,l_3\leq r_3,...,,l_k\leq r_k = n$ 且对于 $1\leq i \leq k-1$ ,均有 $r_i+1=l_{i+1}$ 注意在本题中,你不需要最小化 $k$。 **输入格式** 第一行输入一个数 $T$ ,代表测试组数。 对于每一组测试,先输入一个整数 $n$ ,再输入一个大小为 $n$ 的数组 $a$。 保证 $1 \leq t \leq 10000 , 1 \leq \sum n \leq 20000 , a_i \in \{-1,1\}$。 **输出格式** 对于每一组测试,如果无解,输出 `-1` 否则,先输出一个数 $k$ ,代表你需要的区间数量,接下来 $k$ 行,每行两个数 $l_i,r_i$ ,表示第 $i$ 个区间。

题目描述

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 $ [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: - 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 the array $ a $ . 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. It's guaranteed that the sum of $ n $ over all test cases does not exceed $ 200\,000 $ .

输出格式


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 $ 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

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.

Input

题意翻译

本题目是[CF1753A2](https://www.luogu.com.cn/problem/CF1753A2)的简单版本,不同之处为困难(hard)版本中$a$数组包含$0$。 **题目大意** 给你一个数组 $[a_1,a_2,...a_n]$ ,其中每一项 $a_i$ 都为 $1$ 或 $-1$ ,你需要构造一个划分 $[l_1,r_1],[l_2,r_2],[l_3,r_3],...[l_k,r_k]$ 使得: - 将每一个区间内的数按照以下方法计算出$s_i=a_{l_i}-a_{l_i+1}+a_{l_i+2}-a_{l_i+3}+...\pm a_{r_i}$ - 对于一个合法的划分,所有的 $s_i$ 之和为 $0$ 如果存在这样的划分,输出任何一个,否则输出 `-1` ,代表无解。 称一组区间 $[l_1,r_1],[l_2,r_2],[l_3,r_3],...[l_k,r_k]$ 为数组 $[a_1,a_2,...a_n]$ 的划分当且仅当 $1=l_1\leq r_1,l_2\leq r_2,l_3\leq r_3,...,,l_k\leq r_k = n$ 且对于 $1\leq i \leq k-1$ ,均有 $r_i+1=l_{i+1}$ 注意在本题中,你不需要最小化 $k$。 **输入格式** 第一行输入一个数 $T$ ,代表测试组数。 对于每一组测试,先输入一个整数 $n$ ,再输入一个大小为 $n$ 的数组 $a$。 保证 $1 \leq t \leq 10000 , 1 \leq \sum n \leq 20000 , a_i \in \{-1,1\}$。 **输出格式** 对于每一组测试,如果无解,输出 `-1` 否则,先输出一个数 $k$ ,代表你需要的区间数量,接下来 $k$ 行,每行两个数 $l_i,r_i$ ,表示第 $i$ 个区间。

加入题单

算法标签: