#include <bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define ll long long
#define ff first
#define ss second
#define N 200002
#define MOD 1000000007
string s;
int a[N];
int cnt[3];
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, k;
cin>>n>>k;
cin>>s;
for(int i = 0; i < n; ++i){
if(s[i] == 'J')
a[i] = 0;
else if(s[i] == 'O')
a[i] = 1;
else
a[i] = 2;
}
int i1 = 0, i2, i3;
while(i1 < n && cnt[0] < k)
if(a[i1++] == 0)
++cnt[0];
i2 = i1;
while(i2 < n && cnt[1] < k)
if(a[i2++] == 1)
++cnt[1];
i3 = i2;
while(i3 < n && cnt[2] < k)
if(a[i3++] == 2)
++cnt[2];
if(cnt[2] < k){
cout<<-1;
return 0;
}
int ans = i3 - 3 * k;
for(int i = 0; i < n; ++i){
if(a[i] == 0)
--cnt[0];
while(i1 < n && cnt[0] < k){
if(a[i1] == 0)
++cnt[0];
else if(a[i1] == 1)
--cnt[1];
++i1;
}
while(i2 < n && cnt[1] < k){
if(a[i2] == 1)
++cnt[1];
else if(a[i2] == 2)
--cnt[2];
++i2;
}
while(i3 < n && cnt[2] < k){
if(a[i3] == 2)
++cnt[2];
++i3;
}
if(cnt[2] == k && cnt[1] == k && cnt[0] == k)
ans = min(ans, i3 - 3 * k - i - 1);
}
cout<<ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
11 |
Correct |
5 ms |
376 KB |
Output is correct |
12 |
Correct |
5 ms |
376 KB |
Output is correct |
13 |
Correct |
5 ms |
376 KB |
Output is correct |
14 |
Correct |
5 ms |
380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
11 |
Correct |
5 ms |
376 KB |
Output is correct |
12 |
Correct |
5 ms |
376 KB |
Output is correct |
13 |
Correct |
5 ms |
376 KB |
Output is correct |
14 |
Correct |
5 ms |
380 KB |
Output is correct |
15 |
Correct |
5 ms |
376 KB |
Output is correct |
16 |
Correct |
5 ms |
376 KB |
Output is correct |
17 |
Correct |
5 ms |
376 KB |
Output is correct |
18 |
Correct |
5 ms |
376 KB |
Output is correct |
19 |
Correct |
5 ms |
376 KB |
Output is correct |
20 |
Correct |
5 ms |
376 KB |
Output is correct |
21 |
Correct |
5 ms |
376 KB |
Output is correct |
22 |
Correct |
5 ms |
376 KB |
Output is correct |
23 |
Correct |
5 ms |
376 KB |
Output is correct |
24 |
Correct |
5 ms |
376 KB |
Output is correct |
25 |
Correct |
5 ms |
376 KB |
Output is correct |
26 |
Correct |
5 ms |
376 KB |
Output is correct |
27 |
Correct |
5 ms |
376 KB |
Output is correct |
28 |
Correct |
5 ms |
504 KB |
Output is correct |
29 |
Correct |
5 ms |
376 KB |
Output is correct |
30 |
Correct |
5 ms |
376 KB |
Output is correct |
31 |
Correct |
5 ms |
376 KB |
Output is correct |
32 |
Correct |
5 ms |
376 KB |
Output is correct |
33 |
Correct |
5 ms |
376 KB |
Output is correct |
34 |
Correct |
5 ms |
376 KB |
Output is correct |
35 |
Correct |
5 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
11 |
Correct |
5 ms |
376 KB |
Output is correct |
12 |
Correct |
5 ms |
376 KB |
Output is correct |
13 |
Correct |
5 ms |
376 KB |
Output is correct |
14 |
Correct |
5 ms |
380 KB |
Output is correct |
15 |
Correct |
5 ms |
376 KB |
Output is correct |
16 |
Correct |
5 ms |
376 KB |
Output is correct |
17 |
Correct |
5 ms |
376 KB |
Output is correct |
18 |
Correct |
5 ms |
376 KB |
Output is correct |
19 |
Correct |
5 ms |
376 KB |
Output is correct |
20 |
Correct |
5 ms |
376 KB |
Output is correct |
21 |
Correct |
5 ms |
376 KB |
Output is correct |
22 |
Correct |
5 ms |
376 KB |
Output is correct |
23 |
Correct |
5 ms |
376 KB |
Output is correct |
24 |
Correct |
5 ms |
376 KB |
Output is correct |
25 |
Correct |
5 ms |
376 KB |
Output is correct |
26 |
Correct |
5 ms |
376 KB |
Output is correct |
27 |
Correct |
5 ms |
376 KB |
Output is correct |
28 |
Correct |
5 ms |
504 KB |
Output is correct |
29 |
Correct |
5 ms |
376 KB |
Output is correct |
30 |
Correct |
5 ms |
376 KB |
Output is correct |
31 |
Correct |
5 ms |
376 KB |
Output is correct |
32 |
Correct |
5 ms |
376 KB |
Output is correct |
33 |
Correct |
5 ms |
376 KB |
Output is correct |
34 |
Correct |
5 ms |
376 KB |
Output is correct |
35 |
Correct |
5 ms |
376 KB |
Output is correct |
36 |
Correct |
11 ms |
1412 KB |
Output is correct |
37 |
Correct |
12 ms |
1412 KB |
Output is correct |
38 |
Correct |
12 ms |
1412 KB |
Output is correct |
39 |
Correct |
13 ms |
1412 KB |
Output is correct |
40 |
Correct |
11 ms |
1284 KB |
Output is correct |
41 |
Correct |
12 ms |
1412 KB |
Output is correct |
42 |
Correct |
13 ms |
1412 KB |
Output is correct |
43 |
Correct |
9 ms |
1016 KB |
Output is correct |
44 |
Correct |
9 ms |
1156 KB |
Output is correct |
45 |
Correct |
11 ms |
1412 KB |
Output is correct |
46 |
Correct |
12 ms |
1412 KB |
Output is correct |
47 |
Correct |
11 ms |
1412 KB |
Output is correct |
48 |
Correct |
11 ms |
1412 KB |
Output is correct |
49 |
Correct |
7 ms |
1156 KB |
Output is correct |
50 |
Correct |
7 ms |
1412 KB |
Output is correct |
51 |
Correct |
7 ms |
1412 KB |
Output is correct |
52 |
Correct |
9 ms |
1412 KB |
Output is correct |
53 |
Correct |
9 ms |
1412 KB |
Output is correct |
54 |
Correct |
7 ms |
1408 KB |
Output is correct |
55 |
Correct |
7 ms |
1416 KB |
Output is correct |
56 |
Correct |
6 ms |
1412 KB |
Output is correct |