CF Round 821 (Div. 2)--C. Parity Shuffle Sorting

这篇具有很好参考价值的文章主要介绍了CF Round 821 (Div. 2)--C. Parity Shuffle Sorting。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

You are given an array a with n non-negative integers. You can apply the following operation on it.

  • Choose two indices l and r (1≤l<r≤n).
  • If al+ar is odd, do ar:=al. If al+ar is even, do al:=ar.

Find any sequence of at most n operations that makes a non-decreasing. It can be proven that it is always possible. Note that you do not have to minimize the number of operations.

An array a1,a2,…,an is non-decreasing if and only if a1≤a2≤…≤an.

Input

The first line contains one integer t (1≤t≤10^5) — the number of test cases.

Each test case consists of two lines. The first line of each test case contains one integer n (1≤n≤10^5) — the length of the array.

The second line of each test case contains n integers a1,a2,…,an (0≤ai≤10^9)  — the array itself.

It is guaranteed that the sum of n over all test cases doesn't exceed 10^5.

Output

For each test case, print one integer m (0≤m≤n), the number of operations, in the first line.

Then print m lines. Each line must contain two integers li,ri, which are the indices you chose in the i-th operation (1≤li<ri≤n).

If there are multiple solutions, print any of them.

input

3
2
7 8
5
1 1000000000 3 0 5
1
0

output

0
2
3 4
1 2
0

Note

In the second test case, a changes like this:

  • Select indices 3 and 4. a3+a4=3 is odd, so do a4:=a3. a=[1,1000000000,3,3,5] now.
  • Select indices 1 and 2. a1+a2=1000000001 is odd, so do a2:=a1. a=[1,1,3,3,5] now, and it is non-decreasing.

In the first and third test cases, a is already non-decreasing.

题意:给定长度为n的数组,在一次操作中,你可以选择 l,r 下标,如果al+ar是奇数,那么ar变成al,否则al变成ar,输出最多操作n次使得原数组单调不减的方案。

解析:我们发现可以从两头开始思考,第一步我们直接选1和n,那么操作之后a1和an就相等了,此时我们需要判断是a1变成an还是an变成a1,然后根据a1和an分两类,奇奇和偶偶,如果是奇奇,那么我们遍历2~n-1,如果它是偶数,那么可以直接选a1跟他匹配,否则就选an跟它匹配,然后就可以使得每个数都相等,操作n-1次,如果是偶偶,匹配反一下就行。

注意:因为规定L!=R,因此当n=1时,我们需要特判一下。文章来源地址https://www.toymoban.com/news/detail-512477.html

#include <bits/stdc++.h>
using namespace std;
const int N=1e5+5;
typedef pair<int,int> PII;
int a[N];
void solve()
{
    int n,op;
    scanf("%d",&n);
    vector<PII> ans;
    for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    if(n!=1) ans.push_back({1,n});//因为l,r需要不同,特判
    if((a[1]+a[n])%2)
    {
        if(a[1]%2) op=1;
        else op=2;
    }else
    {
        if(a[n]%2) op=1;
        else op=2;
    }
    if(op==1)//奇奇操作
    {
        for(int i=2;i<n;i++)
        {
            if(a[i]%2==0) ans.push_back({1,i});
            else ans.push_back({i,n});
        }
    }
    else//偶偶操作
    {
        for(int i=2;i<n;i++)
        {
            if(a[i]%2) ans.push_back({1,i});
            else ans.push_back({i,n});
        }
    }
    printf("%d\n",ans.size());
    for(int i=0;i<ans.size();i++) printf("%d %d\n",ans[i].first,ans[i].second);
}
int main()
{
    int t=1;
    scanf("%d",&t);
    while(t--) solve();
    return 0;
}

到了这里,关于CF Round 821 (Div. 2)--C. Parity Shuffle Sorting的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Codeforces Round 881 (Div. 3)

    给定一个数组,给每个元素涂色。求最大的代价。 代价为每个颜色的代价和。 每个颜色的代价为涂了该颜色的元素的极差。 因为是极差,每个元素要么对答案有正的贡献,要么有负的贡献,要么无贡献。且正负贡献的个数要相同。 因为要最大值,自然就是想有正贡献的是最

    2024年02月09日
    浏览(32)
  • Codeforces Round 871 (Div. 4)

    给定n个长度为10的字符串,问其与codeforces字符串的对应下标字母不同的个数。 对于每个字符串从前往后依次和“codeforces”对应字符比较然后统计不同字母数即可 给定长度为n的数组,问连续相邻为0的最大长度 维护一个全局最大长度res,和当前连续序列的长度cnt。从前往后扫

    2024年02月03日
    浏览(44)
  • Codeforces Round 867 (Div. 3)

    从所有a[i]+i-1=t的选择种取个max即可 实际上就是取同符号乘积的最大值 找规律,发现结果与边长n的关系是:res = n * (n + 3) - (n - 2) ①当n为奇数时,除了1其他均无解 ②当n为偶数时,我们可以构造一个形如n,1,n - 2,3,...的数列 首先我们可以发现n必定出现在起始位置。如果n不在起

    2024年02月02日
    浏览(37)
  • Codeforces Round 894 (Div. 3)

    签到题 a数组里,大于等于前一个值的a[i]会被写到b里。直接遍历b,如果b[i]比前一个小就在它前面插一个相等的值 计算反过来的长度,判断是否相等就行 对于没有重复口味的集合,能选出的方案是n*(n-1)/2 (从n个里面选2个的组合数)。二分找出需要多少不同口味的冰淇淋,

    2024年02月11日
    浏览(31)
  • Codeforces Round 882 (Div. 2)

    目录 A. The Man who became a God  题目分析: B. Hamon Odyssey 题目分析: C. Vampiric Powers, anyone? 题目分析:  n个人分成k组,每一组的力量都是 这样的,那么如果分成k组那么就会有k-1个力量不被统计,将力量总和减去前k-1个最大的力量就是最小的结果   将数组分组,每个组内进行按位与

    2024年02月05日
    浏览(38)
  • Codeforces Round 926 (Div. 2)

    类似于倍投法,就是在一赔一的情况下,第一次压一块钱,每输一次就押注上一次两倍的金额. 假如资金无限的话,这种方法赢的期望为无穷大.原理类似于二进制,不论你输再多次,只要赢一次总额就增加了1.比如 15 二进制1111,前3把一直输,但是只要第4把赢,就一定可以增加 1

    2024年02月20日
    浏览(34)
  • Codeforces Round 866 (Div. 2)

    给出一个仅由_或^组成的字符串,你可以在任意位置添加_或^字符,使得字符串满足: 任意字符要么属于^_^的一部分,要么属于^^的一部分。求最少添加的字符数量。 对于_我们只需处理没有组成^_^的_: ①如果_在首位置且左边没有^则添加^ ②如果_在尾位置且右边没有^则添加

    2023年04月25日
    浏览(40)
  • Codeforces Round 920 (Div. 3)

    Codeforces Round 920 (Div. 3) 题意:随机给出正方形在平面坐标系上的四个顶点的坐标,求正方形的面积,正方形边与xy轴平行。 思路:因为正方形与坐标轴平行,所以找出相同的两x或y坐标即可找到一条边的长度,平方就是面积,注意结果返回整型。 AC code: 题意:给出两个01字符

    2024年01月17日
    浏览(33)
  • Codeforces Round 868 Div 2

    要求构造一个仅包含 (1) 和 (-1) 的长度为 (n) 的数组 (a) ,使得存在 (k) 个下标对 ((i, j), i j) 满足 (a_i times a_j = 1) 。 当有 (x) 个 (1) , (y) 个 (-1) 时,其满足条件的下标对数量为 (frac{x (x - 1)}{2} + frac{y (y - 1)}{2}) 。 由于 (n) 只有 (100) ,直接枚举 (x) 即可。

    2024年02月01日
    浏览(32)
  • Codeforces Round 912 (Div. 2)

    大等于2依据冒泡排序即可排序,因此判断下1即可 对每一个数字找哪一位肯定为0填0其他的填1 从后往前考虑加到当前集合或新立一个集合 从最高位开始考虑能否为1,计算能否时每个数当前位为1

    2024年02月04日
    浏览(31)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包