首页 > 其他分享 >[Codeforces] CF1733C Parity Shuffle Sorting

[Codeforces] CF1733C Parity Shuffle Sorting

时间:2023-12-03 17:35:17浏览次数:23  
标签:Parity Sorting int Codeforces leq CF1733C Maxn ans 操作

题面翻译

给定一个长度为 \(n\) 的数组,你可以对它进行不超过 \(n\) 次操作。

对于每次操作:

  • 选择两个下标 \(l, r\),满足 \(1\leq l<r\leq n\);
  • 若 \(a_l + a_r\) 为奇数,将 \(a_r\) 赋值为 \(a_l\),否则将 \(a_l\) 赋值为 \(a_r\)。

求一种方案,使得操作后的数组单调不减(即 \(a_1\leq a_2\leq a_3 \leq \cdots\leq a_n\))。

思路

可以发现,只要一段数形如:

\(k,a_1,a_2,...a_n,k\),那么通过若干次操作,他一定能够变得全部相等

所以这道题只需要在一开始对第\(1\)和第\(n\)位进行一次操作,让着两个位置相等,那整个序列肯定也能变的相等了

代码

#include<bits/stdc++.h>
using namespace std;
const int Maxn=1e5+10;
int n;
int a[Maxn];
void run()
{
    vector<pair<int,int> >ans;
    cin>>n;
    for(int i=1;i<=n;i++) cin>>a[i];
    if((a[1]+a[n])%2==1) a[n]=a[1];
    else a[1]=a[n];
    if(n-1) ans.push_back(make_pair(1,n));
    for(int i=2;i<n;i++)
    {
        if(a[i]==a[1]) continue;
        else if((a[i]+a[1])%2==1) ans.push_back(make_pair(1,i));
        else ans.push_back(make_pair(i,n));
    }
    cout<<ans.size()<<endl;
    for(int i=0;i<ans.size();i++) cout<<ans[i].first<<" "<<ans[i].second<<endl;
}
int main()
{
    int t;
    cin>>t;
    while(t--) run();
    return 0;
}

标签:Parity,Sorting,int,Codeforces,leq,CF1733C,Maxn,ans,操作
From: https://www.cnblogs.com/lyk2010/p/17873458.html

相关文章

  • E. Permutation Sorting
    E.PermutationSortingYouaregivenapermutation$^\dagger$$a$ofsize$n$.Wecallanindex$i$goodif$a_i=i$issatisfied.Aftereachsecond,werotateallindicesthatarenotgoodtotherightbyoneposition.Formally,Let$s_1,s_2,\ldots,s_k$......
  • javascript: Sorting Algorithms
      /***fileSort.js*ide:vscodeJavaScriptSortingAlgorithms*插件:IntelliSense,JSDoc,CodeLens,DebuggerforChrome,静态代码检查:ESLint,JSHint,FlowLangugaeSupport,StandardJS-JavaScriptStandardStyle,koroFileHeader(文件头注释),测试插件:Mochasideba......
  • javascript: Sorting Algorithms
     /***fileSort.js*ide:vscodeJavaScriptSortingAlgorithms*插件:IntelliSense,JSDoc,CodeLens,DebuggerforChrome,静态代码检查:ESLint,JSHint,FlowLangugaeSupport,StandardJS-JavaScriptStandardStyle,koroFileHeader(文件头注释),测试插件:Mochasidebar,M......
  • [AGC037D] Sorting a Grid 题解
    学长给我看了这道题,感觉很有趣啊!想了想想出来了。考虑先把每个数还原到对应行上,然后用最后一次把它们斗出来。那么我们就是要在第一次操作后,对于每种颜色使得它平铺在这个块上。那么我们直接网络流或二分图匹配构造一下方案就做完力!......
  • Go - Sorting Arrays or Slices
    Problem: Youwanttosortelementsinanarrayorslice.Solution: Forint,float64,andstringarraysorslicesyoucanusesort.Ints,sort.Float64s,andsort.Strings.Youcanalsouseacustomcomparatorbyusingsort.Slice.Forstructs,youcan......
  • javascript: Sorting Algorithms
     //SortingAlgorithmsintJavaScripthttps://www.geeksforgeeks.org/sorting-algorithms//***fileSort.js*1.BubbleSort冒泡排序法*@paramarry*@paramnszie*/functionBubbleSort(arry,nszie){vari,j,temp;varswapped;for(i=0;i......
  • java: Sorting Algorithms
     /***encoding:utf-8*版权所有2023©涂聚文有限公司*许可信息查看:https://www.geeksforgeeks.org/sorting-algorithms/*描述:https://www.geeksforgeeks.org/sorting-algorithms/*#Author:geovindu,GeovinDu涂聚文.**#IDE:IntelliJID......
  • CSharp: Sorting Algorithms
     /*****************************************************************//***\fileSortingAlgorithm.cs*\briefcsharpSortingAlgorithms算法*IDEvs2022C#.net6.0*\authorgeovindu*\dateSeptember282023**************************......
  • cpp: Sorting Algorithms
     /*****************************************************************//***\fileSortingAlgorithms.h*\brief排序*\IDEvs2022C++20*\authorgeovindu*\dateSeptember282023********************************************************......
  • python: Sorting Algorithms
     #encoding:utf-8#版权所有2023涂聚文有限公司#许可信息查看:PythonSortingAlgorithms#描述:*https://www.programiz.com/dsa/counting-sort#*https://www.geeksforgeeks.org/sorting-algorithms/#Author:geovindu,GeovinDu涂聚文.#IDE:PyC......