#include <bits/stdc++.h>
using namespace std;
const int mx = 2e5+5;
const int inf = 1e9+1;
int n, k;
string st;
int main() {
cin.tie(0);ios::sync_with_stdio(0);
cin >> n >> k;
cin >> st;
int z, x, c, v, cj, co, ci;
z = x = c = v = 0;
cj = co = ci = 0;
int ans = inf;
while (z<n&&st[z]!='J') z++;
x = z;
while (x!=n&&cj<k) {
if (st[x] == 'J') cj++;
x++;
}
c = x;
// cout << cj << " " << co << " " << ci << "\n";
while (c!=n&&co<k) {
if (st[c] == 'O') co++;
c++;
}
v = c;
while (v!=n&&ci<k) {
if (st[v] == 'I') ci++;
v++;
}
// cout << z << " " << x << " " << c << " " << v << " " << cj << " " << co << " " << ci << "owo\n";
if (cj == k and co == k and ci == k) {
ans = v-z-3*k;
} else {
cout << -1 << "\n";
return 0;
}
for (;x<n;) {
while (z<n&&st[z]!='J') z++;
cj--;
while (x!=n&&cj<k) {
if (st[x] == 'J') cj++;
if (st[x] == 'O') co--;
x++;
}
while (c!=n&&co<k) {
if (st[c] == 'O') co++;
if (st[c] == 'I') ci--;
c++;
}
while (v!=n&&ci<k) {
if (st[v] == 'I') ci++;
v++;
}
if (cj == k and co == k and ci == k) {
ans = min(ans,v-z-3*k);
}
}
cout << ans << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |