#include<bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define ll long long int
const ll N = 100+5 , INF = 1e18 , MOD = 1e9+7;
void solve(){
ll n,k;
string s;
cin >> n >> k >> s;
s = ' '+s;
vector<ll> prej(n+5),preo(n+5),prei(n+5);
vector<ll> nearj(n+5),nearo(n+5),neari(n+5);
ll lastj = 0 , lasto = 0 , lasti = 0;
for(ll i = 1; i <= n; i++){
prej[i] = prej[i-1]+(s[i] == 'J');
preo[i] = preo[i-1]+(s[i] == 'O');
prei[i] = prei[i-1]+(s[i] == 'I');
nearj[i] = lastj;
neari[i] = lasti;
nearo[i] = lasto;
if(s[i] == 'J') lastj = i;
else if(s[i] == 'O') lasto = i;
else lasti = i;
}
ll ans = INF;
for(ll i = 1; i <= n; i++){
if(s[i] == 'I'){
ll x = i;
ll low = 1, high = x;
ll ind1 = 0;
while(low <= high){
ll mid = (low+high)/2;
if(prei[x]-prei[mid-1] >= k){
ind1 = mid;
low = mid+1;
}
else high = mid-1;
}
x = ind1;
x = nearo[x];
low = 1, high = x;
ll ind2 = 0;
while(low <= high){
ll mid = (low+high)/2;
if(preo[x]-preo[mid-1] >= k){
ind2 = mid;
low = mid+1;
}
else high = mid-1;
}
x = ind2;
x = nearj[x];
low = 1, high = x;
ll ind3 = 0;
while(low <= high){
ll mid = (low+high)/2;
if(prej[x]-prej[mid-1] >= k){
ind3 = mid;
low = mid+1;
}
else high = mid-1;
}
x = ind3;
if(x > 0) ans = min(ans,(i-x+1)-(3*k));
}
}
if(ans == INF) ans = -1;
cout << ans;
}
int main(){
fast;
ll tc = 1;
// cin >> tc;
while(tc--) solve();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
448 KB |
Output is correct |
17 |
Correct |
1 ms |
448 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
448 KB |
Output is correct |
20 |
Correct |
1 ms |
448 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
0 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
452 KB |
Output is correct |
29 |
Correct |
1 ms |
448 KB |
Output is correct |
30 |
Correct |
1 ms |
448 KB |
Output is correct |
31 |
Correct |
0 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
0 ms |
448 KB |
Output is correct |
34 |
Correct |
1 ms |
448 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
448 KB |
Output is correct |
17 |
Correct |
1 ms |
448 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
448 KB |
Output is correct |
20 |
Correct |
1 ms |
448 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
0 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
452 KB |
Output is correct |
29 |
Correct |
1 ms |
448 KB |
Output is correct |
30 |
Correct |
1 ms |
448 KB |
Output is correct |
31 |
Correct |
0 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
0 ms |
448 KB |
Output is correct |
34 |
Correct |
1 ms |
448 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
13 ms |
9384 KB |
Output is correct |
37 |
Correct |
15 ms |
10012 KB |
Output is correct |
38 |
Correct |
16 ms |
10056 KB |
Output is correct |
39 |
Correct |
16 ms |
9828 KB |
Output is correct |
40 |
Correct |
15 ms |
9948 KB |
Output is correct |
41 |
Correct |
16 ms |
10012 KB |
Output is correct |
42 |
Correct |
15 ms |
10012 KB |
Output is correct |
43 |
Correct |
7 ms |
6484 KB |
Output is correct |
44 |
Correct |
8 ms |
7880 KB |
Output is correct |
45 |
Correct |
11 ms |
10012 KB |
Output is correct |
46 |
Correct |
11 ms |
10012 KB |
Output is correct |
47 |
Correct |
11 ms |
10012 KB |
Output is correct |
48 |
Correct |
14 ms |
10048 KB |
Output is correct |
49 |
Correct |
8 ms |
6620 KB |
Output is correct |
50 |
Correct |
12 ms |
10012 KB |
Output is correct |
51 |
Correct |
11 ms |
10012 KB |
Output is correct |
52 |
Correct |
8 ms |
9556 KB |
Output is correct |
53 |
Correct |
8 ms |
10012 KB |
Output is correct |
54 |
Correct |
12 ms |
10012 KB |
Output is correct |
55 |
Correct |
8 ms |
10012 KB |
Output is correct |
56 |
Correct |
7 ms |
10008 KB |
Output is correct |