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 ll long long
#define pb push_back
const int mxN = 3e5 + 5;
ll n,k;
string s;
map<ll,map<char,ll>> p;
ll find(char ch, ll i){
ll l = i - 1, r = n;
while(r > l + 1){
ll mid = l + (r - l) / 2;
if(i == 0){
if(p[mid][ch] >= k) r = mid;
else l = mid;
continue;
}
if(p[mid][ch] - p[i - 1][ch] >= k) r = mid;
else l = mid;
}
return r;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> k >> s;
for(int i = 0; i < n; i++){
if(i != 0){
p[i]['J'] = p[i - 1]['J'];
p[i]['O'] = p[i - 1]['O'];
p[i]['I'] = p[i - 1]['I'];
}
p[i][s[i]]++;
}
ll ans = 1e9;
for(int i = 0; i < n; i++){
if(s[i] == 'J'){
ll r = find('J', i);
r = find('O', r);
r = find('I', r);
if(r == n) continue;
ans = min(ans, r - i + 1 - 3 * k);
}
}
if(ans == 1e9) cout << -1;
else cout << ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |