반응형
https://www.acmicpc.net/problem/1517
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
|
package day0225;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.HashMap;
import java.util.StringTokenizer;
public class B1517 {
static long Answer;
static int arrcopy[];
static int arr[];
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
arr = new int[N];
arrcopy = new int[N];
StringTokenizer st = new StringTokenizer(br.readLine());
for (int i = 0; i < N; i++) {
arr[i] = Integer.parseInt(st.nextToken());
}
Answer = 0;
Merge(0, N - 1);
System.out.println(Answer);
}
public static void Merge(int left, int right) {
if (left != right) {
int mid = (left + right) / 2;
Merge(left, mid);
Merge(mid + 1, right);
MergeSort(left, right);
}
}
private static void MergeSort(int left, int right) {
int mid = (left + right) / 2;
int i = left;
int j = mid + 1;
int k = left;
while (i <= mid && j <= right) {
if (arr[i] > arr[j]) {
arrcopy[k++] = arr[j++];
Answer+= mid -i+1;
}
else {
arrcopy[k++] = arr[i++];
}
}
if (i > mid) {
while (j <= right) {
arrcopy[k++] = arr[j++];
}
} else {
while (i <= mid) {
arrcopy[k++] = arr[i++];
}
}
for (k = left; k <= right; k++) {
arr[k] = arrcopy[k];
}
}
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|
반응형
'ProgramSoliving' 카테고리의 다른 글
백준 : 109999 (JAVA) (0) | 2020.02.27 |
---|---|
백준 : 2042 ( 세그먼트 트리) (0) | 2020.02.27 |
백준 : 2617 (0) | 2020.02.26 |
백준 : 2251 (0) | 2020.02.25 |
백준 : 5719 (0) | 2020.02.25 |