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>
#define mp make_pair
#define pb emplace_back
#define fi first
#define se second
#define int long long
#define inf 1e18
#define ick cout<<"ickbmi32.9\n"
using namespace std;
bool isprime(int k) {
for(int i = 2; i <= sqrt(k); i++) if(k % i == 0) return false;
return true;
}
int bm(int a, int b, int mod) {
if(b == 0) return 1;
int t = bm(a, b / 2, mod);
t = t * t % mod;
return (b % 2 == 1 ? t * a % mod : t);
}
int inv(int a, int mod) {return bm(a, mod - 2, mod);}
int n, k;
string s;
int jpos[200005], ipos[200005], ops[200005];// 1-based!!!
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin >> n >> k >> s; k--;
s = "_" + s;
int jcnt = 0, icnt = 0;
for(int i = 1; i <= n; i++) {
if(s[i] == 'J') jpos[jcnt++] = i;
else if(s[i] == 'I') ipos[icnt++] = i;
else ops[i]++;
ops[i] += ops[i - 1];
}
int ans = inf;
int rpt = 0;
for(int i = 0; i < jcnt - k; i++) {
while(rpt + k < icnt && ops[ipos[rpt]] - ops[jpos[i + k]] <= k) rpt++;
if(rpt + k >= icnt) break;
ans = min(ans, ipos[rpt + k] - jpos[i] - 2 - 3 * k);
}
if(ans == inf) ans = -1;
cout << ans << '\n';
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |