• 문제 링크
13399번: Rearranging a Sequence
In Sample Input 1, the initial sequence is (1, 2, 3, 4, 5). The first request is to move the integer 4 to the head, that is, to change the sequence to (4, 1, 2, 3, 5). The next request to move the integer 2 to the head makes the sequence (2, 4, 1, 3, 5).
www.acmicpc.net
• 풀이 코드
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.HashSet;
import java.util.Set;
public class Main {
public static void main(String[] args) throws IOException {
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringBuilder sb = new StringBuilder();
int n = read(), m = read();
int[] arr = new int[m];
for (int i = m - 1; i >= 0; i--) arr[i] = read();
Set<Integer> set = new HashSet<>();
for (int i : arr) {
if (!set.contains(i)) {
sb.append(i).append("\n");
set.add(i);
}
}
for (int i = 1; i <= n; i++) if (!set.contains(i)) sb.append(i).append("\n");
bw.write(sb.toString());
bw.flush();
}
private static int read() throws IOException {
int c, n = System.in.read() & 15;
while ((c = System.in.read()) > 32) n = (n << 3) + (n << 1) + (c & 15);
return n;
}
}
'Problem Solving > Baekjoon' 카테고리의 다른 글
[백준] 13237 Binary tree - Graph Theory / Java (0) | 2023.10.14 |
---|---|
[백준] 21316 스피카 - Graph Theory / Java (0) | 2023.10.13 |
[백준] 17287 The Deeper, The Better - Data Structure / Java (0) | 2023.10.11 |
[백준] 28446 볼링공 찾아주기 - Data Structure / Java (0) | 2023.10.10 |
[백준] 26042 식당 입구 대기 줄 - Data Structure / Java (0) | 2023.10.09 |
댓글