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

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){
		int c=v[b];
		v[b]=v[a];
		v[a]=c;
		c=pos[v[b]];
		pos[v[b]]=pos[v[a]];
		pos[v[a]]=c;
		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();
		StringBuilder sb=new StringBuilder();
		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){
				sb.append(ansptr).append("\n");
				for(int i=0;i<ansptr;++i){
					sb.append(ans[i][0]+1).append(" ").append(ans[i][1]+1).append("\n");
				}
			}else{
				sb.append(-1).append("\n");
			}
		}
		System.out.print(sb.toString());
	}
}

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: ACCEPTED

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
83
58 1
45 2
16 3
29 4
...
Truncated