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;
/*
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using ordered_set = tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>;
*/
#define all(x) begin(x), end(x)
#define sz(x) (int)x.size()
using ll = long long;
const int mod = 1e9+7;
void solve(int tc) {
int n, k;
cin >> n >> k;
string s;
cin >> s;
map<char, int> mp;
mp[0] = 'J', mp[1] = 'O', mp[2] = 'I';
vector<vector<int>> v(3, vector<int>(n));
for (int i = 0; i < 3; ++i) v[i][0] = (s[i] == mp[i]);
for (int i = 1; i < n; ++i) {
for (int j = 0; j < 3; ++j) {
v[j][i] = v[j][i-1] + (s[i] == mp[j]);
}
}
int ans = -1;
for (int i = 0; i < n-3*k+1; ++i) {
bool f = true;
int p = i;
for (int j = 0; j < 3; ++j) {
int it = lower_bound(all(v[j]), (p?v[j][p-1]+k:k)) - v[j].begin();
if (it == n) {
f = false;
break;
}
p = it+1;
}
if (!f) continue;
if (ans == -1) ans = (p - i) - 3 * k;
ans = min(ans, (p - i) - 3 * k);
}
cout << ans << '\n';
}
signed main() {
ios_base::sync_with_stdio(false); cin.tie(NULL);
int tc = 1;
//cin >> tc;
for (int i = 1; i <= tc; ++i) solve(i);
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... |