CSES - Putka Open 2020 – 3/5 - Results
Submission details
Task:Vaihdot
Sender:TapaniS
Submission time:2020-10-17 18:45:09 +0300
Language:Java
Status:READY
Result:0
Feedback
groupverdictscore
#10
#20
Test results
testverdicttimegroup
#1--1, 2details
#20.14 s2details

Code

import java.util.*;


public class vaihdot {


public static int find(int[] a, int target)
{
	for (int i = 0; i < a.length; i++)
		if (a[i] == target)
			return i;

	return -1;
}


	public static void main(String[] args) {

		Scanner input = new Scanner(System.in);

		int t = input.nextInt();
		int[] ans = new int[t];
		int[][][] ans2 = new int[t][50][2];

		for (int i = 0; i < t; i++) {
			int n = input.nextInt();
			int[] elem = new int[n];
			int[] target = new int[n];

			for (int k = 0; k < n; k++) {
				elem[k] = input.nextInt();
				target[k] = k+1;
			}

			if (n == 1) {
				ans[i] = 0;
				continue;
			}

			if (n == 2) {
				ans[i] = 0;
				if (elem[0] == 2) ans[i] = -1;
				continue;
			}

			if (n == 3) {
				ans[i] = 0;
				if (elem[1] != 2) {ans[i] = -1;}
				else if (elem[0] == 3) {ans[i] = 1;}
				continue;
			}

			// n > 3
			int swaps = 0;
			int round = 0;
			int round2 = 0;
			int store = 0;
			int ind2 = 0;

			while (!Arrays.equals(elem, target) && (swaps < 50)) {

				for (int k = 0; k < n; k++) {
				  if (round < 5) {
					if (elem[k] != (k+1)) {
						// ind2 = Arrays.asList(elem).indexOf(k+1);
						// ind2 = ArrayUtils.indexOf(elem, k+1);
						ind2 = find(elem, k+1);
						if (Math.abs(ind2 - k) > 1) {
							store = elem[k];
							elem[k] = elem[ind2];
							elem[ind2] = store;
							ans2[i][swaps][0] = k+1;
							ans2[i][swaps][1] = ind2+1;
							swaps++;
						}
					}
				  }
				  else {
					if (elem[k] == (k) || elem[k] == (k+2)) {
						// ind2 = find(elem, k+1);
						if (Math.abs(n - k - 1) > 1) {
							store = elem[k];
							elem[k] = elem[n-1];
							elem[n-1] = store;
							ans2[i][swaps][0] = k+1;
							ans2[i][swaps][1] = n;
							swaps++;
							round2++;
						}
						if (Math.abs(k) > 1) {
							store = elem[k];
							elem[k] = elem[0];
							elem[0] = store;
							ans2[i][swaps][0] = k+1;
							ans2[i][swaps][1] = 1;
							swaps++;
							round2++;
						}
					}
					if (round2 > 2) {
						round = 0;
						round2 = 0;
					}
				  }

				}
				round++;


			}

			if (swaps > 49) {
				ans[i] = -1;
				continue;
			}

			ans[i] = swaps;

		}

		for (int i = 0; i < t; i++) {
			System.out.println(ans[i]);
			if (ans[i] > 0) {
				for (int k = 0; k < ans[i]; k++) {
					System.out.print(ans2[i][k][0] + " ");
					System.out.println(ans2[i][k][1]);
				}
			}

		}



		input.close(); 
    }
}

Test details

Test 1

Group: 1, 2

Verdict:

input
1000
1
1
2
1 2
...

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

user output
(empty)

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)

Error:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 50 out of bound...