Codeforces Round 829 (Div. 1)A1. Make Nonzero Sum (easy version)(思维找规律)

发布时间 2023-11-29 20:05:48作者: cxy8

先考虑无解的情况:当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;
}