Codeforces Round #829 (Div. 2) C1. Make Nonzero Sum (easy version) 解题报告

原题链接:

Problem - C1 - Codeforces

题目描述:

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][a1,a2,…an] consisting of integers −1−1 and 11. 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] with the following property:

  • Denote the alternating sum of all elements of the ii-th segment as sisi: sisi = ali−ali+1+ali+2−ali+3+…±ariali−ali+1+ali+2−ali+3+…±ari. For example, the alternating sum of elements of segment [2,4][2,4] in array [1,0,−1,1,1][1,0,−1,1,1] equals to 0−(−1)+1=20−(−1)+1=2.
  • The sum of sisi over all segments of partition should be equal to zero.

Note that each sisi does not have to be equal to zero, this property is about sum of sisi over all segments of partition.

The set of segments [l1,r1],[l2,r2],…,[lk,rk][l1,r1],[l2,r2],…,[lk,rk] is called a partition of the array aa of length nn if 1=l1≤r1,l2≤r2,…,lk≤rk=n1=l1≤r1,l2≤r2,…,lk≤rk=n and ri+1=li+1ri+1=li+1 for all i=1,2,…k−1i=1,2,…k−1. 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 tt (1≤t≤100001≤t≤10000). Description of the test cases follows.

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

The second line of each test case contains nn integers a1,a2,…,ana1,a2,…,an (aiai is −1−1 or 11) — the elements of the given array.

It's guaranteed that the sum of nn over all test cases does not exceed 200000200000.

Output

For each test case, if required partition does not exist, print −1−1. Otherwise, print an integer kk — the number of segments in the partition.

Then in the ii-th of the following kk lines print two integers lili and riri — description of the ii-th segment. The following conditions should be satisfied:

  • li≤rili≤ri for each ii from 11 to kk.
  • li+1=ri+1li+1=ri+1 for each ii from 11 to (k−1)(k−1).
  • l1=1,rk=nl1=1,rk=n.

If there are multiple correct partitions of the array, print any of them.

题目大意:

C题的简单版本,给定一个长度为n的数组,数组元素只包含1和-1,我们可以把整个数组段分割为若干个连续子段,每个子段[l, r]的value为a[l]-a[l+1]+a[l+2]-a[l+3]……,以此类推,每个子段上的奇数位做加法,偶数位做减法,请问如何分割子段,可以使得各个子段的value之和为0,不比使得子段数量最小,如果有多种答案,可以输出任意一种,如果没有满足要求的答案,则输出-1。

解题思路:

如果n为奇数,则一定不可能有答案,直接可以输出-1,我们可以先把整个数组段的value计算出来,如果为0,则整个数组段即为一个答案。如果不是0,我们要考虑分割的每个子区间的左端点下标一定要是偶数(这里假定下标从1开始),因为如果每个端点都从奇数位开始的话,并不会对总的value值产生任何影响。具体细节看代码。

代码(CPP):

#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define int long long
#define PII pair<long long, long long>
typedef unsigned long long ull;
const int maxn = 2e5 + 10;
const int INF = 0x3fffffff;
int n, a[maxn];
vector<PII> seg;  // 存放区间端点

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cout << fixed;
    cout.precision(18);

    int t;
    cin >> t;
    while (t--)
    {
        seg.clear();
        cin >> n;
        int value = 0;
        for (int i = 1; i <= n; i++)
        {
            cin >> a[i];
            if(i & 1)
                value += a[i];
            else
                value -= a[i];
        }
        if(n & 1) // 如果数组长度为奇数,那么不可能满足题目要求
        {
            cout << -1 << endl;
            continue;
        }
        int l = 1;
        for (int i = 1; i <= n; i++)
        {
            if(value == 0)
                break;
            if(i % 2 == 0)
            {
                if(value < 0 && a[i] == 1)  // 如果此时的value小于0,并且偶数位的ai为1
                {
                    value += 2;
                    seg.push_back({l, i - 1});
                    seg.push_back({i, i});
                    l = i + 1;
                }
                if(value > 0 && a[i] == -1) // 如果此时的value大于0,并且偶数位的ai为-1
                {
                    value -= 2;
                    seg.push_back({l, i - 1});
                    seg.push_back({i, i});
                    l = i + 1;
                }
            }
        }
        if(l < n)
            seg.push_back({l, n});
        cout << seg.size() << endl;
        for (int i = 0; i < seg.size(); i++)
        {
            cout << seg[i].first << " " << seg[i].second << endl;
        }
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值