1691B - Shoe Shuffling - CodeForces Solution


constructive algorithms greedy implementation two pointers *1000

Please click on ads to support us..

Python Code:

def rotate(temp_ls):
	a = temp_ls.pop(0)
	temp_ls.append(a)
	return temp_ls

def print_func(temp_ls):
	for i in temp_ls:
		print(i, end = " ")
	print("")

for _ in range(int(input())):
	n = int(input())
	s = list(map(int,input().split()))
	ls=[]
	x=1
	out = []
	temp =[]

	for i in range(len(s)):
		if i == n-1:
			if s[i] == s[i-1]:
				temp.append(i+1)
			else:
				temp = [i+1]

			if len(temp) == 1:
				print("-1")
				x = 0
				break
				
			for j in rotate(temp):
				out.append(j)
		elif i == 0:
			temp =[i+1]
		else:
			if s[i] == s[i-1]:
				temp.append(i+1)
			else:
				if len(temp) == 1:
					print("-1")
					x = 0
					break
				
				for j in rotate(temp):
					out.append(j)
				temp = [i+1]

	if x:print_func(out)

C++ Code:

#include<bits/stdc++.h>
using namespace std;
#define ll long long int
int main()
{
 ios_base::sync_with_stdio(false);
cin.tie(NULL);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int t;
cin>>t;

while(t--)
{
ll n;
cin>>n;
ll a[n];
set<int> s;
map<int,int> m;
for(int i=0;i<n;i++){cin>>a[i]; m[(a[i])]++;}

 
int f=0;
for(auto x:m)
{
    if(x.second==1)
    {
        cout<<"-1"<<endl;
        f=1;
        break;
    }
}
if(!f)
{
    vector<int> v(n+1);
    for(int i=1;i<=n;i++)
    {
  v[i]=i;
    }
    for(int i=1;i<n;i++)
    {
        if(a[i-1]==a[i])
        {
            swap(v[i],v[i+1]);
        }
    }
    for(auto x:v)
    {
        if(x==0){continue;}
        cout<<x<<" ";
    }
    cout<<endl;  
}



}
 return 0;
}


Comments

Submit
0 Comments
More Questions

1384A - Common Prefixes
371A - K-Periodic Array
1542A - Odd Set
1567B - MEXor Mixup
669A - Little Artem and Presents
691B - s-palindrome
851A - Arpa and a research in Mexican wave
811A - Vladik and Courtesy
1006B - Polycarp's Practice
1422A - Fence
21D - Traveling Graph
1559B - Mocha and Red and Blue
1579C - Ticks
268B - Buttons
898A - Rounding
1372B - Omkar and Last Class of Math
1025D - Recovering BST
439A - Devu the Singer and Churu the Joker
1323A - Even Subset Sum Problem
1095A - Repeating Cipher
630F - Selection of Personnel
630K - Indivisibility
20B - Equation
600B - Queries about less or equal elements
1015A - Points in Segments
1593B - Make it Divisible by 25
680C - Bear and Prime 100
1300A - Non-zero
1475E - Advertising Agency
1345B - Card Constructions