先考虑无解的情况:当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. 英伟达系列显卡大解析B100、H200、L40S、A100、A800、H100、H800、V100如何选择,含架构技术和性能对比带你解决疑惑

    英伟达系列显卡大解析B100.H200.L40S.A100.A800.H100.H800.V100如何选择,含架构技术和性能对比带你解决疑惑 近期,AIGC领域呈现出一片繁荣景象,其背后离不开强大算力 ...

  2. 分布式压测之locust和Jmeter的使用

    受限于单台机器的配置问题,我们在单台机器上达不到一个很高的压测并发数,那这个时候就需要引入分布式压测 分布式压测原理: 一般通过局域网把不同测试计算机链接到一起,达到测试共享.分散操作.集中管理的目的 ...

  3. SpringCloud-Gateway搭建保姆级教程

    一.网关介绍 1.什么是网关? 使⽤服务⽹关作为接⼝服务的统⼀代理,前端通过⽹关完成服务的统⼀调⽤ 2.⽹关可以⼲什么? 路由:接⼝服务的统⼀代理,实现前端对接⼝服务的统⼀访问 过滤:对⽤户请求进⾏拦 ...

  4. SESSION会话机制解析

    Windows Session(会话)的概念 会话 session 是由代表单个用户登录会话的所有进程和系统对象组成的.其中的对象包括所有的窗口,桌面和windows stations.桌面是特定se ...

  5. .NET Core开发实战(第25课:路由与终结点:如何规划好你的Web API)--学习笔记(下)

    25 | 路由与终结点:如何规划好你的Web API 自定义约束实现了路由约束接口,它只有一个 Match 方法,这个方法传入了 Http 当前的 httpContext,route,routeKey ...

  6. MySQL-CDC原理与实践

    MySQL CDC (Change Data Capture),中文名为MySQL变化数据捕获,是一种截取MySQL主从复制流中binlog的技术,从而实时捕获数据库中的增.删.改操作.在大数据.实时 ...

  7. Python Rich:美化终端显示效果

    Rich库的功能就像它的名字一样,使Python编程更加丰富(rich),它帮助开发者在控制台(命令行)输出中创建丰富.多彩和具有格式化的文本. 本篇总结了如何使用Rich库让我们的命令行工具更加美观 ...

  8. 从零开始的微信小程序入门教程(四),理解小程序事件与冒泡机制

    壹 ❀ 引 我在之前初识WXML与数据绑定两篇文章中,介绍了小程序静态模板与样式相关概念,以及小程序几种常用数据绑定方式,在知道这些知识后,我们可以写一些不算复杂的小程序页面,并能将一些自定义的数据渲 ...

  9. 计网学习笔记九 Routing Fundamentals

    在这一讲开始讲路由器的控制平面.简单介绍了routing,两个最小cost算法. 参考看的文章: VC网络中的路由 VC网络和数据报网络中路由的区别:Differences between Virtu ...

  10. AsyncHttpClient And Download Speed Limit

    AsyncHttpClient Official repository and docs: https://github.com/AsyncHttpClient/async-http-client M ...