-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlarrays-array.java
79 lines (71 loc) · 2.08 KB
/
larrays-array.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
static int[] aux;
public static long insertSort(int[] ar)
{
aux = new int[ar.length];
long count = mergeSort(ar, 0, ar.length-1);
return count;
}
public static long mergeSort(int[] ar, int from, int to) {
long count = 0;
if (from + 8 >= to) {
for (int i = from+1; i <= to; i++) {
int x = ar[i];
int j = i-1;
while (j >= from && ar[j] > x) {
ar[j+1] = ar[j];
count++;
j--;
}
ar[j+1] = x;
}
return count;
}
int middle = (from + to) / 2;
count += mergeSort(ar, from, middle);
count += mergeSort(ar, middle+1, to);
count += merge(ar, from, middle+1, to);
return count;
}
public static long merge(int[] ar, int from, int second, int to) {
long count = 0;
for (int i = from; i <= to; i++) {
aux[i] = ar[i];
}
int i = from;
int j = second;
int k = from;
while (k <= to) {
if (i >= second) ar[k] = aux[j++];
else if (j > to) ar[k] = aux[i++];
else if (aux[i] <= aux[j]) ar[k] = aux[i++];
else {
ar[k] = aux[j++];
count += (second-i);
}
k++;
}
return count;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int t = in.nextInt();
for(int i=0;i<t;i++){
int n = in.nextInt();
int[] ar = new int[n];
for(int j=0;j<n;j++){
ar[j]=in.nextInt();
//System.err.println(ar[j]);
}
long c = insertSort(ar);
String answer = "YES";
if (c%2 == 1) answer = "NO";
System.out.println(answer);
}
}
}