//#pragma GCC optomize("Ofast")
#pragma GCC optimize("unroll-loops")
//#pragma GCC optimize("O3")
//#pragma GCC target("avx2")
//#pragma GCC target("sse,sse2,sse4.1,sse4.2")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define max_heap priority_queue<pair <ll, pair <ll, ll>>>
#define min_heap priority_queue<pair <ll, ll>, vector<pair <ll, ll>>, greater<pair <ll, ll>>>
//#define min_heap priority_queue<ll, vector<ll>, greater<ll>>
#define sariE cin.tie(NULL); cout.tie(NULL); ios_base::sync_with_stdio(false);
#define filE freopen("in.txt", "r", stdin); freopen("out1.txt", "w", stdout);
#define endl '\n'
#define md(a) (a % mod + mod) % mod
#define pb push_back
//cout << vectorprecision(5) << fixed << f;
//hash prime = 769
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
ll const maxn = 2e5 + 123;
ll const inf = 2e18;
ll const loG = 23;
ll const mod = 1e9 + 7;
//ll const mod = 998244353;
ll const sq = 350;
ll power(ll a, ll b, ll mod){if(b==0)return 1;if(b==1)return a;ll x = power(a, b / 2, mod);return (((x * x) % mod) * (b % 2 ? a : 1)) % mod;}
ll n, k, ps[maxn][3], ans;
string s;
vector <ll> koj[3];
void Solve(){
cin >> n >> k >> s;
s = ' ' + s;
for (int i = 1; i < n + 1; i++){
ps[i][0] = ps[i - 1][0];
ps[i][1] = ps[i - 1][1];
ps[i][2] = ps[i - 1][2];
if (s[i] == 'J'){
ps[i][0]++;
koj[0].pb(i);
}
else if (s[i] == 'O'){
ps[i][1]++;
koj[1].pb(i);
}
else{
ps[i][2]++;
koj[2].pb(i);
}
}
ans = inf;
for (int i = 0; i < koj[1].size(); i++){
if (i + k - 1 >= koj[1].size())
break;
ll x = koj[1][i], y = koj[1][i + k - 1];
ll sum = 0;
if (ps[x - 1][0] < k)
continue;
if (ps[n][2] - ps[y][2] < k)
continue;
ll t1 = ps[x - 1][0], t2 = ps[n][2] - ps[y][2];
ll manj = koj[0][t1 - 1 - k + 1], ja = koj[2].size() - 1 - t2 + 1, mani = koj[2][ja + k - 1];
sum += ps[y][0] - ps[x - 1][0] + ps[y][2] - ps[x - 1][2];
sum += ps[x - 1][1] - ps[manj][1] + ps[x - 1][2] - ps[manj][2];
sum += ps[mani][0] - ps[y][0] + ps[mani][1] - ps[y][1];
//cout << x << ' ' << y << ' ' << sum << ' ' << manj << ' ' << mani << endl;
ans = min(ans, sum);
}
cout << (ans == inf ? -1 : ans) << endl;
}
int main(){
sariE;// filE;
int test = 1;
//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... |