This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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.rbegin() + 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;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |