1510K - King's Task - CodeForces Solution


brute force graphs implementation *1200

Please click on ads to support us..

Python Code:

def func1(a,n):

    
    for i in range(n):
        a[i],a[i+n]=a[i+n],a[i]

    return a

    
def func2(a,n):

    i=1
    for _ in range(n):
        a[i],a[i-1]=a[i-1],a[i]
        i+=2

    return a





n=int(input())
l=list(map(int,input().split()))
a=l[:]
temp=l[:]


mn=10**9
for j in range(2):
    ans=0
    a=l[:]
    while a[-1]!=2*n:

        if j:
            a=func2(a,n)

        else:
            a=func1(a,n)

        ans+=1
        j=not j

    if a==sorted(l):
        mn=min(mn,ans)


if mn==10**9:
    print(-1)

else:
    print(mn)
    
    
           


    

    


    

C++ Code:

// #include <bits/stdc++.h>
#include <iostream>
#include <cmath>
#include <iomanip>
#include <unordered_map>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <unordered_set>
#include <cstring>
#include <climits>
#include <algorithm>
using namespace std;
#define endl "\n"
#define int long long int
#define transform_to_upper(s)  transform(s.begin(), s.end(),s.begin(),::toupper);
#define transform_to_lower(s) transform(s.begin(), s.end(),s.begin(),::tolower);


void fun(vector<int> & v){
  int n=v.size();
  for(int i=0;i<n;i+=2)swap(v[i],v[i+1]);
    return ;
}


void fun2(vector<int> & v){
  int n=v.size();
  for(int i=0,j=n/2;j<n;i++,j++)swap(v[i],v[j]);

    return ;
}

bool check(vector<int>a,vector<int>  b){
  for(int i=0;i<a.size();i++){
    if(a[i]!=b[i])return false;
  }
  return true;
}



signed main(){
    int n;
    cin>>n;
    n*=2;

    vector<int> v(n);
    for(int & val : v)cin>>val;
   int ans1=-1,ans2=-1;

  vector<int> one=v,two=v;
  sort(v.begin(),v.end());

    int operations=0;
    while(operations<n){
      if(check(v,one))break;
          if(operations&1) fun(one);
          else fun2(one);
          operations++;
    }
   if(check(one,v)) ans1=operations;
    operations=0;


    while(operations<n){
      if(check(v,two))break;
          if(operations&1) fun2(two);
          else fun(two);
          operations++;
    }

   if(check(two,v)) ans2=operations;
   if(ans1==-1||ans2==-1)cout<<max(ans1,ans2)<<endl;
   else cout<<min(ans1,ans2)<<endl;
  



}


Comments

Submit
0 Comments
More Questions

1472A - Cards for Friends
315A - Sereja and Bottles
1697C - awoo's Favorite Problem
165A - Supercentral Point
1493A - Anti-knapsack
1493B - Planet Lapituletti
747B - Mammoth's Genome Decoding
1591C - Minimize Distance
1182B - Plus from Picture
1674B - Dictionary
1426C - Increase and Copy
520C - DNA Alignment
767A - Snacktower
1365A - Matrix Game
714B - Filya and Homework
31A - Worms Evolution
1691A - Beat The Odds
433B - Kuriyama Mirai's Stones
892A - Greed
32A - Reconnaissance
1236D - Alice and the Doll
1207B - Square Filling
1676D - X-Sum
1679A - AvtoBus
1549A - Gregor and Cryptography
918C - The Monster
4B - Before an Exam
545B - Equidistant String
1244C - The Football Season
1696B - NIT Destroys the Universe