CSES - Putka Open 2020 – 3/5 - Results
Submission details
Task:Vaihdot
Sender:hltk
Submission time:2020-11-27 14:42:26 +0200
Language:Java
Status:READY
Result:35
Feedback
groupverdictscore
#1ACCEPTED35
#20
Test results
testverdicttimegroup
#1ACCEPTED0.46 s1, 2details
#2--2details

Code

import java.util.*;
public class Vaihdot {
	public static int[][]ans=new int[500][2];
	public static int[]v=new int[100],pos=new int[100];
	public static int ansptr=0;
	public static void do_swap(int a,int b){
		v[a] ^= v[b];
		v[b] ^= v[a];
		v[a] ^= v[b];
		pos[v[a]] ^= pos[v[b]];
		pos[v[b]] ^= pos[v[a]];
		pos[v[a]] ^= pos[v[b]];
		ans[ansptr][0]=a;
		ans[ansptr][1]=b;
		ansptr++;
	}
	public static int abs(int x){return x<0?-x:x;}
	public static void main(String[]args){
		Scanner sc=new Scanner(System.in);
		int t=sc.nextInt();
		while(t-->0){
			ansptr=0;
			int n=sc.nextInt();
			for(int i=0;i<n;++i){
				v[i]=sc.nextInt();
				v[i]--;
				pos[v[i]]=i;
			}
			for(int i=0; i<n; ++i){
				if(abs(pos[i]-i)>1) do_swap(pos[i],i);
				for(int j=0;j<n&&pos[i] != i; ++j){
					if(abs(i-j)>1&&abs(pos[i]-j)>1){
						int u=pos[i];
						do_swap(u,j);
						do_swap(i,j);
						do_swap(u,j);
					}
				}
				for(int j=0; j<n && pos[i]!=i; ++j){
					if(abs(pos[i]-j)>1){
						for(int k=j+1; k<n && pos[i]!=i; ++k){
							if(abs(j-k)>1 && abs(i-k)>1){
								int u=pos[i];
								do_swap(j,u);
								do_swap(j,k);
								do_swap(i,k);
								do_swap(j,k);
								do_swap(j,u);
							}
						}
					}
				}
			}
			boolean ok=true;
			for(int i=0; i+1<n; ++i){
				if(v[i]>v[i+1]){
					ok=false;
				}
			}
			if(ok){
				System.out.println(ansptr);
				for(int i=0;i<ansptr;++i){
					System.out.println((ans[i][0]+1)+" "+(ans[i][1]+1));
				}
			}else{
				System.out.println(-1);
			}
		}
	}
}

Test details

Test 1

Group: 1, 2

Verdict: ACCEPTED

input
1000
1
1
2
1 2
...

correct output
0
0
-1
0
-1
...

user output
0
0
-1
0
-1
...
Truncated

Test 2

Group: 2

Verdict:

input
1000
79
49 42 77 41 37 61 46 55 7 72 4...

correct output
81
67 79
70 78
3 77
60 76
...

user output
(empty)