해결
코드
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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Inet4Address;
import java.util.*;
public class Main {
static int N,H;
static int[] ceil,floor;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
H = Integer.parseInt(st.nextToken());
ceil= new int[H+1];
floor = new int[H+1];
for (int i = 0; i < N/2 ; i++) { // 길이별로 카운트
floor[Integer.parseInt(br.readLine())]++;
ceil[Integer.parseInt(br.readLine())]++;
}
// 현재 길이보다 길다면 항상 충돌하므로 뒤에서부터 누적합
for (int i = H-1; i >0 ; i--) {
ceil[i] += ceil[i+1];
floor[i] += floor[i+1];
}
int[] stone = new int[H+1];
int min = N+1;
for (int i = 1; i <= H ; i++) { // 최솟값 탐색
stone[i] = floor[i] + ceil[H-i+1];
min = Math.min(min, stone[i]);
}
int cnt = 0;
for (int i = 1; i <= H ; i++) {
if(stone[i] == min)
cnt++;
}
System.out.println(min + " " + cnt);
}
}
참고
- 직접구현