原题链接:

Problem - C1 - Codeforces

题目描述:

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][a1,a2,…an] consisting of integers −1−1 and 11. You have to build a partition of this array into the set of segments [l1,r1],[l2,r2],…,[lk,rk][l1,r1],[l2,r2],…,[lk,rk] with the following property:

  • Denote the alternating sum of all elements of the ii-th segment as sisi: sisi = ali−ali+1+ali+2−ali+3+…±ariali−ali+1+ali+2−ali+3+…±ari. For example, the alternating sum of elements of segment [2,4][2,4] in array [1,0,−1,1,1][1,0,−1,1,1] equals to 0−(−1)+1=20−(−1)+1=2.
  • The sum of sisi over all segments of partition should be equal to zero.

Note that each sisi does not have to be equal to zero, this property is about sum of sisi over all segments of partition.

The set of segments [l1,r1],[l2,r2],…,[lk,rk][l1,r1],[l2,r2],…,[lk,rk] is called a partition of the array aa of length nn if 1=l1≤r1,l2≤r2,…,lk≤rk=n1=l1≤r1,l2≤r2,…,lk≤rk=n and ri+1=li+1ri+1=li+1 for all i=1,2,…k−1i=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.

Input

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤100001≤t≤10000). Description of the test cases follows.

The first line of each test case contains an integer nn (1≤n≤2000001≤n≤200000) — the length of the array aa.

The second line of each test case contains nn integers a1,a2,…,ana1,a2,…,an (aiai is −1−1 or 11) — the elements of the given array.

It's guaranteed that the sum of nn over all test cases does not exceed 200000200000.

Output

For each test case, if required partition does not exist, print −1−1. Otherwise, print an integer kk — the number of segments in the partition.

Then in the ii-th of the following kk lines print two integers lili and riri — description of the ii-th segment. The following conditions should be satisfied:

  • li≤rili≤ri for each ii from 11 to kk.
  • li+1=ri+1li+1=ri+1 for each ii from 11 to (k−1)(k−1).
  • l1=1,rk=nl1=1,rk=n.

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

题目大意:

C题的简单版本,给定一个长度为n的数组,数组元素只包含1和-1,我们可以把整个数组段分割为若干个连续子段,每个子段[l, r]的value为a[l]-a[l+1]+a[l+2]-a[l+3]……,以此类推,每个子段上的奇数位做加法,偶数位做减法,请问如何分割子段,可以使得各个子段的value之和为0,不比使得子段数量最小,如果有多种答案,可以输出任意一种,如果没有满足要求的答案,则输出-1。

解题思路:

如果n为奇数,则一定不可能有答案,直接可以输出-1,我们可以先把整个数组段的value计算出来,如果为0,则整个数组段即为一个答案。如果不是0,我们要考虑分割的每个子区间的左端点下标一定要是偶数(这里假定下标从1开始),因为如果每个端点都从奇数位开始的话,并不会对总的value值产生任何影响。具体细节看代码。

代码(CPP):

#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define int long long
#define PII pair<long long, long long>
typedef unsigned long long ull;
const int maxn = 2e5 + 10;
const int INF = 0x3fffffff;
int n, a[maxn];
vector<PII> seg;  // 存放区间端点signed main()
{ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);cout << fixed;cout.precision(18);int t;cin >> t;while (t--){seg.clear();cin >> n;int value = 0;for (int i = 1; i <= n; i++){cin >> a[i];if(i & 1)value += a[i];elsevalue -= a[i];}if(n & 1) // 如果数组长度为奇数,那么不可能满足题目要求{cout << -1 << endl;continue;}int l = 1;for (int i = 1; i <= n; i++){if(value == 0)break;if(i % 2 == 0){if(value < 0 && a[i] == 1)  // 如果此时的value小于0,并且偶数位的ai为1{value += 2;seg.push_back({l, i - 1});seg.push_back({i, i});l = i + 1;}if(value > 0 && a[i] == -1) // 如果此时的value大于0,并且偶数位的ai为-1{value -= 2;seg.push_back({l, i - 1});seg.push_back({i, i});l = i + 1;}}}if(l < n)seg.push_back({l, n});cout << seg.size() << endl;for (int i = 0; i < seg.size(); i++){cout << seg[i].first << " " << seg[i].second << endl;}}return 0;
}

Codeforces Round #829 (Div. 2) C1. Make Nonzero Sum (easy version) 解题报告相关推荐

  1. C1. Make Nonzero Sum (easy version)【Codeforces Round #829 (Div. 2】

    Codeforces Round #829 (Div. 2)中C1题目 Codeforces比赛记录 文章目录 题目链接: 一.C1. Make Nonzero Sum (easy version) ...

  2. Codeforces Round #579 (Div. 3) F1. Complete the Projects (easy version) 排序 + 贪心

    传送门 文章目录 题意: 思路: 题意: 思路: 比较直观的想法就是对于bi≥0b_i\ge0bi​≥0的项目,我们将aia_iai​从小到大排序,让后依次加bib_ibi​,如果有取不到的,显然就无 ...

  3. Codeforces Round #829 (Div. 2) A~D

    比赛链接:Dashboard - Codeforces Round #829 (Div. 2) - Codeforces 目录 A. Technical Support B. Kevin and Pe ...

  4. Codeforces Round #829 (Div. 2)

    A. Technical Support         题目大意:                 每次一个问题Q,和一个回答A.要求每个问题Q后面都有A与之对应,问给出的字符串是不是满足这个条件 ...

  5. Codeforces Round #568 (Div. 2)C2. Exam in BerSU (hard version)

    Codeforces Round #568 (Div. 2)C2. Exam in BerSU (hard version) 贪心+暴力 大致题意:N个人考试,每个人花费的时间是a[i],他们总共花费 ...

  6. Codeforces Round #636 (Div. 3) D.Constant Palindrome Sum

    Codeforces Round #636 (Div. 3) D.Constant Palindrome Sum 题目链接 You are given an array a consisting of ...

  7. Codeforces Round #579 (Div. 3) F2. Complete the Projects (hard version) dp + 贪心

    传送门 文章目录 题意: 思路: 题意: 思路: 排序方式跟easyeasyeasy版本的一样,但是hardhardhard版本是输出最多能选多少,所以我们对b<0b<0b<0的情况 ...

  8. Codeforces Round #726 (Div. 2) E2. Erase and Extend (Hard Version) 贪心

    传送门 文章目录 题意: 思路: 题意: 给你一个长度为nnn的串sss,你有两个操作可以使用: (1)(1)(1)从sss的结尾删除一个字母. (2)s=s+s(2)s=s+s(2)s=s+s. 让 ...

  9. Codeforces Round #636 (Div. 3) D. Constant Palindrome Sum 思维 + 差分

    传送门 文章目录 题意: 思路: 题意: 思路: 首先有一个显然的性质就是每组操作最多不会超过两次. 很容易想到一个很暴力的思路,就是枚举x∈[1,2∗k]x \in [1,2*k]x∈[1,2∗k] ...

  10. Codeforces Round #828 (Div. 3) E1. Divisible Numbers (easy version) 解题报告

    原题链接: Problem - E1 - Codeforces 题目描述: This is an easy version of the problem. The only difference be ...

最新文章

  1. major头文件_JPEG头文件结构及组成
  2. 用eclipse创建WebService项目
  3. LoadRunner中的参数与变量
  4. linux运维高频命令汇总
  5. 神秘的40毫秒延迟与 TCP_NODELAY
  6. matlab 删除cell 中的 0 元素
  7. Maya Mesh Relaxation Deformer
  8. leapmotion 在unity中看不到手
  9. 用接纳的心看待新员工
  10. 网络流行简笔画图片大全,网络的简笔画图片
  11. 其实很简单 QQ被盗了可以这样找回来(转)
  12. AndroidStudio如何删除Modle
  13. jquery 立体走马灯_CSS3超酷3D文字跑马灯特效
  14. java短信接口_java调用短信接口代码
  15. Codeforces Round #613 (Div. 2)(B-D)
  16. matlab三点确定抛物线,[转]matlab编写的进退法,黄金分割法,抛物线法(二次插值法),wolfe不精确一维搜索...
  17. SRAM SROM DRAM DROM DDR NAND FLASH EMMC的区别
  18. Google map获取手机屏幕当前显示地图的范围
  19. Button控件的使用方法
  20. Cis-Glyoxal-Cyclam ,CAS: 74199-16-9,CIS-十氢-1H,6H-3A,5A,8A,10A-四氮杂芘

热门文章

  1. 互联网晚报 | 10月16日 星期六 | 搜狗正式并入腾讯;宏光MINIEV累计销量破40万台;神舟十三号载人飞船成功发射...
  2. opencms mysql_OpenCms for MySql 安装图解
  3. 电力拖动自动控制系统 华南理工大学期末重点 阮毅 长篇思维导图
  4. 4.3.2 信道编码 ——卷积码
  5. 在安装软件CAJViewer时出现,“错误1327。无效驱动器:F:
  6. matlab解超越函数,矩阵的超越函数Matlab提供的矩阵函数.PPT
  7. WebService原理浅析
  8. android uml建模工具 mac,UML建模工具Mac版
  9. WM8960的音量控制测试程序
  10. ipv6一致性测试(IPv6 Specification)内核修改