-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumberofInversion.java
More file actions
73 lines (60 loc) · 2.25 KB
/
Copy pathNumberofInversion.java
File metadata and controls
73 lines (60 loc) · 2.25 KB
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
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.StringTokenizer;
public class NumberofInversion {
public static void main(String[] args)throws Exception {
int answer;
// System.setIn(new FileInputStream("res/sample.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
int[] a = new int[N];
StringTokenizer st = new StringTokenizer(br.readLine());
for(int i=0;st.hasMoreTokens();i++){
a[i] = Integer.parseInt(st.nextToken());
}
long t1 = System.currentTimeMillis();
answer = countingInversions(a);
long t2 = System.currentTimeMillis();
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
bw.write(answer+"");
bw.newLine();
bw.flush();
bw.close();
System.out.println("duration="+(t2-t1)+" milliseconds)");
}
private static int countingInversions(int[] a) {
int n = a.length;
int[] buf = new int[n];
int cnt = count(a,0,n-1,buf);
return cnt;
}
private static int count(int[] a, int s, int e, int[] buf){
if((e-s)<1) {
return 0;
}
int m = (s+e) / 2;
int leftCount = count(a,s,m,buf);
int rightCount = count(a,m+1,e,buf);
int mergeCount = merge(a,s,m,e,buf);
System.arraycopy(buf, s, a, s, (e-s)+1);
return leftCount + rightCount + mergeCount;
}
private static int merge(int[]a,int s, int m, int e, int[] buf){
//System.out.println("merge("+s+" "+e+")");
int left=s;
int right=m+1;
int count=0;
for(int k=s;k<=e;k++){
if(left<=m && ( (right>e) || (a[left] <= a[right]) ) ){
buf[k] = a[left++];
}else{
buf[k] = a[right++];
count = count + (m-left+1);
}
}
return count;
}
}