-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBOJ24060.java
More file actions
64 lines (57 loc) · 1.49 KB
/
BOJ24060.java
File metadata and controls
64 lines (57 loc) · 1.49 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
package ¹éÁØ;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class BOJ24060 {
static int[] tmp;
static int N, K;
static int cnt = 0;
static int result = -1;
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
K = Integer.parseInt(st.nextToken());
int[] arr = new int[N];
tmp = new int[N];
st = new StringTokenizer(br.readLine());
for(int i = 0; i < N; i++) {
arr[i] = Integer.parseInt(st.nextToken());
}
mergeSort(arr, 0, N-1);
System.out.print(result);
}
public static void mergeSort(int[] arr, int start, int end) {
if(cnt > K) return;
if(start < end) {
int mid = (start + end) / 2;
mergeSort(arr, start, mid);
mergeSort(arr, mid + 1, end);
merge(arr, start, mid, end);
}
}
public static void merge(int[] arr, int start, int mid, int end) {
int i = start, j = mid + 1, t = 0;
while(i <= mid && j <= end) {
if(arr[i] <= arr[j]) {
tmp[t++] = arr[i++];
}
else {
tmp[t++] = arr[j++];
}
}
while (i <= mid) tmp[t++] = arr[i++];
while (j <= end) tmp[t++] = arr[j++];
i = start;
t = 0;
while (i <= end) {
cnt++;
if(cnt == K) {
result = tmp[t];
break;
}
arr[i++] = tmp[t++];
}
}
}