Skip to content

Commit 2c589fd

Browse files
committed
[BOJ] 2531 회전 초밥_240910
1 parent b50e427 commit 2c589fd

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

BOJ/1000-10000번/JW_2531.java

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
public class Main {
2+
public static void main(String[] args) throws Exception {
3+
int n = read(), d = read(), k = read(), c = read();
4+
int[] dishes = new int[n]; // 전체 초밥들의 종류를 저장할 배열
5+
for (int i = 0; i < n; i++)
6+
dishes[i] = read();
7+
int[] used = new int[d + 1]; // 초밥이 종류별로 몇 개 사용됐는지 저장할 배열
8+
int max = 0, cnt = 0; // 최대 종류의 수, 현재 종류의 수
9+
// 슬라이딩 윈도우 초기화
10+
for (int i = 0; i < k; i++) {
11+
// 사용하지 않은 초밥이라면
12+
if (used[dishes[i]] == 0)
13+
cnt++; // 종류 증가
14+
used[dishes[i]]++; // 초밥 개수 증가
15+
}
16+
// 쿠폰 그릇을 사용에 따른 종류 개수
17+
max = used[c] == 0 ? cnt + 1 : cnt;
18+
// 슬라이딩 윈도우 진행
19+
for (int i = 1; i < n; i++) {
20+
used[dishes[i - 1]]--; // 초밥 개수 감소
21+
// 해당 초밥의 개수가 0이 되면
22+
if (used[dishes[i - 1]] == 0)
23+
cnt--; // 종류 감소
24+
// 다음 초밥이 처음 추가되는 거라면
25+
if (used[dishes[(i + k - 1) % n]] == 0)
26+
cnt++; // 종류 증가
27+
used[dishes[(i + k - 1) % n]]++; // 초밥 개수 증가
28+
// 쿠폰 그릇을 사용에 따른 종류 개수
29+
max = Math.max(max, used[c] == 0 ? cnt + 1 : cnt);
30+
}
31+
System.out.println(max);
32+
}
33+
34+
// 빠른 입력을 위한 함수
35+
private static int read() throws Exception {
36+
int c, n = System.in.read() & 15;
37+
while ((c = System.in.read()) >= 48)
38+
n = (n << 3) + (n << 1) + (c & 15);
39+
if (c == 13)
40+
System.in.read();
41+
return n;
42+
}
43+
}

0 commit comments

Comments
 (0)