#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#define int long long
#define exit exit(false)
#define pause system("PAUSE")
//#define here() cerr << "herewego\n";
#define showe(x) cerr << #x << ": " << x << '\n'
#define shows(x) cerr << #x << ": " << x << ", "
#define ioio() ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
using namespace std;
//using namespace __gnu_pbds;
//typedef tree <int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
int n, k, po, ans = INT_MAX;
string s;
vector <int> J, O, I;
int find_1 (int x) {
int l = 0, r = O.size() - 1;
bool f = 0;
while (l <= r) {
int m = (l + r) >> 1;
if (O[m] >= x) {
r = m-1;
f=1;
} else {
l = m+1;
}
}
if (!f) {
return -1;
}
return (r+1);
}
int find_2 (int x) {
int l = 0, r = I.size() - 1;
bool f = 0;
while (l <= r) {
int m = (l + r) >> 1;
if (I[m] >= x) {
r = m-1;
f=1;
} else {
l = m+1;
}
}
if (!f) {
return -1;
}
return (r+1);
}
signed main () { ioio();
cin >> n >> k;
cin >> s;
for (char x : s) {
if (x == 'J') {
J.push_back(po++);
} else if (x == 'O') {
O.push_back(po++);
} else {
I.push_back(po++);
}
}
for (int i = 0; i < J.size(); i++) {
int Js = J[i];
if (i + k - 1 >= J.size()) {
break;
}
int Jf = J[i+k-1];
int c_1 = (Jf - Js + 1) - k;
int j = find_1(Jf+1);
if (j == -1) {
break;
}
int Os = O[j];
if (j + k - 1 >= O.size()) {
break;
}
int Of = O[j+k-1];
int c_2 = (Of - Os + 1) - k;
int c_3 = Os - Jf - 1;
int l = find_2(Of+1);
if (l == -1) {
break;
}
int Is = I[l];
if (l + k - 1 >= I.size()) {
break;
}
int If = I[l+k-1];
int c_4 = (If - Is + 1) - k;
int c_5 = Is - Of - 1;
ans = min(ans, c_1+c_2+c_3+c_4+c_5);
}
if (ans == INT_MAX) {
cout << "-1";
exit;
}
cout << ans;
// cerr << "\nTime elapsed: " << (clock() + 0.0) / CLOCKS_PER_SEC;
}
Compilation message
ho_t2.cpp: In function 'int main()':
ho_t2.cpp:72:20: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
72 | for (int i = 0; i < J.size(); i++) {
| ~~^~~~~~~~~~
ho_t2.cpp:74:17: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
74 | if (i + k - 1 >= J.size()) {
| ~~~~~~~~~~^~~~~~~~~~~
ho_t2.cpp:84:17: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
84 | if (j + k - 1 >= O.size()) {
| ~~~~~~~~~~^~~~~~~~~~~
ho_t2.cpp:95:17: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
95 | if (l + k - 1 >= I.size()) {
| ~~~~~~~~~~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
0 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
0 ms |
364 KB |
Output is correct |
6 |
Correct |
0 ms |
364 KB |
Output is correct |
7 |
Correct |
0 ms |
364 KB |
Output is correct |
8 |
Correct |
0 ms |
364 KB |
Output is correct |
9 |
Correct |
0 ms |
364 KB |
Output is correct |
10 |
Correct |
0 ms |
364 KB |
Output is correct |
11 |
Correct |
0 ms |
364 KB |
Output is correct |
12 |
Correct |
0 ms |
364 KB |
Output is correct |
13 |
Correct |
0 ms |
364 KB |
Output is correct |
14 |
Correct |
0 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
0 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
0 ms |
364 KB |
Output is correct |
6 |
Correct |
0 ms |
364 KB |
Output is correct |
7 |
Correct |
0 ms |
364 KB |
Output is correct |
8 |
Correct |
0 ms |
364 KB |
Output is correct |
9 |
Correct |
0 ms |
364 KB |
Output is correct |
10 |
Correct |
0 ms |
364 KB |
Output is correct |
11 |
Correct |
0 ms |
364 KB |
Output is correct |
12 |
Correct |
0 ms |
364 KB |
Output is correct |
13 |
Correct |
0 ms |
364 KB |
Output is correct |
14 |
Correct |
0 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
0 ms |
364 KB |
Output is correct |
24 |
Correct |
1 ms |
364 KB |
Output is correct |
25 |
Correct |
1 ms |
492 KB |
Output is correct |
26 |
Correct |
1 ms |
364 KB |
Output is correct |
27 |
Correct |
1 ms |
364 KB |
Output is correct |
28 |
Correct |
1 ms |
364 KB |
Output is correct |
29 |
Correct |
1 ms |
364 KB |
Output is correct |
30 |
Correct |
1 ms |
364 KB |
Output is correct |
31 |
Correct |
1 ms |
364 KB |
Output is correct |
32 |
Correct |
1 ms |
364 KB |
Output is correct |
33 |
Correct |
1 ms |
364 KB |
Output is correct |
34 |
Correct |
1 ms |
364 KB |
Output is correct |
35 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
0 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
0 ms |
364 KB |
Output is correct |
6 |
Correct |
0 ms |
364 KB |
Output is correct |
7 |
Correct |
0 ms |
364 KB |
Output is correct |
8 |
Correct |
0 ms |
364 KB |
Output is correct |
9 |
Correct |
0 ms |
364 KB |
Output is correct |
10 |
Correct |
0 ms |
364 KB |
Output is correct |
11 |
Correct |
0 ms |
364 KB |
Output is correct |
12 |
Correct |
0 ms |
364 KB |
Output is correct |
13 |
Correct |
0 ms |
364 KB |
Output is correct |
14 |
Correct |
0 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
0 ms |
364 KB |
Output is correct |
24 |
Correct |
1 ms |
364 KB |
Output is correct |
25 |
Correct |
1 ms |
492 KB |
Output is correct |
26 |
Correct |
1 ms |
364 KB |
Output is correct |
27 |
Correct |
1 ms |
364 KB |
Output is correct |
28 |
Correct |
1 ms |
364 KB |
Output is correct |
29 |
Correct |
1 ms |
364 KB |
Output is correct |
30 |
Correct |
1 ms |
364 KB |
Output is correct |
31 |
Correct |
1 ms |
364 KB |
Output is correct |
32 |
Correct |
1 ms |
364 KB |
Output is correct |
33 |
Correct |
1 ms |
364 KB |
Output is correct |
34 |
Correct |
1 ms |
364 KB |
Output is correct |
35 |
Correct |
1 ms |
364 KB |
Output is correct |
36 |
Correct |
11 ms |
2268 KB |
Output is correct |
37 |
Correct |
13 ms |
2780 KB |
Output is correct |
38 |
Correct |
12 ms |
2764 KB |
Output is correct |
39 |
Correct |
10 ms |
2872 KB |
Output is correct |
40 |
Correct |
7 ms |
2912 KB |
Output is correct |
41 |
Correct |
10 ms |
2932 KB |
Output is correct |
42 |
Correct |
13 ms |
2788 KB |
Output is correct |
43 |
Correct |
3 ms |
1728 KB |
Output is correct |
44 |
Correct |
4 ms |
2024 KB |
Output is correct |
45 |
Correct |
5 ms |
2852 KB |
Output is correct |
46 |
Correct |
5 ms |
2852 KB |
Output is correct |
47 |
Correct |
5 ms |
2964 KB |
Output is correct |
48 |
Correct |
6 ms |
2884 KB |
Output is correct |
49 |
Correct |
3 ms |
1764 KB |
Output is correct |
50 |
Correct |
5 ms |
2880 KB |
Output is correct |
51 |
Correct |
5 ms |
2912 KB |
Output is correct |
52 |
Correct |
4 ms |
2264 KB |
Output is correct |
53 |
Correct |
5 ms |
2780 KB |
Output is correct |
54 |
Correct |
7 ms |
3184 KB |
Output is correct |
55 |
Correct |
6 ms |
3184 KB |
Output is correct |
56 |
Correct |
4 ms |
3184 KB |
Output is correct |