Problem Solving/Baekjoon

[백준] 13399 Rearranging a Sequence - Data Structure / Java

graycode 2023. 10. 12. 23:33

 문제 링크

 

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;
    }

}