From 75e2282f5cd61aee1a9e2ffa91615882a9a1ef11 Mon Sep 17 00:00:00 2001 From: oncsr Date: Tue, 24 Jun 2025 11:08:51 +0900 Subject: [PATCH] =?UTF-8?q?[20250624]=20BOJ=20/=20P5=20/=20=EC=A7=91?= =?UTF-8?q?=ED=95=A9=20=EC=97=B0=EC=82=B0=20/=20=EA=B6=8C=ED=98=81?= =?UTF-8?q?=EC=A4=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...1\355\225\251 \354\227\260\354\202\260.md" | 152 ++++++++++++++++++ 1 file changed, 152 insertions(+) create mode 100644 "khj20006/202506/24 BOJ P5 \354\247\221\355\225\251 \354\227\260\354\202\260.md" diff --git "a/khj20006/202506/24 BOJ P5 \354\247\221\355\225\251 \354\227\260\354\202\260.md" "b/khj20006/202506/24 BOJ P5 \354\247\221\355\225\251 \354\227\260\354\202\260.md" new file mode 100644 index 00000000..b520080d --- /dev/null +++ "b/khj20006/202506/24 BOJ P5 \354\247\221\355\225\251 \354\227\260\354\202\260.md" @@ -0,0 +1,152 @@ +```java +import java.util.*; +import java.io.*; + +class IOController { + BufferedReader br; + BufferedWriter bw; + StringTokenizer st; + + public IOController() { + br = new BufferedReader(new InputStreamReader(System.in)); + bw = new BufferedWriter(new OutputStreamWriter(System.out)); + st = new StringTokenizer(""); + } + + String nextLine() throws Exception { + String line = br.readLine(); + st = new StringTokenizer(line); + return line; + } + + String nextToken() throws Exception { + while (!st.hasMoreTokens()) nextLine(); + return st.nextToken(); + } + + int nextInt() throws Exception { + return Integer.parseInt(nextToken()); + } + + long nextLong() throws Exception { + return Long.parseLong(nextToken()); + } + + double nextDouble() throws Exception { + return Double.parseDouble(nextToken()); + } + + void close() throws Exception { + bw.flush(); + bw.close(); + } + + void write(String content) throws Exception { + bw.write(content); + } + +} + +public class Main { + + static IOController io; + + // + + static int N, Q; + static TreeSet S; + static long[] B; + + public static void main(String[] args) throws Exception { + + io = new IOController(); + + init(); + solve(); + + io.close(); + + } + + public static void init() throws Exception { + + N = io.nextInt(); + S = new TreeSet<>(); + for(int i=0;i list = new ArrayDeque<>(); + + long left = N; + + // 처음에 제거되는 경우가 있으면 먼저 처리 + if(S.contains((long)N)) { + while(left >= 0 && S.contains(left)) S.remove(left--); + list.offerLast(new long[]{N, left}); + } + + // 추가, 제거 반복 + while(true) { + + long next = S.ceiling(left); + if(next == Long.MAX_VALUE){ + list.offerLast(new long[]{left, next}); + break; + } + S.add(left); + list.offerLast(new long[]{left, next}); + S.remove(next); + while(left >= 0 && S.contains(left)) S.remove(left--); + list.offerLast(new long[]{next, left}); + + } + + for(long q : B) { + + while(q > 0) { + long[] cur = list.pollFirst(); + long l = cur[0], r = cur[1], cnt = Math.abs(l-r); + + if(q < cnt) { + if(l > r) { + list.offerFirst(new long[]{l-q, r}); + r = l-q; + } + else { + list.offerFirst(new long[]{l+q, r}); + r = l+q; + } + q = 0; + } + else q -= cnt; + + if(l > r) { + sum -= l*(l+1)/2 - r*(r+1)/2; + } + else { + sum += (r-1)*r/2 - (l-1)*l/2; + } + + } + + io.write(sum + "\n"); + + } + + + + } + +} +```