#include <bits/stdc++.h>
using namespace std;
#define int int64_t
signed main(){
int n, k;
string str;
cin >> n >> k >> str;
vector<int> j_pref(n+1, 0), o_pref(n+1, 0), i_pref(n+1, 0);
for(int i = 0; i < n; i++){
j_pref[i+1] = j_pref[i];
o_pref[i+1] = o_pref[i];
i_pref[i+1] = i_pref[i];
if(str[i] == 'J') j_pref[i+1]++;
else if(str[i] == 'O') o_pref[i+1]++;
else i_pref[i+1]++;
}
int min_ops = 1e9;
for(int i = 0; i < n; i++){
if(str[i] == 'J'){
int curr_ops = 0;
int l = i;
int r = n;
while(r-l > 1){
int m = (l+r)/2;
if(j_pref[m] - j_pref[i] < k) l = m;
else r = m;
}
if(j_pref[r] - j_pref[i] < k) continue;
curr_ops += r - i - k;
i = r;
l = i;
r = n;
while(r-l > 1){
int m = (l+r)/2;
if(o_pref[m] - o_pref[i] < k) l = m;
else r = m;
}
if(o_pref[r] - o_pref[i] < k) continue;
curr_ops += r - i - k;
i = r;
l = i;
r = n;
while(r-l > 1){
int m = (l+r)/2;
if(i_pref[m] - i_pref[i] < k) l = m;
else r = m;
}
if(i_pref[r] - i_pref[i] < k) continue;
curr_ops += r - i - k;
min_ops = min(min_ops, curr_ops);
}
}
if(min_ops == 1e9) cout << -1 << "\n";
else cout << min_ops << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Incorrect |
0 ms |
364 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Incorrect |
0 ms |
364 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Incorrect |
0 ms |
364 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |