forked from Algorithm-with-SSAFY/Algorithm-Study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBOJ_3020.java
More file actions
64 lines (43 loc) · 1.34 KB
/
BOJ_3020.java
File metadata and controls
64 lines (43 loc) · 1.34 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
import java.io.*;
import java.util.*;
public class Main {
static int n, h, down[], up[];
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());
h = Integer.parseInt(st.nextToken());
int[] down = new int[n/2];
int[] up = new int[n/2];
for (int i = 0; i < n / 2; i++) {
down[i] = Integer.parseInt(br.readLine());
up[i] = Integer.parseInt(br.readLine());
}
Arrays.sort(down);
Arrays.sort(up);
int min = n; //장애물의 최소 개수
int cnt = 0;
for (int i = 1; i <= h; i++) {
int conf = binarySearch(0, n/2, i, down) + binarySearch(0, n/2, h-i+1, up);//높이에서 i를 빼주
if (min == conf) {
cnt++;
} else if (min > conf) {
min = conf;
cnt = 1;
}
}
System.out.println(min + " " + cnt);
}
static int binarySearch(int left, int right, int h, int[] arr) {
int mid;
if (left >= right) {
return arr.length - right; //idx가 0부터 시작하는 것을 생각1
}
mid = (left + right) / 2;
if (arr[mid] >= h) { //가운데의 높이가 같거나 더 크면
return binarySearch(left, mid, h, arr);
} else {
return binarySearch(mid + 1, right, h, arr);
}
}
}