|
| 1 | +``` |
| 2 | +import java.io.*; |
| 3 | +import java.util.*; |
| 4 | +
|
| 5 | +public class Main { |
| 6 | + private static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 7 | + private static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 8 | + private static int[] uf, cards; |
| 9 | + private static int N, M, K; |
| 10 | + public static void main(String[] args) throws IOException { |
| 11 | + init(); |
| 12 | +
|
| 13 | + bw.flush(); |
| 14 | + bw.close(); |
| 15 | + br.close(); |
| 16 | + } |
| 17 | +
|
| 18 | + private static void init() throws IOException { |
| 19 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 20 | + N = Integer.parseInt(st.nextToken()); |
| 21 | + M = Integer.parseInt(st.nextToken()); |
| 22 | + K = Integer.parseInt(st.nextToken()); |
| 23 | +
|
| 24 | + cards = new int[M]; |
| 25 | + uf = new int[M]; |
| 26 | +
|
| 27 | + st = new StringTokenizer(br.readLine()); |
| 28 | +
|
| 29 | + for (int i = 0; i < M; i++) { |
| 30 | + cards[i] = Integer.parseInt(st.nextToken()); |
| 31 | + uf[i] = i; |
| 32 | + } |
| 33 | +
|
| 34 | + Arrays.sort(cards); |
| 35 | +
|
| 36 | + st = new StringTokenizer(br.readLine()); |
| 37 | + for (int i = 0; i < K; i++) { |
| 38 | + int input = Integer.parseInt(st.nextToken()); |
| 39 | + int temp = binarySearch(input); |
| 40 | + int index = find(temp); |
| 41 | +
|
| 42 | + bw.write(cards[index] + "\n"); |
| 43 | + uf[index] = index + 1; |
| 44 | + } |
| 45 | + } |
| 46 | +
|
| 47 | + private static int binarySearch(int target) { |
| 48 | + int left = 0; |
| 49 | + int right = M-1; |
| 50 | +
|
| 51 | + while (left < right) { |
| 52 | + int mid = left + (right - left) / 2; |
| 53 | +
|
| 54 | + if (target < cards[mid]) { |
| 55 | + right = mid; |
| 56 | + } else { |
| 57 | + left = mid + 1; |
| 58 | + } |
| 59 | + } |
| 60 | +
|
| 61 | + return left; |
| 62 | + } |
| 63 | +
|
| 64 | + private static int find(int x) { |
| 65 | + if (uf[x] == x) return x; |
| 66 | +
|
| 67 | + return uf[x] = find(uf[x]); |
| 68 | + } |
| 69 | +} |
| 70 | +``` |
0 commit comments