CSES - Putka Open 2020 – 3/5 - Results
Submission details
Task:Vaihdot
Sender:hltk
Submission time:2020-11-27 14:39:02 +0200
Language:Java
Status:COMPILE ERROR

Compiler report

input/lol.java:2: error: class Main is public, should be declared in a file named Main.java
public class Main{
       ^
1 error

Code

import java.util.*;
public class Main{
	static int[][]ans=new int[500][2];
	static int[]v=new int[100],pos=new int[100];
	static int ansptr=0;
	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++;
	}
	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);
			}
		}
	}
}