https://www.acmicpc.net/problem/2531
코드에 상수를 쓴걸 모르고 한참 헤맸다;;
초밥 종류 +1 만큼 배열 변수 type 를 선언하고 특정 초밥이 몇개 있는지 카운팅을 한다
특정 초밥이 0개 였는데 1이 될때 초밥종류 typeCnt++
특정 초밥 개수가 1에서 0이 되면 초밥종류 typeCnt--
방식으로 풀었다
탐색 시작 변수가 i 이고 k 가 4이면
i가 4부터 시작해서 다시 4가 될때 까지 한바퀴 돌려야 한다
import java.io.*;
public class Main
{
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] input = br.readLine().split(" ");
int N = Integer.parseInt(input[0]);//2 ≤ N ≤ 30,000
int d = Integer.parseInt(input[1]);//2 ≤ d ≤ 3,000
int k = Integer.parseInt(input[2]);//2 ≤ k ≤ 3,000 (k ≤ N)
int c = Integer.parseInt(input[3]);//1 ≤ c ≤ d
int[] arr = new int[N];
for(int i = 0; i < N; ++i)
{
arr[i] = Integer.parseInt(br.readLine());
}
//k 개수만큼 종류 파악
int typeCnt = 0;
int[] type = new int[d+1];//종류별 개수
for(int i = 0; i < k; ++i)
{
if(type[arr[i]] == 0)
typeCnt++;
type[arr[i]]++;
}
//쿠폰
if(type[c] == 0)
typeCnt++;
type[c]++;
int ans = typeCnt;
int i = k;
while(true)
{
if(i >= N)
i = 0;
int del = i-k;
if(del < 0)
del += N;
type[arr[del]]--;
if(type[arr[del]] == 0)
typeCnt--;
if(type[arr[i]] == 0)
typeCnt++;
type[arr[i]]++;
ans = Math.max(ans, typeCnt);
i++;
if(i == k)
break;
}
System.out.println(ans);
}
}