//
// 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);
void solve() {
int n, k;
cin >> n >> k;
string s;
cin >> s;
if (n == 3) {
if (k == 1 and s == "JOI") cout << 0;
else cout << -1;
return;
}
j[0] = (s[1] == 'J');
o[0] = (s[1] == 'O');
i[0] = (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 = 0; q < n; q++){
if (s[q] != 'J') continue;
int l = q;
int r = n;
int x = j[q] + k - 1;
int cnt = 0;
while (l <= r) {
int mid = (l + r) / 2;
if (j[mid] >= x) {
r = mid - 1;
cnt = mid;
}
else l = mid + 1;
}
if (cnt == 0) continue;
l = cnt + 1;
r = n;
x = o[l - 1] + k;
cnt = 0;
while(l <= r){
int mid = (l + r) / 2;
if (o[mid] >= x) {
r = mid - 1;
cnt = mid;
}
else l = mid + 1;
}
if (cnt == 0) continue;
l = cnt + 1;
r = n;
x = i[l - 1] + k;
cnt = 0;
while(l <= r){
int mid = (l + r) / 2;
if (i[mid] >= x) {
r = mid - 1;
cnt = mid;
}
else l = mid + 1;
}
if (cnt == 0) continue;
mx = min(mx, (ll)cnt - q + 1 - 3 * k);
}
if (mx == 1e18) {
cout << -1;
return;
}
cout << mx;
}
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 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
2 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
2 ms |
4948 KB |
Output is correct |
9 |
Correct |
2 ms |
4948 KB |
Output is correct |
10 |
Correct |
4 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
2 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
4940 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
2 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
2 ms |
4948 KB |
Output is correct |
9 |
Correct |
2 ms |
4948 KB |
Output is correct |
10 |
Correct |
4 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
2 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
4940 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
5032 KB |
Output is correct |
16 |
Correct |
4 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
5028 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
19 |
Correct |
3 ms |
4948 KB |
Output is correct |
20 |
Correct |
3 ms |
4948 KB |
Output is correct |
21 |
Correct |
3 ms |
4948 KB |
Output is correct |
22 |
Correct |
3 ms |
4948 KB |
Output is correct |
23 |
Correct |
2 ms |
4948 KB |
Output is correct |
24 |
Correct |
3 ms |
4948 KB |
Output is correct |
25 |
Correct |
3 ms |
5040 KB |
Output is correct |
26 |
Correct |
3 ms |
4948 KB |
Output is correct |
27 |
Correct |
3 ms |
4948 KB |
Output is correct |
28 |
Correct |
3 ms |
4948 KB |
Output is correct |
29 |
Correct |
3 ms |
4948 KB |
Output is correct |
30 |
Correct |
3 ms |
4948 KB |
Output is correct |
31 |
Correct |
3 ms |
4968 KB |
Output is correct |
32 |
Correct |
3 ms |
4948 KB |
Output is correct |
33 |
Correct |
3 ms |
5040 KB |
Output is correct |
34 |
Correct |
3 ms |
4948 KB |
Output is correct |
35 |
Correct |
3 ms |
4948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
2 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
2 ms |
4948 KB |
Output is correct |
9 |
Correct |
2 ms |
4948 KB |
Output is correct |
10 |
Correct |
4 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
2 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
4940 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
5032 KB |
Output is correct |
16 |
Correct |
4 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
5028 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
19 |
Correct |
3 ms |
4948 KB |
Output is correct |
20 |
Correct |
3 ms |
4948 KB |
Output is correct |
21 |
Correct |
3 ms |
4948 KB |
Output is correct |
22 |
Correct |
3 ms |
4948 KB |
Output is correct |
23 |
Correct |
2 ms |
4948 KB |
Output is correct |
24 |
Correct |
3 ms |
4948 KB |
Output is correct |
25 |
Correct |
3 ms |
5040 KB |
Output is correct |
26 |
Correct |
3 ms |
4948 KB |
Output is correct |
27 |
Correct |
3 ms |
4948 KB |
Output is correct |
28 |
Correct |
3 ms |
4948 KB |
Output is correct |
29 |
Correct |
3 ms |
4948 KB |
Output is correct |
30 |
Correct |
3 ms |
4948 KB |
Output is correct |
31 |
Correct |
3 ms |
4968 KB |
Output is correct |
32 |
Correct |
3 ms |
4948 KB |
Output is correct |
33 |
Correct |
3 ms |
5040 KB |
Output is correct |
34 |
Correct |
3 ms |
4948 KB |
Output is correct |
35 |
Correct |
3 ms |
4948 KB |
Output is correct |
36 |
Correct |
11 ms |
5556 KB |
Output is correct |
37 |
Correct |
12 ms |
5588 KB |
Output is correct |
38 |
Correct |
10 ms |
5536 KB |
Output is correct |
39 |
Correct |
16 ms |
5588 KB |
Output is correct |
40 |
Correct |
12 ms |
5520 KB |
Output is correct |
41 |
Correct |
15 ms |
5588 KB |
Output is correct |
42 |
Correct |
14 ms |
5536 KB |
Output is correct |
43 |
Correct |
7 ms |
5332 KB |
Output is correct |
44 |
Correct |
9 ms |
5536 KB |
Output is correct |
45 |
Correct |
9 ms |
5588 KB |
Output is correct |
46 |
Correct |
11 ms |
5540 KB |
Output is correct |
47 |
Correct |
9 ms |
5572 KB |
Output is correct |
48 |
Correct |
10 ms |
5496 KB |
Output is correct |
49 |
Correct |
7 ms |
5460 KB |
Output is correct |
50 |
Correct |
10 ms |
5540 KB |
Output is correct |
51 |
Correct |
10 ms |
5576 KB |
Output is correct |
52 |
Correct |
8 ms |
5588 KB |
Output is correct |
53 |
Correct |
7 ms |
5556 KB |
Output is correct |
54 |
Correct |
10 ms |
5552 KB |
Output is correct |
55 |
Correct |
8 ms |
5540 KB |
Output is correct |
56 |
Correct |
5 ms |
5540 KB |
Output is correct |