CSES - Putka Open 2020 – 3/5 - Results
Submission details
Task:Vaihdot
Sender:TapaniS
Submission time:2020-10-18 15:51:19 +0300
Language:Java
Status:READY
Result:35
Feedback
groupverdictscore
#1ACCEPTED35
#20
Test results
testverdicttimegroup
#1ACCEPTED0.52 s1, 2details
#2--2details

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][500][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;
					ans2[t1][0][0] = 1;
					ans2[t1][0][1] = 3;
				}
				continue;
			}

		// n > 3

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


			if (n == 4) {
				ans[t1] = 0;
				if (elem[1] != 2) {

					if (indOf[2] == 2) {
						a = elem[0];
						elem[0] = 2;
						elem[2] = a;
						indOf[2] = 0;
						indOf[a] = 2;

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

					if (indOf[2] == 0) {
						a = elem[3];
						elem[3] = 2;
						elem[0] = a;
						indOf[2] = 3;
						indOf[a] = 0;

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

					if (indOf[2] == 3) {
						a = elem[1];
						elem[1] = 2;
						elem[3] = a;
						indOf[2] = 1;
						indOf[a] = 3;

						ans2[t1][swaps][0] = 2;
						ans2[t1][swaps][1] = 4;
						swaps++;
						ans[t1]++;
					}

				}
				if (elem[2] != 3) {

					if (indOf[3] == 3) {
						a = elem[0];
						elem[0] = 3;
						elem[3] = a;
						indOf[3] = 0;
						indOf[a] = 3;

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

					if (indOf[3] == 0) {
						a = elem[2];
						elem[2] = 3;
						elem[0] = a;
						indOf[3] = 2;
						indOf[a] = 0;

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

				if (elem[0] != 1) {
					a = elem[0];
					elem[0] = 1;
					elem[3] = a;
					indOf[1] = 0;
					indOf[a] = 3;

					ans2[t1][swaps][0] = 1;
					ans2[t1][swaps][1] = 4;
					swaps++;
					ans[t1]++;
				}
				continue;
			}

		// n > 4

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