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 int long long
signed main(){
int n; cin>>n;
int k; cin>>k;
string s; cin>>s;
vector<vector<int>> pre(n+1, vector<int>(3,0));
vector<vector<int>> dp(n+1, vector<int> (3,-1));
vector<char> c = {'J','O','I'};
for(int i = 0; i<n; i++){
for(int j = 0; j<3; j++){
pre[i+1][j] = pre[i][j] + (s[i] == c[j]);
}
}
for(int i = 0; i<n; i++){
for(int j = 0; j < 3; j++){
int l = i; int r = n+1;
while(r > l+1){
int m = (l+r)/2;
if(pre[m][j] - pre[i][j] >= k) r = m;
else l = m;
}
// cerr<<i<<" "<<j<<" "<<r<<" "<<r-i<<endl;
if(r == n+1) dp[i][j] = -1;
else dp[i][j] = r-i;
}
}
int ans = LLONG_MAX;
for(int i = 0; i<n; i++){
bool ok = 1;
if(s[i] != 'J') continue;
int cur = i;
for(int j = 0; j<3; j++){
if(dp[cur][j] == -1){
ok =0; break;
}
cur+=dp[cur][j];
}
if(ok)ans = min(ans,cur-i - 3*k);
}
if(ans == LLONG_MAX) ans = -1;
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... |