From f808326b7a2518415a3aa8ce3d03775daf41df2c Mon Sep 17 00:00:00 2001 From: jinu-ahn <66781422+jinu-ahn@users.noreply.github.com> Date: Fri, 6 Sep 2024 23:31:04 +0900 Subject: [PATCH] =?UTF-8?q?=ED=9A=8C=EC=A0=84=20=EC=B4=88=EB=B0=A5=20/=206?= =?UTF-8?q?0=EB=B6=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- jinu-ahn/BOJ_15961.java | 57 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 jinu-ahn/BOJ_15961.java diff --git a/jinu-ahn/BOJ_15961.java b/jinu-ahn/BOJ_15961.java new file mode 100644 index 0000000..f0c9b3d --- /dev/null +++ b/jinu-ahn/BOJ_15961.java @@ -0,0 +1,57 @@ +import java.io.*; +import java.util.StringTokenizer; + +public class BOJ_15961 { + private static int N,d,k,c; + private static int[] sushi; + private static int result; + private static int[] choice; + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st = new StringTokenizer(br.readLine()); + N = Integer.parseInt(st.nextToken()); + d = Integer.parseInt(st.nextToken()); + k = Integer.parseInt(st.nextToken()); + c = Integer.parseInt(st.nextToken()); + choice = new int[d+1]; + sushi = new int[N]; + + for (int i = 0; i < N; i++) { + sushi[i] = Integer.parseInt(br.readLine()); + } + + slide(); + + System.out.println(result); + } + + static void slide() { + int left = 1; + int right = k; + int cnt = 0; + + /* 초기 결과값 세팅*/ + for(int i = 0 ; i < k; i++) { + if(choice[sushi[i]] == 0) + cnt++; + choice[sushi[i]]++; + } + result = choice[c] == 0 ? cnt+1 : cnt; // 쿠폰 초밥을 먹지 않았다면 결과 + 1 + if(result == k+1) return; + + while(left != N) { + if(choice[sushi[left-1]] == 1) // 왼쪽 값이 중복되어있지 않을 때 + cnt--; + choice[sushi[left-1]]--; + if(choice[sushi[right]] == 0) // 오른쪽 값이 중복되어 있지 않을 때 + cnt++; + choice[sushi[right]]++; + + result = choice[c] == 0 ? Math.max(result,cnt+1) : Math.max(result,cnt); + if(result == k+1) break; + + right = (right+1) % N; + left++; + } + } +}