๐ก Info
๋ด์ฉ
์ฒ ์์ ๋ฏผ์๋ ์นด๋ ๊ฒ์์ ์ฆ๊ฒจํ๋ค. ์ด ์นด๋ ๊ฒ์์ ๊ท์น์ ๋ค์๊ณผ ๊ฐ๋ค.
๋ฏผ์๋ ๋ฐ์ด๋ ์ฌ๋ฆฌํ์์ด๊ธฐ ๋๋ฌธ์ ์ฒ ์๊ฐ ๋ผ ์นด๋๋ฅผ ์์๋ผ ์ ์๋ค. ๊ทธ๋์ ๋ฏผ์๋ ์ฒ ์๊ฐ ๋ผ ์นด๋๋ณด๋ค ํฐ ์นด๋๊ฐ ์๋ค๋ฉด ๊ทธ ์นด๋๋ค ์ค ๊ฐ์ฅ ์์ ์นด๋๋ฅผ ๋ด๊ธฐ๋ก ํ๋ค.
K๋ฒ ๋์ ์ฒ ์๊ฐ ๋ผ ์นด๋๊ฐ ์ ๋ ฅ์ผ๋ก ์ฃผ์ด์ง๋ค. ๊ทธ๋ ๋ค๋ฉด ๋ฏผ์๊ฐ ์ด๋ค ์นด๋๋ฅผ ๋ผ์ง ์ถ๋ ฅํ๋ผ. ๋จ, ๋ฏผ์๊ฐ ์นด๋๋ฅผ ๋ด์ง ๋ชปํ๋ ๊ฒฝ์ฐ๋ ์๋ค๊ณ ๊ฐ์ ํ๋ค.
๐ฅ์ ๋ ฅ ์กฐ๊ฑด
10 7 5
2 5 3 7 8 4 9
4 1 1 3 8
๐ค์ถ๋ ฅ ์กฐ๊ฑด
5
2
3
4
9
์ค์ ํ์ด ์๊ฐ : 1์๊ฐ 10๋ถ(์ฒซ ํ์ด ์๊ฐ ํฌํจ)
์ ๋ ฅ
๊ณ์ฐ
์ถ๋ ฅ
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
int N = Integer.parseInt(st.nextToken());
int M = Integer.parseInt(st.nextToken());
int K = Integer.parseInt(st.nextToken());
int[] cards = new int[M];
st = new StringTokenizer(br.readLine(), " ");
for (int i = 0; i < M; i++) {
cards[i] = Integer.parseInt(st.nextToken());
}
Arrays.sort(cards);
int[] parent = new int[M];
for (int i = 0; i < M; i++) {
parent[i] = i;
}
st = new StringTokenizer(br.readLine(), " ");
for (int i = 0; i < K; i++) {
int pick = Integer.parseInt(st.nextToken());
int index = findCard(cards, pick);
int availableIndex = find(parent, index);
sb.append(cards[availableIndex]).append("\n");
union(parent, availableIndex, availableIndex + 1);
}
System.out.print(sb);
}
private static int findCard(int[] cards, int pick) {
int low = 0, high = cards.length - 1;
while (low <= high) {
int mid = (low + high) / 2;
if (cards[mid] <= pick) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return low;
}
private static int find(int[] parent, int x) {
if (parent[x] == x) {
return x;
}
return parent[x] = find(parent, parent[x]);
}
private static void union(int[] parent, int x, int y) {
if (y < parent.length) {
parent[x] = find(parent, y);
}
}
}