#include <iostream>
#include <iomanip>
#include <cassert>
#include <vector>
#include <numeric>
#include <string>
#include <bitset>
#include <cmath>
#include <algorithm>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <random>
#include <ctime>
#include <chrono>
using namespace std;
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) (int)((x).size())
typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
//typedef __int128_t int128;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const char en = '\n';
const int INF = 1e9 + 7;
const ll INFLL = 1e18;
mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
#ifdef __APPLE__
#include "debug.h"
//#define debug(...) 42
#else
#define debug(...) 42
#endif
void solve() {
int n, k;
string s;
cin >> n >> k >> s;
vector<int> pos_j, pos_i;
for (int i = 0; i < n; ++i) {
if (s[i] == 'J') {
pos_j.emplace_back(i);
}
}
for (int i = n - 1; i > -1; --i) {
if (s[i] == 'I') {
pos_i.emplace_back(i);
}
}
if (sz(pos_j) < k || sz(pos_i) < k) {
cout << -1 << en;
return;
}
vector<int> pos_o;
for (int i = pos_j[k - 1] + 1; i < pos_i[k - 1]; ++i) {
if (s[i] == 'O') {
pos_o.emplace_back(i);
}
}
if (sz(pos_o) < k) {
cout << -1 << en;
return;
}
int ans = n;
reverse(all(pos_i));
// int ptr_j = k - 1, ptr_o = 0, ptr_i = 0;
// while (pos_i[ptr_i] < pos_o[ptr_o + k - 1]) {
// ++ptr_i;
// }
//
// pos_j.emplace_back(-1);
// pos_i.emplace_back(n + 1);
//
// while (ptr_j < sz(pos_j) && ptr_o + k - 1 < sz(pos_o) && ptr_i + k - 1 < sz(pos_i)) {
// int l = pos_j[ptr_j - k + 1], r = pos_i[ptr_i + k - 1];
// ans = min(ans, r - l + 1 - 3 * k);
// ++ptr_o;
// if (!(ptr_o + k - 1 < sz(pos_o))) {
// break;
// }
// while (ptr_i < sz(pos_i) && pos_i[ptr_i] < pos_o[ptr_o + k - 1]) {
// ++ptr_i;
// }
// while (ptr_j + 1 < sz(pos_j) && pos_o[ptr_o] < pos_j[ptr_j + 1]) {
// ++ptr_j;
// }
// }
for (int i = 0; i + k - 1 < sz(pos_o); ++i) {
int o1 = pos_o[i], o2 = pos_o[i + k - 1];
int id_j = (int)(lower_bound(all(pos_j), o1) - pos_j.begin());
--id_j;
// while (id_j > -1 && o1 < pos_j[id_j]) {
// --id_j;
// }
if (id_j - k + 1 < 0) {
continue;
}
int id_i = (int)(lower_bound(all(pos_i), o2) - pos_i.begin());
if (id_i + k - 1 >= sz(pos_i)) {
continue;
}
int l = pos_j[id_j - k + 1], r = pos_i[id_i + k - 1];
ans = min(ans, r - l + 1 - 3 * k);
}
cout << ans << en;
}
int main() {
#ifdef __APPLE__
freopen("input.txt", "r", stdin);
#else
ios_base::sync_with_stdio(0);
cin.tie(0);
#endif
solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
288 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
288 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
324 KB |
Output is correct |
29 |
Correct |
1 ms |
328 KB |
Output is correct |
30 |
Correct |
1 ms |
324 KB |
Output is correct |
31 |
Correct |
1 ms |
212 KB |
Output is correct |
32 |
Correct |
1 ms |
324 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
324 KB |
Output is correct |
35 |
Correct |
1 ms |
324 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
288 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
288 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
324 KB |
Output is correct |
29 |
Correct |
1 ms |
328 KB |
Output is correct |
30 |
Correct |
1 ms |
324 KB |
Output is correct |
31 |
Correct |
1 ms |
212 KB |
Output is correct |
32 |
Correct |
1 ms |
324 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
324 KB |
Output is correct |
35 |
Correct |
1 ms |
324 KB |
Output is correct |
36 |
Correct |
11 ms |
1484 KB |
Output is correct |
37 |
Correct |
13 ms |
2012 KB |
Output is correct |
38 |
Correct |
13 ms |
2012 KB |
Output is correct |
39 |
Correct |
12 ms |
1624 KB |
Output is correct |
40 |
Correct |
5 ms |
1752 KB |
Output is correct |
41 |
Correct |
9 ms |
1752 KB |
Output is correct |
42 |
Correct |
12 ms |
1748 KB |
Output is correct |
43 |
Correct |
3 ms |
1172 KB |
Output is correct |
44 |
Correct |
3 ms |
1220 KB |
Output is correct |
45 |
Correct |
4 ms |
1748 KB |
Output is correct |
46 |
Correct |
5 ms |
1728 KB |
Output is correct |
47 |
Correct |
5 ms |
1752 KB |
Output is correct |
48 |
Correct |
5 ms |
1752 KB |
Output is correct |
49 |
Correct |
3 ms |
1116 KB |
Output is correct |
50 |
Correct |
4 ms |
1752 KB |
Output is correct |
51 |
Correct |
4 ms |
1752 KB |
Output is correct |
52 |
Correct |
4 ms |
1240 KB |
Output is correct |
53 |
Correct |
4 ms |
1764 KB |
Output is correct |
54 |
Correct |
6 ms |
2008 KB |
Output is correct |
55 |
Correct |
5 ms |
2012 KB |
Output is correct |
56 |
Correct |
3 ms |
2012 KB |
Output is correct |