//Autor: Bartłomiej Czarkowski
#include <bits/stdc++.h>
using namespace std;
#ifdef DEBUG
template<class A,class B>auto&operator<<(ostream&o,pair<A,B>p){return o<<'('<<p.first<<", "<<p.second<<')';}
template<class T>auto operator<<(ostream&o,T x)->decltype(x.end(),o){o<<'{';int i=0;for(auto e:x)o<<(", ")+2*!i++<<e;return o<<'}';}
#define debug(x...) cerr<<"["#x"]: ",[](auto...$){((cerr<<$<<"; "),...);}(x),cerr<<'\n'
#else
#define debug(...) {}
#endif
const int N = 201000;
int n, k;
int t[N];
int poz[N];
char s[N];
vector<int> v[3];
set<int> S[3];
int main() {
scanf("%d%d", &n, &k);
scanf("%s", s + 1);
for (int i = 1; i <= n; ++i) {
if (s[i] == 'J') {
t[i] = 0;
}
else if (s[i] == 'O') {
t[i] = 1;
}
else {
t[i] = 2;
}
v[t[i]].push_back(i);
}
for (int i = 0; i < 3; ++i) {
if ((int)v[i].size() < k) {
printf("-1\n");
return 0;
}
S[i].insert(n + 1 + i);
for (int j = 0; j < (int)v[i].size() - k + 1; ++j) {
S[i].insert(v[i][j]);
poz[v[i][j]] = v[i][j + k - 1];
debug(i, v[i][j], poz[v[i][j]]);
}
}
int odp = 1e9;
for (int i = 1; i <= n; ++i) {
if (t[i] == 0 && poz[i]) {
auto x = S[1].lower_bound(poz[i] + 1);
if (*x > n) {
continue;
}
auto y = S[2].lower_bound(poz[*x] + 1);
if (*y > n) {
continue;
}
debug(i, *x, poz[*x], *y, poz[*y]);
odp = min(odp, poz[*y] - i + 1 - k * 3);
}
}
if (odp > n) {
printf("-1\n");
return 0;
}
printf("%d\n", odp);
return 0;
}
Compilation message
ho_t2.cpp: In function 'int main()':
ho_t2.cpp:21:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
21 | scanf("%d%d", &n, &k);
| ~~~~~^~~~~~~~~~~~~~~~
ho_t2.cpp:22:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
22 | scanf("%s", s + 1);
| ~~~~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
304 KB |
Output is correct |
4 |
Correct |
1 ms |
304 KB |
Output is correct |
5 |
Correct |
1 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 |
308 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 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 |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
304 KB |
Output is correct |
4 |
Correct |
1 ms |
304 KB |
Output is correct |
5 |
Correct |
1 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 |
308 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
584 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
596 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 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 |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
436 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
304 KB |
Output is correct |
33 |
Correct |
1 ms |
468 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
304 KB |
Output is correct |
4 |
Correct |
1 ms |
304 KB |
Output is correct |
5 |
Correct |
1 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 |
308 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
584 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
596 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 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 |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
436 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
304 KB |
Output is correct |
33 |
Correct |
1 ms |
468 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
51 ms |
11588 KB |
Output is correct |
37 |
Correct |
53 ms |
12332 KB |
Output is correct |
38 |
Correct |
52 ms |
12384 KB |
Output is correct |
39 |
Correct |
49 ms |
11644 KB |
Output is correct |
40 |
Correct |
40 ms |
9580 KB |
Output is correct |
41 |
Correct |
46 ms |
11048 KB |
Output is correct |
42 |
Correct |
56 ms |
12236 KB |
Output is correct |
43 |
Correct |
21 ms |
5716 KB |
Output is correct |
44 |
Correct |
27 ms |
6988 KB |
Output is correct |
45 |
Correct |
35 ms |
9052 KB |
Output is correct |
46 |
Correct |
35 ms |
8996 KB |
Output is correct |
47 |
Correct |
35 ms |
8984 KB |
Output is correct |
48 |
Correct |
35 ms |
9032 KB |
Output is correct |
49 |
Correct |
22 ms |
6056 KB |
Output is correct |
50 |
Correct |
35 ms |
9012 KB |
Output is correct |
51 |
Correct |
34 ms |
8980 KB |
Output is correct |
52 |
Correct |
9 ms |
3536 KB |
Output is correct |
53 |
Correct |
11 ms |
4028 KB |
Output is correct |
54 |
Correct |
46 ms |
12364 KB |
Output is correct |
55 |
Correct |
16 ms |
5676 KB |
Output is correct |
56 |
Correct |
3 ms |
2780 KB |
Output is correct |