#include <bits/stdc++.h>
// #include <ext/rope>
// #include <ext/pb_ds/assoc_container.hpp>
// using namespace __gnu_pbds;
// using namespace __gnu_cxx;
using namespace std;
// #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define ll long long
#define ull unsigned long long
#define ld long double
#define pb push_back
#define bit(mask, i) ((mask >> i) & 1)
#define el '\n'
#define F first
#define S second
template <class X, class Y> bool maximize(X &x, const Y &y) { return (x < y ? x = y, 1 : 0); }
template <class X, class Y> bool minimize(X &x, const Y &y) { return (x > y ? x = y, 1 : 0); }
const int INF = 1e9;
const ll LINF = 1e18;
const int MOD = 1e9 + 7;
const int MULTI = 0;
const ld eps = 1e-9;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0}; // R D L U
const int ddx[4] = {-1, 1, 1, -1}, ddy[4] = {1, 1, -1, -1}; // UR DR DL UL
const char cx[4] = {'R', 'D', 'L', 'U'};
const ll base = 31;
const int nMOD = 2;
const ll mods[] = {(ll)1e9 + 10777, (ll)1e9 + 19777, (ll)1e9 + 3, (ll)1e9 + 3777};
const int N = 3e5 + 5;
int n, k;
string s;
vector<pair<int, int>> J, O, I, OI;
void getP(vector<pair<int, int>> &v, char c) {
deque<int> q;
int bp = 0;
for (int i = n; i >= 1; --i) {
if (s[i] != c) continue;
q.push_front(i);
if ((int) q.size() == k) {
bp = i;
break;
}
}
if (bp) v.push_back(make_pair(bp, q.back()));
for (int i = bp - 1; i >= 1; --i) {
if (s[i] != c) continue;
q.pop_back();
q.push_front(i);
v.push_back(make_pair(i, q.back()));
}
reverse(v.begin(), v.end());
}
void solve() {
cin >> n >> k >> s;
s = ' ' + s;
getP(J, 'J');
getP(O, 'O');
getP(I, 'I');
int cur = (int) I.size();
for (int i = (int) O.size() - 1; i >= 0; --i) {
while (cur > 0 && I[cur - 1].F > O[i].S) cur--;
if (cur >= 0 && cur < (int) I.size())
OI.push_back(make_pair(O[i].F, I[cur].S));
}
int res = INF;
cur = (int) OI.size();
reverse(OI.begin(), OI.end());
for (int i = (int) J.size() - 1; i >= 0; --i) {
while (cur > 0 && OI[cur - 1].F > J[i].S) cur--;
if (cur >= 0 && cur < (int) OI.size())
minimize(res, OI[cur].S - J[i].F + 1 - 3 * k);
}
cout << (res == INF ? -1 : res);
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
if (!MULTI) solve();
else {
int test; cin >> test;
while (test--) solve();
}
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... |