Make Nonzero Sum (easy version)
问题 给你一个只有1, -1的序列,序列长度为n。如何划分区间,使得所有区间和为0;可以的话,输出划分为几个区间,以及左右端点;不行的话输出-1.
区间和计算公式:
思路
两个数运算结果一定为偶数,所以只有n为偶数的时候,才可以; 在这基础之上,我们以两个数字为区间,相同则放在一个区间内他们的和为0,否则分别以一个数字作为一个区间,
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
#define long long long
#define fspr(n) fixed << setprecision(n)
#define spr(n) setprecision(n)
#define sci setiosflags(ios::scientific)
#define siosf setiosflags
#define endl '\n'
#define ifor(i, l, r) for (long (i) = (l); (i) <= (r); ++(i))
#define rfor(i, r, l) for (long (i) = (r); (i) >= (l); --(i))
set<long> sav;
int a[1000000];
void solve() {
int n;
cin >> n;
ifor(i, 1, n ) scanf("%d", &a[i]);
if ( n & 1 ) puts("-1");
else {
vector<pair<int,int>> vt;
for ( int i = 1; i <= n; i += 2 ) {
if ( a[i] == a[i + 1] ) vt.push_back({ i,i + 1 });
else vt.push_back({ i,i }), vt.push_back({ i + 1,i + 1 });
}
printf("%d\n", vt.size());
for ( auto i : vt ) printf("%d %d\n", i.first, i.second);
}
}
int main(int argv, char** argc) {
/*ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);*/
/*string a;
while ( cin >> a )
{
a.insert(a.begin(), 'b');
a.append("a");
long sum = 0;
bool f = 0;
ifor(i, 0, a.size() - 1)
{
if ( f && a[i] - '0'==0 ) {
a.erase(i,1);
i--;
continue;
}
if ( a[i] - '0' == 0 )
{
f = 1;
}
else
f = 0;
}
ifor(i, 0, a.size() - 1)
{
if ( isdigit(a[i]) )
{
if ( isalpha(a[i - 1]) && a[i]-'0'==0 )
{
if ( i + 1 < a.size() )
{
if ( isalpha(a[i + 1]) )
sav.emplace(0);
}
else
sav.emplace(0);
}
sum = sum * 10 + a[i] - '0';
}
else
{
if ( sum != 0 ) sav.emplace(sum);
sum = 0;
}
}
for ( auto i = sav.begin(); i != sav.end(); i++ )
{
if ( i == begin(sav) )cout << *sav.begin();
else cout << " " << *i;
}
cout << endl;
sav.clear();
}*/
int k;
cin >> k;
while ( k-- )
{
solve();
}
return 0;
}