| Task: | Vaihdot |
| Sender: | TapaniS |
| Submission time: | 2020-10-17 18:37:26 +0300 |
| Language: | Java |
| Status: | READY |
| Result: | 0 |
| group | verdict | score |
|---|---|---|
| #1 | TIME LIMIT EXCEEDED | 0 |
| #2 | TIME LIMIT EXCEEDED | 0 |
| test | verdict | time | group | |
|---|---|---|---|---|
| #1 | TIME LIMIT EXCEEDED | -- | 1, 2 | details |
| #2 | RUNTIME ERROR | 0.14 s | 2 | details |
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[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;
System.out.println(ans[i]);
continue;
}
if (n == 2) {
ans[i] = 0;
if (elem[0] == 2) ans[i] = -1;
System.out.println(ans[i]);
continue;
}
if (n == 3) {
ans[i] = 0;
if (elem[1] != 2) {ans[i] = -1;}
else if (elem[0] == 3) {ans[i] = 1;}
System.out.println(ans[i]);
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[swaps][0] = k+1;
ans2[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[swaps][0] = k+1;
ans2[swaps][1] = n;
swaps++;
round2++;
}
if (Math.abs(k) > 1) {
store = elem[k];
elem[k] = elem[0];
elem[0] = store;
ans2[swaps][0] = k+1;
ans2[swaps][1] = 1;
swaps++;
round2++;
}
}
if (round2 > 2) {
round = 0;
round2 = 0;
}
}
}
round++;
}
if (swaps > 49) {
ans[i] = -1;
System.out.println(ans[i]);
continue;
}
ans[i] = swaps;
System.out.println(ans[i]);
for (int k2 = 0; k2 < swaps; k2++) {
System.out.print(ans2[k2][0] + " ");
System.out.println(ans2[k2][1]);
}
}
input.close();
}
}Test details
Test 1
Group: 1, 2
Verdict: TIME LIMIT EXCEEDED
| input |
|---|
| 1000 1 1 2 1 2 ... |
| correct output |
|---|
| 0 0 -1 0 -1 ... |
| user output |
|---|
| (empty) |
Test 2
Group: 2
Verdict: RUNTIME ERROR
| 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...
