From c729a8802248b3a6d87d7d9e59d22a457fa667ef Mon Sep 17 00:00:00 2001 From: DJ-archive Date: Sat, 13 May 2023 17:24:45 +0900 Subject: [PATCH] =?UTF-8?q?[BOJ]=2023326=5F=ED=99=8D=EC=9D=B5=ED=88=AC?= =?UTF-8?q?=EC=96=B4=EB=A6=AC=EC=8A=A4=ED=8A=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- WEEK14/BOJ_23326/dajeong.java | 59 +++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 WEEK14/BOJ_23326/dajeong.java diff --git a/WEEK14/BOJ_23326/dajeong.java b/WEEK14/BOJ_23326/dajeong.java new file mode 100644 index 0000000..170b4ae --- /dev/null +++ b/WEEK14/BOJ_23326/dajeong.java @@ -0,0 +1,59 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.StringTokenizer; +import java.util.TreeSet; + +public class Main { + + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st = new StringTokenizer(br.readLine()); + int N = Integer.parseInt(st.nextToken()); + int Q = Integer.parseInt(st.nextToken()); + + // 이진검색트리 (트리셋) 사용 + // 명소만 저장하기 + TreeSet popular = new TreeSet<>(); + + st = new StringTokenizer(br.readLine()); + for (int i = 1; i <= N; i++) { + int card = Integer.parseInt(st.nextToken()); + if (card == 1) { + popular.add(i); + } + } + + int pos = 1; + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < Q; i++) { + st = new StringTokenizer(br.readLine()); + int command = Integer.parseInt(st.nextToken()); + if (command == 1) { + int idx = Integer.parseInt(st.nextToken()); + if (popular.contains(idx)) { + popular.remove(idx); + } else { + popular.add(idx); + } + } else if (command == 2) { + int move = Integer.parseInt(st.nextToken()); + pos = (pos + move) % N; + if (pos == 0) pos = N; + } else if (command == 3) { + if (popular.isEmpty()) { + sb.append(-1).append("\n"); // 명소가 없는 경우 불가능 + } else { // 명소가 있는 경우 + Integer t = popular.ceiling(pos); // upperbound (도현이 다음 명소의 위치) + if (t == null) { // 도현이 이후에 명소가 없는 경우 + sb.append((popular.first() + N - pos)%N).append("\n"); + } else { // 도현이 이후에 명소가 있는 경우 + sb.append((t - pos)%N).append("\n"); + } + } + } + } + + System.out.println(sb.toString()); + } +} \ No newline at end of file