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 consisting of integers and . You have to build a partition of this array into the set of segments with the following property:
- Denote the alternating sum of all elements of the -th segment as : . For example, the alternating sum of elements of segment in array equals to .
- The sum of over all segments of partition should be equal to zero.
Note that each does not have to be equal to zero, this property is about sum of over all segments of partition.
The set of segments is called a partition of the array a of length if and for all . 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 . Description of the test cases follows.
The first line of each test case contains an integer — the length of the array a.
The second line of each test case contains integers ( is or ) — the elements of the given array.
It's guaranteed that the sum of over all test cases does not exceed .
Output
For each test case, if required partition does not exist, print . Otherwise, print an integer — the number of segments in the partition.
Then in the -th of the following lines print two integers and — description of the -th segment. The following conditions should be satisfied:
- for each from to .
- for each from to .
- .
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 . The sum of this segment will be equal to .
In the second test case we can build a partition of two segments of length . The sum of the first segment will be equal to , and the sum of the second segment: . So, the total sum will be equal to .
In the third and in the fourth test cases it can be proved that there are no required partition.
解题思路
如果序列的长度为奇数,那么一定无解。将序列的每一个数加起来得到总和,我们对任意一个数取反:
- 如果是,则取反得到,此时序列的总和变成,的奇偶性不变。
- 如果是,则取反得到,此时序列的总和变成,的奇偶性不变。
因此可以发现,对序列中任意的数取反,总和的奇偶性都不会发生改变。由于当序列长度为奇数时,任意的和相加得到的总和必定时奇数,因此无论怎么改变序列中的数都不会使得总和变偶数,因此总和无法变成,即无解。
那么当序列长度为偶数时就一定有解了吗?只要我们能构造出一种合理的划分方案就能说明有解。
由于此时的序列长度为偶数,因此我们可以先对相邻的两个元素分成一组,得到。考虑第组,如果,那么就将这组作为答案,此时这组的总和为。否则如果,那么就分成两组,分别是和,第一组的和为,第二组的和为,因此这两组加起来和就为。
可以发现按照上面这种规则对序列进行分组后元素总和必定为。
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
本文来自博客园,作者:onlyblues,转载请注明原文链接:https://www.cnblogs.com/onlyblues/p/16823446.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效