|
| 1 | +``` |
| 2 | +import java.io.*; |
| 3 | +import java.util.*; |
| 4 | +
|
| 5 | +public class Main { |
| 6 | + private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 7 | + private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 8 | + private static int[] bottom, top; |
| 9 | + private static int N, H; |
| 10 | +
|
| 11 | + public static void main(String[] args) throws IOException { |
| 12 | + init(); |
| 13 | + int count = 0; |
| 14 | + int answer = 20000001; |
| 15 | +
|
| 16 | + for (int i = 1; i <= H; i++) { |
| 17 | + int t = topBinarySearch(i); |
| 18 | + int b = bottomBinarySearch(i); |
| 19 | +
|
| 20 | + if (t+b < answer) { |
| 21 | + answer = t+b; |
| 22 | + count = 1; |
| 23 | + } else if (t+b == answer) { |
| 24 | + count++; |
| 25 | + } |
| 26 | + } |
| 27 | +
|
| 28 | + bw.write(answer + " " + count + "\n"); |
| 29 | + bw.flush(); |
| 30 | + bw.close(); |
| 31 | + br.close(); |
| 32 | + } |
| 33 | +
|
| 34 | + private static void init() throws IOException { |
| 35 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 36 | + N = Integer.parseInt(st.nextToken()); |
| 37 | + H = Integer.parseInt(st.nextToken()); |
| 38 | +
|
| 39 | + bottom = new int[N/2]; |
| 40 | + top = new int[N/2]; |
| 41 | +
|
| 42 | + for (int i = 0; i < N/2; i++) { |
| 43 | + bottom[i] = Integer.parseInt(br.readLine()); |
| 44 | + top[i] = Integer.parseInt(br.readLine()); |
| 45 | + } |
| 46 | +
|
| 47 | + Arrays.sort(bottom); |
| 48 | + Arrays.sort(top); |
| 49 | + } |
| 50 | +
|
| 51 | + private static int bottomBinarySearch(int h) { |
| 52 | + int left = 0; |
| 53 | + int right = N/2-1; |
| 54 | + int result = -1; |
| 55 | +
|
| 56 | + while (left <= right) { |
| 57 | + int mid = left+(right-left)/2; |
| 58 | +
|
| 59 | + if (bottom[mid] >= h) { |
| 60 | + result = mid; |
| 61 | + right = mid-1; |
| 62 | + } else { |
| 63 | + left = mid+1; |
| 64 | + } |
| 65 | + } |
| 66 | +
|
| 67 | + return result == -1 ? 0 : N/2-result; |
| 68 | + } |
| 69 | +
|
| 70 | + private static int topBinarySearch(int h) { |
| 71 | + int left = 0; |
| 72 | + int right = N/2-1; |
| 73 | + int result = -1; |
| 74 | +
|
| 75 | + while (left <= right) { |
| 76 | + int mid = left+(right-left)/2; |
| 77 | +
|
| 78 | + if (top[mid] >= H-h+1) { |
| 79 | + result = mid; |
| 80 | + right = mid-1; |
| 81 | + } else { |
| 82 | + left = mid+1; |
| 83 | + } |
| 84 | + } |
| 85 | +
|
| 86 | + return result == -1 ? 0 : N/2-result; |
| 87 | + } |
| 88 | +} |
| 89 | +``` |
0 commit comments