#include <bits/stdc++.h>
using namespace std;
#define finish(x) return cout << x << endl, 0
#define ll long long
int n, k;
string s;
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> k >> s;
set <int> p;
vector <int> J(n);
for(int i = 0 ; i < n ; i++){
if(s[i] == 'J') p.insert(i);
if((int)p.size() > k) p.erase(p.begin());
if((int)p.size() < k) J[i] = -1;
else J[i] = *p.begin();
}
p.clear();
vector <int> I(n);
for(int i = n - 1 ; i >= 0 ; i--){
if(s[i] == 'I') p.insert(i);
if((int)p.size() > k) p.erase(--p.end());
if((int)p.size() < k) I[i] = -1;
else I[i] = *p.rbegin();
}
p.clear();
int ans = 1e9;
for(int i = n - 1 ; i >= 0 ; i--){
if(s[i] == 'O') p.insert(i);
if((int)p.size() > k) p.erase(--p.end());
if((int)p.size() < k) continue;
if(i > 0 && J[i - 1] != -1 && *p.begin() + 1 < n && I[*p.rbegin() + 1] != -1){
ans = min(ans, I[*p.rbegin() + 1] - J[i - 1] + 1);
}
}
if(ans == 1e9) ans = -1;
else ans -= 3 * k;
cout << ans << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
380 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Incorrect |
5 ms |
376 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
380 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Incorrect |
5 ms |
376 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
380 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Incorrect |
5 ms |
376 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |