CSES - Putka Open 2020 – 3/5 - Results
Submission details
Task:Vaihdot
Sender:TapaniS
Submission time:2020-10-18 15:08:11 +0300
Language:Java
Status:READY
Result:0
Feedback
groupverdictscore
#10
#20
Test results
testverdicttimegroup
#10.50 s1, 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];
		int a = 0;
		int b = 0;

		for (int t1 = 0; t1 < t; t1++) {

			int n = input.nextInt();
			int[] elem = new int[n];
			int[] indOf = new int[n+1];

			for (int n1 = 0; n1 < n; n1++) {
				a = input.nextInt();
				elem[n1] = a;
				indOf[a] = n1;
			}

			ans[t1] = 0;

			if (n == 1) {
				continue;
			}

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

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

		// n > 3

			int swaps = 0;
			int ind1 = 0;
			int ind2 = 0;

			// parittomat ja parilliset kohdilleen
			for (int k = 0; k < n; k++) {
				ind1 = k;
				a = elem[ind1];
				if (((elem[k]+k) % 2) == 0) {
					for (int k2 = k+2; k2 < n; k2++) {
						ind2 = k2;
						b = elem[ind2];
						if (((a+b) % 2) == 1) {

							elem[ind1] = b;
							elem[ind2] = a;
							indOf[b] = ind1;
							indOf[a] = ind2;

							ans2[t1][swaps][0] = ind1+1;
							ans2[t1][swaps][1] = ind2+1;
							swaps++;
							ans[t1]++;
							break;
						}
					}
				}
			}

			// tarkista kaksi viimeista
			if (((elem[n-2]+n) % 2) == 0) {

				ind1 = n-4;
				a = elem[ind1];
				ind2 = n-2;
				b = elem[ind2];

				elem[ind1] = b;
				elem[ind2] = a;
				indOf[b] = ind1;
				indOf[a] = ind2;

				ans2[t1][swaps][0] = ind1+1;
				ans2[t1][swaps][1] = ind2+1;
				swaps++;
				ans[t1]++;


				ind1 = n-4;
				a = elem[ind1];
				ind2 = n-1;
				b = elem[ind2];

				elem[ind1] = b;
				elem[ind2] = a;
				indOf[b] = ind1;
				indOf[a] = ind2;

				ans2[t1][swaps][0] = ind1+1;
				ans2[t1][swaps][1] = ind2+1;
				swaps++;
				ans[t1]++;
			}

			// nyt vaihdot kohdilleen

			for (int k3 = 0; k3 < n; k3++) {

				if (elem[k3] != (k3+1)) {

					ind1 = k3;
					a = elem[ind1];
					ind2 = indOf[k3+1];
					b = elem[ind2];

					elem[ind1] = b;
					elem[ind2] = a;
					indOf[b] = ind1;
					indOf[a] = ind2;

					ans2[t1][swaps][0] = ind1+1;
					ans2[t1][swaps][1] = ind2+1;
					swaps++;
					ans[t1]++;
				}
			}  // vaihdot ok

		}  // t1


		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
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)

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