File tree Expand file tree Collapse file tree 1 file changed +30
-5
lines changed Expand file tree Collapse file tree 1 file changed +30
-5
lines changed Original file line number Diff line number Diff line change 1
- // Authored by : BaaaaaaaaaaarkingDog
1
+ // Authored by : OceanShape
2
2
// Co-authored by : -
3
- // http://boj.kr/****************
3
+ // http://boj.kr/5af73c100b294e40b6bcc4463ab1c879
4
4
#include < bits/stdc++.h>
5
5
using namespace std ;
6
6
7
- int main (void ){
7
+ int N, K, ans;
8
+ int cnt[100005 ]; // cnt[i]: i번째 수의 등장 횟수
9
+ int a[200005 ]; // 수열 a_i를 저장하는 배열
10
+
11
+ int main (void ) {
8
12
ios::sync_with_stdio (0 );
9
13
cin.tie (0 );
10
-
11
- }
14
+ cin >> N >> K;
15
+ for (int i = 0 ; i < N; ++i) cin >> a[i];
16
+
17
+ // 연속 부분 수열의 시작을 st, 끝을 en으로 정의
18
+ int en = 0 ;
19
+ // 처음 위치 a_0의 등장 횟수 추가
20
+ ++cnt[a[en]];
21
+ for (int st = 0 ; st < N; ++st) {
22
+ // en + 1이 수열 a_i의 끝(N-1)을 넘지 않거나,
23
+ // a_(en + 1)의 등장횟수를 높여도 K를 넘지 않을 경우,
24
+ // en을 올려 수열의 길이 연장
25
+ while (en + 1 != N && cnt[a[en + 1 ]] < K) {
26
+ ++en;
27
+ cnt[a[en]]++; // 새로운 수열 끝부분을 확인 후, 횟수 추가
28
+ }
29
+ // 연장이 끝날 경우, 현재 길이를 최장 길이와 비교
30
+ ans = max (ans, en - st + 1 );
31
+ // 시작 인덱스 값을 올리기 전,
32
+ // 수열의 시작에 해당하는 수의 등장횟수를 줄인다
33
+ cnt[a[st]]--;
34
+ }
35
+ cout << ans;
36
+ }
You can’t perform that action at this time.
0 commit comments