#include <bits/stdc++.h>
#define int long long
#define fastio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
using namespace std;
const int N = 2e5+5;
int cntj[N], cnto[N], cnti[N];
signed main(){
fastio
int n,k;
cin >> n >> k;
string s;
cin >> s;
s = '$' + s;
for(int i = 1;i <= n;i++){
cntj[i] = cntj[i-1], cnto[i] = cnto[i-1], cnti[i] = cnti[i-1];
if(s[i]=='J'){
cntj[i]++;
}else if(s[i]=='O'){
cnto[i]++;
}else if(s[i]=='I'){
cnti[i]++;
}
}
auto check = [&](int i, int j){
int idx = lower_bound(cntj+1,cntj+n,cntj[i-1]+k)-cntj;
if(idx >= j) return false;
idx = lower_bound(cnto+1,cnto+n,cnto[idx-1]+k)-cnto;
if(idx >= j) return false;
idx = lower_bound(cnti+1,cnti+n,cnti[idx-1]+k)-cnti;
if(idx > j) return false;
else return true;
};
int ans = 1e18;
for(int i = 1;i <= n;i++){
int l = i, r = n;
while(l < r){
int mid = l+r>>1;
if(check(i,mid)) r = mid;
else l = mid+1;
}
if(check(i,l)) ans = min(l-i+1-k*3,ans);
}
cout << (ans==1e18 ? -1 : ans) << "\n";
}
Compilation message
ho_t2.cpp: In function 'int main()':
ho_t2.cpp:41:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
41 | int mid = l+r>>1;
| ~^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
492 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
492 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
492 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |