#include<bits/stdc++.h>
using namespace std;
#define int long long
signed main(){
int n; cin>>n;
int k; cin>>k;
string s; cin>>s;
vector<vector<int>> pre(n+1, vector<int>(3,0));
vector<vector<int>> dp(n+1, vector<int> (3,-1));
vector<char> c = {'J','O','I'};
for(int i = 0; i<n; i++){
for(int j = 0; j<3; j++){
pre[i+1][j] = pre[i][j] + (s[i] == c[j]);
}
}
for(int i = 0; i<n; i++){
for(int j = 0; j < 3; j++){
int l = i; int r = n+1;
while(r > l+1){
int m = (l+r)/2;
if(pre[m][j] - pre[i][j] >= k) r = m;
else l = m;
}
// cerr<<i<<" "<<j<<" "<<r<<" "<<r-i<<endl;
if(r == n+1) dp[i][j] = -1;
else dp[i][j] = r-i;
}
}
int ans = LLONG_MAX;
for(int i = 0; i<n; i++){
bool ok = 1;
if(s[i] != 'J') continue;
int cur = i;
for(int j = 0; j<3; j++){
if(dp[cur][j] == -1){
ok =0; break;
}
cur+=dp[cur][j];
}
if(ok)ans = min(ans,cur-i - 3*k);
}
if(ans == LLONG_MAX) ans = -1;
cout<<ans<<endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
436 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
436 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
696 KB |
Output is correct |
17 |
Correct |
1 ms |
600 KB |
Output is correct |
18 |
Correct |
1 ms |
440 KB |
Output is correct |
19 |
Correct |
1 ms |
604 KB |
Output is correct |
20 |
Correct |
1 ms |
604 KB |
Output is correct |
21 |
Correct |
1 ms |
604 KB |
Output is correct |
22 |
Correct |
1 ms |
600 KB |
Output is correct |
23 |
Correct |
1 ms |
604 KB |
Output is correct |
24 |
Correct |
1 ms |
600 KB |
Output is correct |
25 |
Correct |
1 ms |
604 KB |
Output is correct |
26 |
Correct |
1 ms |
604 KB |
Output is correct |
27 |
Correct |
1 ms |
604 KB |
Output is correct |
28 |
Correct |
1 ms |
604 KB |
Output is correct |
29 |
Correct |
1 ms |
604 KB |
Output is correct |
30 |
Correct |
1 ms |
604 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
604 KB |
Output is correct |
33 |
Correct |
1 ms |
604 KB |
Output is correct |
34 |
Correct |
1 ms |
604 KB |
Output is correct |
35 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
436 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
696 KB |
Output is correct |
17 |
Correct |
1 ms |
600 KB |
Output is correct |
18 |
Correct |
1 ms |
440 KB |
Output is correct |
19 |
Correct |
1 ms |
604 KB |
Output is correct |
20 |
Correct |
1 ms |
604 KB |
Output is correct |
21 |
Correct |
1 ms |
604 KB |
Output is correct |
22 |
Correct |
1 ms |
600 KB |
Output is correct |
23 |
Correct |
1 ms |
604 KB |
Output is correct |
24 |
Correct |
1 ms |
600 KB |
Output is correct |
25 |
Correct |
1 ms |
604 KB |
Output is correct |
26 |
Correct |
1 ms |
604 KB |
Output is correct |
27 |
Correct |
1 ms |
604 KB |
Output is correct |
28 |
Correct |
1 ms |
604 KB |
Output is correct |
29 |
Correct |
1 ms |
604 KB |
Output is correct |
30 |
Correct |
1 ms |
604 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
604 KB |
Output is correct |
33 |
Correct |
1 ms |
604 KB |
Output is correct |
34 |
Correct |
1 ms |
604 KB |
Output is correct |
35 |
Correct |
1 ms |
604 KB |
Output is correct |
36 |
Correct |
34 ms |
20972 KB |
Output is correct |
37 |
Correct |
36 ms |
22508 KB |
Output is correct |
38 |
Correct |
38 ms |
22508 KB |
Output is correct |
39 |
Correct |
54 ms |
22508 KB |
Output is correct |
40 |
Correct |
55 ms |
22496 KB |
Output is correct |
41 |
Correct |
54 ms |
22508 KB |
Output is correct |
42 |
Correct |
54 ms |
22500 KB |
Output is correct |
43 |
Correct |
31 ms |
14320 KB |
Output is correct |
44 |
Correct |
39 ms |
17724 KB |
Output is correct |
45 |
Correct |
50 ms |
22736 KB |
Output is correct |
46 |
Correct |
50 ms |
22508 KB |
Output is correct |
47 |
Correct |
51 ms |
22736 KB |
Output is correct |
48 |
Correct |
51 ms |
22508 KB |
Output is correct |
49 |
Correct |
37 ms |
15084 KB |
Output is correct |
50 |
Correct |
51 ms |
22728 KB |
Output is correct |
51 |
Correct |
50 ms |
22728 KB |
Output is correct |
52 |
Correct |
45 ms |
21728 KB |
Output is correct |
53 |
Correct |
43 ms |
22508 KB |
Output is correct |
54 |
Correct |
43 ms |
22724 KB |
Output is correct |
55 |
Correct |
43 ms |
22496 KB |
Output is correct |
56 |
Correct |
42 ms |
22504 KB |
Output is correct |