-
Notifications
You must be signed in to change notification settings - Fork 0
/
CODESPTB.java
79 lines (72 loc) · 1.92 KB
/
CODESPTB.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
/**
* Created by andrew on 9/19/15.
*/
import java.util.Scanner;
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int testCases = in.nextInt();
while (testCases > 0) {
int n = in.nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = in.nextInt();
}
ArrayAndCount t = new ArrayAndCount(a, 0);
t = divideAndCount(t);
a = t.array;
int count = t.count;
System.out.println(count);
testCases--;
}
in.close();
System.exit(0);
}
private static ArrayAndCount divideAndCount(ArrayAndCount t) {
int[] a = t.array;
int count = 0;
if (a.length == 1) {
return t;
}
int m = a.length/2;
ArrayAndCount lw = divideAndCount(new ArrayAndCount(Arrays.copyOfRange(a, 0, m), 0));
ArrayAndCount rw = divideAndCount(new ArrayAndCount(Arrays.copyOfRange(a, m, a.length), 0));
count += lw.count;
count += rw.count;
int[] l = lw.array;
int[] r = rw.array;
int[] f = new int[a.length];
int i = 0, j = 0, k = 0;
while(i < l.length && j < r.length) {
if (l[i] > r[j]) {
f[k] = r[j];
j++;
count += l.length - i;
} else {
f[k] = l[i];
i++;
}
k++;
}
while (i < l.length) {
f[k] = l[i];
i++;
k++;
}
while (j < r.length) {
f[k] = r[j];
j++;
k++;
}
return new ArrayAndCount(f, count);
}
}
class ArrayAndCount {
int[] array;
int count = 0;
public ArrayAndCount(int[] a, int c) {
array = a;
count = c;
}
}