From c9cca9e68ec073f3968764b7ff6cc96451d05182 Mon Sep 17 00:00:00 2001 From: oncsr Date: Mon, 1 Sep 2025 22:34:05 +0900 Subject: [PATCH] =?UTF-8?q?[20250901]=20BOJ=20/=20P3=20/=20=EC=8B=9D?= =?UTF-8?q?=EB=8B=B9=20/=20=EA=B6=8C=ED=98=81=EC=A4=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../01 BOJ P3 \354\213\235\353\213\271.md" | 102 ++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 "khj20006/202509/01 BOJ P3 \354\213\235\353\213\271.md" diff --git "a/khj20006/202509/01 BOJ P3 \354\213\235\353\213\271.md" "b/khj20006/202509/01 BOJ P3 \354\213\235\353\213\271.md" new file mode 100644 index 00000000..98f3cde3 --- /dev/null +++ "b/khj20006/202509/01 BOJ P3 \354\213\235\353\213\271.md" @@ -0,0 +1,102 @@ +```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 final int INF = (int)1e9 + 7; + + static int N, M; + static int[] a; + static int[][] dp, idx; + static int[] min; + + public static void main(String[] args) throws Exception { + + io = new IOController(); + + N = io.nextInt(); + M = io.nextInt(); + a = new int[N+1]; + for(int i=1;i<=N;i++) a[i] = io.nextInt(); + dp = new int[N+1][201]; + idx = new int[N+1][201]; + min = new int[N+1]; + Arrays.fill(min, INF); + min[0] = 0; + + for(int k=1;k<=200;k++) { + int[] cnt = new int[M+1]; + int count = 0; + for(int s=1,e=1;e<=N;e++) { + if(cnt[a[e]]++ == 0) count++; + while(count > k) { + if(--cnt[a[s++]] == 0) count--; + } + + idx[e][k] = s; + } + } + + for(int i=1;i<=N;i++) { + for(int k=1;k<=200;k++) dp[i][k] = min[idx[i][k]-1] + k*k; + for(int k=1;k<=200;k++) min[i] = Math.min(min[i], dp[i][k]); + } + + io.write(min[N] + "\n"); + + io.close(); + + } + +} +```