//
// 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;
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 |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
5036 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
5076 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 |
5024 KB |
Output is correct |
9 |
Correct |
2 ms |
4948 KB |
Output is correct |
10 |
Correct |
2 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Incorrect |
2 ms |
4948 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
5036 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
5076 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 |
5024 KB |
Output is correct |
9 |
Correct |
2 ms |
4948 KB |
Output is correct |
10 |
Correct |
2 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Incorrect |
2 ms |
4948 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
5036 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
5076 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 |
5024 KB |
Output is correct |
9 |
Correct |
2 ms |
4948 KB |
Output is correct |
10 |
Correct |
2 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Incorrect |
2 ms |
4948 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |