회전 초밥
Last updated
Last updated
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nectInt(); // 접시 수
int d = sc.nextInt(); // 초밥 가짓수
int k = sc.nextInt(); // 연속으로 먹는 접시 수
int c = sc.nextInt(); // 쿠폰 번호
int[] arr = new int[N]; // 초밥 벨트
for(int i = 0; i < N; i++) {
arr[i] = sc.nextInt();
}
int[] count = new int[d + 1]; // 각 초밥 종류의 먹은 개수 카운트
int unique = 0;
//초기 윈도우 설정
for(int i = 0; i < k; i++) {
int sushi = arr[i % N];
if(count[sushi] == 0) unique++; // 처음 먹는 종류묜 증가
count[sushi]++;
}
// 쿠폰 초밥 처리
if(count[c] == 0) unique++;
int max = unique;
//슬라이딩 윈도우 : 총 N 번만 이동해보면 충분(원형 구조 고려)
for(int i = 1; i < N; i++) {
//윈도우 맨 앞 제거
int remove = arr[(i - 1) % N];
count[remove]--;
if(count[remove] == 0 && remove != c) unique--; // 종류 빠지면 감소
// 윈도우 새로 추가
int add = arr[(i + k - 1) % N];
if(count[add] == 0) unique++;
count[add]++;
// 쿠폰 초밥이 이미 포함되어 있지 않다면 보너스로 +1
int current = unique;
if(count[c] == 0) current++;
max = Math.max(max, current);
max = Math.max(max, current);
}
System.out.println(max);
}
}