1753A1 - Make Nonzero Sum (easy version)

news/2025/1/12 18:09:19/

题目链接

1753A1 - Make Nonzero Sum (easy version) Rating:1300

题目描述

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][a_1,a_2,…a_n][a1,a2,an] consisting of integers −1and 1. 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][l1,r1],[l2,r2],,[lk,rk] with the following property:

  • Denote the alternating sum of all elements of the i-th segment as si: si=ali−ali+1+ali+2−ali+3+…±arisi = ali−ali+1+ali+2−ali+3+…±arisi=aliali+1+ali+2ali+3+±ari. 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 si over all segments of partition should be equal to zero.

Note that each sis_isi 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][l1,r1],[l2,r2],…,[lk,rk][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=n1=l1≤r1,l2≤r2,…,lk≤rk=n1=l1r1,l2r2,,lkrk=n and ri+1=li+1ri+1=li+1ri+1=li+1 for all i=1,2,…k−1i=1,2,…k−1i=1,2,k1. 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 t(1≤t≤10000)t (1≤t≤10000)t(1t10000). Description of the test cases follows.

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

The second line of each test case contains n integers a1,a2,…,an(aiis−1or1)a1,a2,…,an (ai is −1 or 1)a1,a2,,an(aiis1or1) — the elements of the given array.

It’s guaranteed that the sum of n over all test cases does not exceed $200000.

Output

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 lilili and ririri— description of the i-th segment. The following conditions should be satisfied:

  • li≤rili≤riliri for each i from 1 to k.
  • li+1=ri+1li+1=ri+1li+1=ri+1 for each i from 1 to (k−1).
  • l1=1,rk=nl1=1,rk=nl1=1,rk=n
    .
    If there are multiple correct partitions of the array, print any of them.

inputCopy

4
4
1 1 1 1
6
-1 1 1 1 1 1
3
1 -1 1
1
1

outputCopy

1
1 4
2
1 3
4 6
-1
-1

大致题意:

每次给你一个长度为nnn ,只包含 1-1的数组 aaa

我们定义:区间和的计算方式为 a1∗1+a2∗(−1)+a3∗1+a4∗(−1)....a1 * 1 + a2 * (-1) + a3 * 1 + a4 * (-1)....a11+a2(1)+a31+a4(1)....

问的是能否将数组aaa,分成一个或多个连续的区间(左端点和右端点可以相等,例如:[1,1],[2,2][1,1],[2,2][1,1],[2,2]…这些都是合法区间),所有的区间和加起来sum=0sum = 0sum=0。如果可以,就输出区间的个数,以及是哪些区间(返回任意合法答案即可);否则输出 -1

分析:

如果整个数组之和 sumsumsum 是奇数的话肯定不会有答案,打印-1即可。因为即使将原数组拆分成多个区间,也不会影响奇偶性。反之,sumsumsum 为偶数的情况下,nnn 也为偶数,肯定有答案。

我们可以考虑 让拆分的每一个区间的和都为0。这样总的加起来也为0,满足要求。

我们可以将原数组拆分成两个数的区间 [a2i−1,a2i][a_{2i-1},a_{2i}][a2i1,a2i]

  • 如果 a2i−1==a2ia_{2i-1}==a_{2i}a2i1==a2i,那么就直接加入此区间 [a2i−1,a2i][a_{2i-1},a_{2i}][a2i1,a2i]
  • 否则加入两个区间,[a2i−1,a2i−1][a_{2i-1},a_{2i-1}][a2i1,a2i1][a2i,a2i][a_{2i},a_{2i}][a2i,a2i]

只有这样才能保证没一个区间和都是 0。

时间复杂度:O(n)O(n)O(n)

代码:

#include <iostream>
#include<algorithm>
#include<vector>
#include<cstring>using namespace std;
using PII = pair<int,int>;const int N = 2e5+10;
int a[N];void solve(){int n;scanf("%d",&n);int sum = 0;for(int i = 1;i <= n;i++){scanf("%d",&a[i]);sum += a[i];}if(sum % 2){puts("-1");return;}vector<PII> ans;for(int i = 1,j = 2;j <= n;i += 2,j += 2){if(a[i] == a[j]){ans.push_back({i,j});}else{ans.push_back({i,i});ans.push_back({j,j});}}cout<<ans.size()<<endl;for(auto &e:ans){printf("%d %d\n",e.first,e.second);}
}int main() {int t;cin>>t;while(t--){solve();}return 0;
} 

http://www.ppmy.cn/news/20671.html

相关文章

关于栈和队列

目录栈&#xff08;Stack&#xff09;什么是栈栈的使用栈的模拟实现队列&#xff08;Queue&#xff09;什么是队列队列的使用队列的模拟实现循环队列双端队列 (Deque)栈&#xff08;Stack&#xff09; 什么是栈 栈是一种特殊的线性表&#xff0c;它只允许在固定的一端进行插入…

Java SSM (springboot+mybatis)美食菜谱分享平台系统设计和实现以及论文报告

Java SSM (springbootmybatis)美食菜谱分享平台系统设计和实现以及论文报告 博主介绍&#xff1a;5年java开发经验&#xff0c;专注Java开发、定制、远程、文档编写指导等,csdn特邀作者、专注于Java技术领域 作者主页 超级帅帅吴 Java毕设项目精品实战案例《500套》 欢迎点赞 收…

什么蓝牙耳机便宜音质好?平价高音质蓝牙耳机推荐

随着蓝牙耳机的品类越来越多&#xff0c;人们在选择时有了更大的空间。作为蓝牙耳机选择的两大参考要素&#xff0c;性价比和音质的出现频率相对来说会比较高。那么&#xff0c;什么蓝牙耳机便宜音质好&#xff1f;下面&#xff0c;我来给大家推荐几款平价高音质的蓝牙耳机&…

19 | 三方协议怎么签?

前言 前言&#xff1a;简介三方协议签约的相关内容。 文章目录前言一. 什么是就业协议书二. 签约流程1. 网签流程&#xff08;线上签约&#xff09;三. 参考链接一. 什么是就业协议书 就业协议书俗称三方协议&#xff0c;是《全国普通高等学校毕业生就业协议书》的简称。 它是…

新来测试用一手Postman实现UI自动化测试拿下了大厂面试官

看到这篇文章的标题&#xff0c;是不是有小伙伴会感到惊讶呢&#xff1f; Postman不是做接口测试的吗&#xff1f;为什么还能做UI自动化测试呢&#xff1f; 其实&#xff0c;只要你了解Selenium的运行原理&#xff0c;就可以理解为什么Postman也能实现UI自动化测试了。 Sele…

JavaScriptArray和String对象~

初识Array&#xff1a; 定义&#xff1a; 方式1 var 变量名new Array(元素列表);举例&#xff1a; <script>var arraynew Array(1,2,3);alert(array); </script>显示如下&#xff1a; 方式2 var 变量名[元素列表];举例&#xff1a; <script>var array[…

51单片机学习笔记-3模块化编程

3 模块化编程 [toc] 注&#xff1a;笔记主要参考B站江科大自化协教学视频“51单片机入门教程-2020版 程序全程纯手打 从零开始入门”。 注&#xff1a;工程及代码文件放在了本人的Github仓库。 3.1 模块化编程 传统方式编程&#xff1a;所有的函数均放在main.c里&#xff0c…

IB课程为何号称全球最难国际课程?

在读国际学校的同学们&#xff0c;一定对大名鼎鼎的IB课程不陌生&#xff0c;可是他为什么被称作是它号称最难的国际课程呢&#xff1f;今天就来给大家全面解析一下IB课程&#xff5e; IB课程最开始是IBO为外交官子女开设全球统一标准的课程。IB课程为全球学生开设从幼儿园到大…