//
// main.cpp
// HAZ
//
// Created by Nurbek Nurbagi
//
// #pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include <bits/stdc++.h>
#define pb push_back
#define ppb pop_back
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
typedef long double ld;
typedef long long ll;
using namespace std;
int gcd (int a, int b) {
while (b) {
a %= b;
swap (a, b);
}
return a;
}
const ll N = 2e5 + 7;
const ll mod = 1e9 + 7;
const ll inf = 1e18;
const ld pi = 3.141592653589793;
const ld eps = 1e-12;
const ll zero = 0;
const int A = 1e9 + 5;
const int dx[4] = {0, -1, 0, 0};
const int dy[4] = {1, 0, -1, 1};
vector <ll> j(N), o(N), i(N);
ll get (int k, int mn, ll l, ll r, char c) {
vector <ll> d;
if (c == 'j') d = j;
if (c == 'o') d = o;
if (c == 'i') d = i;
while (l + 1 < r) {
int mid = (l + r) / 2;
if (d[mid] - d[mn] < k) l = mid;
else r = mid;
}
return r;
}
void solve() {
int n, k;
cin >> n >> k;
string s;
cin >> s;
s = 'm' + s;
j[1] = 0;
o[1] = 0;
i[1] = 0;
j[1] = (s[1] == 'J');
o[1] = (s[1] == 'O');
i[1] = (s[1] == 'I');
for (int q = 1; q <= n; q++) {
j[q] = j[q - 1] + (s[q] == 'J');
o[q] = o[q - 1] + (s[q] == 'O');
i[q] = i[q - 1] + (s[q] == 'I');
}
ll mx = 1e18;
for (int q = 1; q <= n; q++) {
if (s[q] != 'J') continue;
ll cnt = get(k, q - 1, q, n, 'j');
ll cnt2 = get(k, cnt, cnt + 1, n, 'o');
ll cnt3 = get(k, cnt2, cnt2 + 1, n, 'i');
// cout << cnt << " " << cnt2 << " " << cnt3 << "\n";
if (cnt == n or cnt2 == n) break;
cnt3 = (cnt3 - q + 1);
mx = min(cnt3, mx);
}
if (mx == 1e18) {
cout << -1;
return;
}
cout << mx - (k * 3);
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
// freopen("herding.in","r",stdin);
// freopen("herding.out","w",stdout);
int t = 1;
// cin >> t;
for (int i = 1; i <= t; i++) solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
5 ms |
6584 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
5 ms |
6584 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
5 ms |
6584 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |