#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl "\n"
#define pb push_back
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
void solve(){
int n,k;
cin >> n >> k;
int ans = 1e18; string s; cin >> s;
vector<array<int,26>> cnt(n + 37);
cnt[0][s[0] - 'A'] = 1;
for(int i = 1; i < n; i++){
for(int j = 0; j < 26; j++){
cnt[i][j] = cnt[i-1][j];
}
cnt[i][s[i] - 'A']++;
}
auto check = [&](int i,int m,int ch){
assert(i >= 0 && m >= 0);
return (cnt[i][ch] - (m > 0 ? cnt[m-1][ch] : 0) >= k);
};
for(int i = 2; i < n; i++){
if(s[i] != 'I') continue;
int l = 0,r = i;
while(l < r){
int m = (l + r + 1) / 2;
if(check(i,m,'I'-'A')) l = m;
else r = m - 1;
}
if(!check(i,l,'I'-'A')) continue;
int pre = l - 1; r = l - 1; l = 0;
while(l < r){
int m = (l + r + 1) / 2;
if(check(pre,m,'O'-'A')) l = m;
else r = m - 1;
}
if(!check(pre,l,'O'-'A')) continue;
pre = l - 1; r = l - 1; l = 0;
while(l < r){
int m = (l + r + 1) / 2;
if(check(pre,m,'J'-'A')) l = m;
else r = m - 1;
}
if(!check(pre,l,'J'-'A')) continue;
ans = min(ans,i-l+1 - k*3);
}
cout << (ans == 1e18 ? -1 : ans) << endl;
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
int t = 1;
// cin >> t;
while(t--)
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 |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Runtime error |
1 ms |
468 KB |
Execution killed with signal 6 |
8 |
Halted |
0 ms |
0 KB |
- |
# |
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 |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Runtime error |
1 ms |
468 KB |
Execution killed with signal 6 |
8 |
Halted |
0 ms |
0 KB |
- |
# |
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 |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Runtime error |
1 ms |
468 KB |
Execution killed with signal 6 |
8 |
Halted |
0 ms |
0 KB |
- |