先考虑无解的情况:当n为奇数时无解

相邻的两个元素一定可以变成0

\[a[i] != a[i + 1]时, 分成[i, i], 和[i + 1, i + 1]
\]
\[a[i] = a[i + 1]时, 分成[i, i + 1]
\]

这两种情况对答案的贡献都是0,当n为奇数时我们总会有一个没办法凑成0.

#include <bits/stdc++.h>
#define ls p<<1
#define rs p<<1|1
#define PII pair<int, int>
#define ll long long
#define db double
#define ull unsigned long long
#define endl '\n' using namespace std;
const int N = 1e6 + 10;
int t, a[N]; void solve()
{
int res = 0, n;
cin >> n;
vector<PII>ans;
for(int i = 1; i <= n; ++ i) cin >> a[i];
if(n & 1 == 1)
{
cout << "-1" << endl;
return;
}
else
{
for(int i = 1; i <= n - 1; i += 2)
{
if(a[i] == a[i + 1]) ans.push_back({i, i + 1}), ++ res;
else
{
res += 2;
ans.push_back({i, i});
ans.push_back({i + 1, i + 1});
}
}
cout << res << endl;
for(auto x : ans) cout << x.first << ' ' << x.second << endl;
}
} int main()
{
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
// freopen("1.in", "r", stdin);
cin >> t;
while(t --)
solve();
return 0;
}

Codeforces Round 829 (Div. 1)A1. Make Nonzero Sum (easy version)(思维找规律)的更多相关文章

  1. Codeforces Round #540 (Div. 3) D1. Coffee and Coursework (Easy version) 【贪心】

    任意门:http://codeforces.com/contest/1118/problem/D1 D1. Coffee and Coursework (Easy version) time limi ...

  2. Codeforces Round #521 (Div. 3) F1. Pictures with Kittens (easy version)

    F1. Pictures with Kittens (easy version) 题目链接:https://codeforces.com/contest/1077/problem/F1 题意: 给出n ...

  3. Codeforces Round #575 (Div. 3) D1+D2. RGB Substring (easy version) D2. RGB Substring (hard version) (思维,枚举,前缀和)

    D1. RGB Substring (easy version) time limit per test2 seconds memory limit per test256 megabytes inp ...

  4. Codeforces Round #540 (Div. 3)--1118D1 - Coffee and Coursework (Easy version)

    https://codeforces.com/contest/1118/problem/D1 能做完的天数最大不超过n,因为假如每天一杯咖啡,每杯咖啡容量大于1 首先对容量进行从大到小的排序, sor ...

  5. Codeforces Round #568 (Div. 2) G1. Playlist for Polycarp (easy version) (状压dp)

    题目:http://codeforces.com/contest/1185/problem/G1 题意:给你n给选项,每个选项有个类型和价值,让你选择一个序列,价值和为m,要求连续的不能有两个相同的类 ...

  6. Codeforces Round #493 (Div. 2)D. Roman Digits 第一道打表找规律题目

    D. Roman Digits time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...

  7. Codeforces Round #579 (Div. 3) D2. Remove the Substring (hard version) (思维,贪心)

    题意:给你一个模式串\(t\),现在要在主串\(s\)中删除多个子串,使得得到的\(s\)的子序列依然包含\(t\),问能删除的最长子串长度. 题解:首先,我们不难想到,我们可以选择\(s\)头部到最 ...

  8. Codeforces Round #829 (Div. 2)/CodeForces1754

    CodeForces1754 注:所有代码均为场上所书 Technical Support 解析: 题目大意 给定一个只包含大写字母 \(\texttt{Q}\) 和 \(\texttt{A}\) 的 ...

  9. Codeforces Round #529 (Div. 3) E. Almost Regular Bracket Sequence (思维)

    Codeforces Round #529 (Div. 3) 题目传送门 题意: 给你由左右括号组成的字符串,问你有多少处括号翻转过来是合法的序列 思路: 这么考虑: 如果是左括号 1)整个序列左括号 ...

  10. Codeforces Round #829 (Div. 1/Div. 2) 1753 A B C D 题解

    Div1A / 2C. Make Nonzero Sum 令最后每个\(a_i\)的系数为\(c_i\)(\(c_i=1/-1\)),发现只要满足\(c_1=1\)(下标从1开始),且c中没有两个-1 ...

随机推荐

  1. Fabric网络升级(四)

    原文来自这里. 用户从v1.4.x升级到v2.x后,必须编辑通道配置来启用新的lifecycle功能.这个过程涉及到相关用户必须执行的一系列通道配置更新. 要启用新的chaincode lifecyc ...

  2. 强化学习基础篇[2]:SARSA、Q-learning算法简介、应用举例、优缺点分析

    强化学习基础篇[2]:SARSA.Q-learning算法简介.应用举例.优缺点分析 1.SARSA SARSA(State-Action-Reward-State-Action)是一个学习马尔可夫决 ...

  3. 【2】超级详细Python-matplotlib画图,手把手教你画图!(线条颜色、大小、线形、标签)

    相关文章: 全网最详细超长python学习笔记.14章节知识点很全面十分详细,快速入门,只用看这一篇你就学会了! [1]windows系统如何安装后缀是whl的python库 [2]超级详细Pytho ...

  4. 【一】LaTeX的安装和使用、安装TeXstudio、中文界面输出设置

    安装方法一:(推荐) 下载链接·:http://tug.org/texlive/acquire-netinstall.html 下载zip,然后运行Windows批处理脚本(install-tl-wi ...

  5. 【5】OpenCV2.4.9实现图像拼接与融合方法【SURF、SIFT、ORB、FAST、Harris角点 、stitch 】

    相关文章: [1]windows下安装OpenCV(4.3)+VS2017安装+opencv_contrib4.3.0配置 [2]Visual Studio 2017同时配置OpenCV2.4 以及O ...

  6. C/C++ 类与构造析构等知识

    简单定义类 #include <iostream> #include <string> using namespace std; class Student { public: ...

  7. 5个前端开源项目带你在Web上体验Windows

    本文来分享五个 yyds 的开源项目,这些项目把 Windows 带到了 Web 平台上.让我们一起感受这些项目带来的回忆和创新,重温 Windows 93.98.XP 和 7 的经典界面,甚至探索现 ...

  8. Qwik 1.0 发布,全栈式 Web 框架

    Qwik 是一个全栈式 Web 框架,Qwik 基于 React.Angular 和 Vue 等其他 Web 框架的概念,但以 JavaScript 流等更新的方法脱颖而出,允许以高性能向用户交付复杂 ...

  9. InnoDB存储引擎的行级锁

    InnoDB存储引擎的行级锁 InnoDB存储引擎和MyISAM的其中有两个很重要的区别:一个是事务,一个就是锁机制不同.事务之前有介绍,有问题的去补课;锁方面的不同是InnoDB引擎既有表锁又有行锁 ...

  10. 解决docker push镜像到私有仓库时的报错【http: server gave HTTP response to HTTPS client】

    一:解决docker push镜像到私有仓库时的报错[http: server gave HTTP response to HTTPS client]