This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// Source: https://usaco.guide/general/io
#include <bits/stdc++.h>
#define pb push_back
using namespace std;
const int inf = 1e9;
vector <int> v[7];
int dp[200003][6];
int main() {
ios_base::sync_with_stdio(0),cin.tie(NULL),cout.tie(NULL);
int n,k;
cin>>n>>k;
string s;
cin>>s;
s='%'+s;
map <char,int> mp;
mp['J'] = 1;
mp['O'] = 2;
mp['I'] = 3;
dp[0][1] = dp[0][2] = dp[0][3] = inf;
int ans = inf;
for (int i = 1; i <= n; i++){
v[mp[s[i]]].pb(i);
for (int j = 1; j <= 3; j++){
dp[i][j] = dp[i - 1][j] + 1;
int t = v[j].size();
if (t < k) continue;
int l = v[j][t - k];
int del = (i - l + 1 - k);
dp[i][j] = min(dp[i][j],del + dp[l - 1][j - 1]);
}
ans=min(ans,dp[i][3]);
}
if (ans >= inf) 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... |