/*#pragma GCC optimize("O3")
#pragma GCC optimization("Ofast,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")*/
#include <bits/stdc++.h>
/*#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>*/
#define ll int
#define ll1 long long
#define ull unsigned long long
#define dou long double
#define str string
#define vll vector<ll>
#define vi vector<int>
#define pll pair<ll, ll>
#define vpll vector<pll>
#define vbool vector<bool>
#define vstr vector<str>
#define vvll vector<vll>
#define pb push_back
#define pf push_front
#define endl "\n"
#define fr first
#define se second
// #define sortcmp(a) sort(a.begin(), a.end(), cmp)
#define sort(a) sort(a.begin(), a.end())
#define reverse(a) reverse(a.begin(), a.end())
#define speed ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define INF 1000000000000000007
#define ordered_set tree<ll, null_type, less_equal<ll>, rb_tree_tag, tree_order_statistics_node_update>
using namespace std;
//using namespace __gnu_pbds;
mt19937 rng(time(0));
ll randll(ll l, ll r) {
return uniform_int_distribution<ll>(l, r)(rng);
}
int main(){
speed;
srand(time(0));
ll n, k;
cin >> n >> k;
str s;
cin >> s;
ll k1 = 0, k2 = 0, k3 = 0;
ll l = -1, r = -1, r1 = -1, x = -1;
for (int i = 0; i < n; i ++) {
if (k1 < k) {
if (s[i] == 'J') {
if (l == -1) l = i;
k1 ++;
x = i;
}
}
else if (k2 < k) {
if (s[i] == 'O') {
k2 ++;
r = i;
}
}
else if (k3 < k) {
if (s[i] == 'I') {
k3 ++;
r1 = i;
}
}
else break;
}
if (k3 < k) {
cout << -1 << endl;
return 0;
}
ll minl = r1-l+1-k*3;
for (int i = x+1; i < n; i ++) {
if (s[i] != 'J') {
if (s[i] == 'O') {
r1 ++;
while (1) {
if (s[r] == 'O') break;
if (s[r] == 'I') {
k3 --;
r1 ++;
while (r1 < n) {
if (s[r1] == 'I') {
k3 ++;
break;
}
r1 ++;
}
if (k3 < k) break;
}
r ++;
}
if (k3 < k) break;
}
continue;
}
l ++;
while (s[l] != 'J') l ++;
minl = min(minl, r1-l+1-k*3);
}
cout << minl << 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... |