Merged
Conversation
Contributor
Author
import java.io.*;
import java.util.StringTokenizer;
public class BJ_16566_카드_게임_union_find {
private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
private static final StringBuilder sb = new StringBuilder();
private static StringTokenizer st;
private static int N, M, K;
private static int[] choices, parents;
private static boolean[] cards;
public static void main(String[] args) throws IOException {
init();
sol();
}
private static void init() throws IOException {
st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
K = Integer.parseInt(st.nextToken());
cards = new boolean[N + 1];
st = new StringTokenizer(br.readLine());
for (int i = 0; i < M; i++) {
cards[Integer.parseInt(st.nextToken())] = true;
}
parents = new int[N + 2];
int parent = 0;
for (int i = N; i > 0; i--) {
if (cards[i]) parent = i;
parents[i] = parent;
}
choices = new int[K];
st = new StringTokenizer(br.readLine());
for (int i = 0; i < K; i++) {
choices[i] = Integer.parseInt(st.nextToken());
}
}
private static void sol() throws IOException {
for (int choice : choices) {
int target = find(choice + 1);
sb.append(parents[target]).append("\n");
union(target, target + 1);
}
bw.write(sb.toString());
bw.flush();
bw.close();
br.close();
}
private static int find(int a) {
if (parents[a] == a) {
return a;
}
return parents[a] = find(parents[a]);
}
private static void union(int a, int b) {
int rootA = find(a);
int rootB = find(b);
parents[rootA] = rootB;
}
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
🧷 문제 링크
카드 게임
🧭 풀이 시간
40분
👀 체감 난이도
✏️ 문제 설명
주어진 카드 중 철수가 내고자 하는 카드보다 더 큰 카드 내기
단, 이미 낸 카드는 사용할 수 없음
🔍 풀이 방법
upperbound + union-find
기본적으로 upperbound로 내야하는 카드보다 더 큰 카드를 찾는데 이미 낸 카드는 사용 불가능하니까 분리집합에 낼 수 있는 가장 작은 수를 저장함
카드를 내고난 후
union(x, x+1)해서 저장⏳ 회고
낼 수 있는 카드를 미리 저장해두면 이분탐색 없이 분리집합으로도 된다고 합니다