#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pll;
#define fastio ios::sync_with_stdio(false), cin.tie(0)
#pragma GCC optimize("Ofast")
#define pb push_back
#define eb emplace_back
#define f first
#define s second
#define lowbit(x) x&-x
const int maxn = 4e5 + 5;
const int INF = 1e9;
int main(void){
fastio;
int n, k;
cin>>n>>k;
string s;
cin>>s;
int r1, r2, r3;
vector<int> cnt(3);
for(r1 = 0; r1 < n; r1++){
if(s[r1] == 'J'){
cnt[0] ++;
if(cnt[0] == k) break;
}
}
for(r2 = r1 + 1; r2 < n; r2++){
if(s[r2] == 'O'){
cnt[1]++;
if(cnt[1] == k) break;
}
}
for(r3 = r2 + 1; r3 < n; r3++){
if(s[r3] == 'I'){
cnt[2]++;
if(cnt[2] == k) break;
}
}
if(r3 >= n){
cout<<-1<<"\n";
return 0;
}
int ans = (r3 + 1) - k * 3;
for(int i = 0; i < n; i++){
if(s[i] == 'J') cnt[0] --;
while(cnt[0] < k && r1 < n){
if(s[r1 + 1] == 'O' && r1 + 1 <= r2) cnt[1] --;
if(s[r1 + 1] == 'I' && r1 + 1 > r2 && r1 + 1 <= r3) cnt[2] --;
if(s[r1 + 1] == 'J') cnt[0] ++;
r1++;
}
r2 = max(r2, r1 + 1);
while(cnt[1] < k && r2 < n){
if(s[r2 + 1] == 'O') cnt[1] ++;
if(s[r2 + 1] == 'I' && r2 + 1 <= r3) cnt[2] --;
r2++;
}
r3 = max(r3, r2 + 1);
while(cnt[2] < k && r3 < n){
if(s[r3 + 1] == 'J') cnt[2] ++;
r3++;
}
if(r3 >= n) break;
ans = min(ans, (r3 - i) - 3 * k);
}
cout<<ans<<"\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |