#include <iostream>
using namespace std;
const int MAXN = 200001;
int n, k;
int prefi_J[MAXN];
int prefi_O[MAXN];
int prefi_I[MAXN];
int wystepowanie_J[MAXN];
int wystepowanie_O[MAXN];
int wystepowanie_I[MAXN]; //na jakim indeksie najwczesniej wystepuje wartosc i
char slowo[MAXN];
int zrob(int ktore_j) { //ile trzeba bedzie uzyc operacji 3 typu pesymistycznie zakładając ze pierwsze J w wyrazie bedzie ktore_j w calym wyrazie
int aktl_ind = wystepowanie_J[ktore_j + k];
int ile_O_przed = prefi_O[aktl_ind];
aktl_ind++;
if ((prefi_O[n] - ile_O_przed) < k) {
return 1000000000;
}
int ktore_o = ile_O_przed + k;
aktl_ind = wystepowanie_O[ktore_o];
int ile_i_przed = prefi_I[aktl_ind];
aktl_ind++;
if ((prefi_I[n] - ile_i_przed) < k) {
return 1000000000;
}
int ktore_i = ile_i_przed + k;
aktl_ind = wystepowanie_I[ktore_i];
int ile_liter = aktl_ind - (wystepowanie_J[ktore_j + 1] - 1);
ile_liter -= (3 * k);
return ile_liter;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> k;
for (int i = 1; n >= i; i++) {
cin >> slowo[i];
prefi_J[i] = prefi_J[i - 1];
prefi_I[i] = prefi_I[i - 1];
prefi_O[i] = prefi_O[i - 1];
if (slowo[i] == 'J') {
prefi_J[i]++;
wystepowanie_J[prefi_J[i]] = i;
} else if (slowo[i] == 'O') {
prefi_O[i]++;
wystepowanie_O[prefi_O[i]] = i;
} else {
prefi_I[i]++;
wystepowanie_I[prefi_I[i]] = i;
}
}
if (prefi_J[n] < k) {
cout << -1;
return 0;
}
int wynik = 1000000000;
for (int i = 0; (prefi_J[n] - k) >= i; i++) {
wynik = min(wynik, zrob(i));
}
if (wynik == 1000000000) wynik = -1;
cout << wynik;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2384 KB |
Output is correct |
2 |
Correct |
1 ms |
2384 KB |
Output is correct |
3 |
Correct |
1 ms |
2384 KB |
Output is correct |
4 |
Correct |
1 ms |
2384 KB |
Output is correct |
5 |
Correct |
1 ms |
2384 KB |
Output is correct |
6 |
Correct |
1 ms |
2384 KB |
Output is correct |
7 |
Correct |
2 ms |
2384 KB |
Output is correct |
8 |
Correct |
1 ms |
2384 KB |
Output is correct |
9 |
Correct |
1 ms |
2384 KB |
Output is correct |
10 |
Correct |
1 ms |
2468 KB |
Output is correct |
11 |
Correct |
1 ms |
2384 KB |
Output is correct |
12 |
Correct |
1 ms |
2384 KB |
Output is correct |
13 |
Correct |
1 ms |
2552 KB |
Output is correct |
14 |
Correct |
1 ms |
2384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2384 KB |
Output is correct |
2 |
Correct |
1 ms |
2384 KB |
Output is correct |
3 |
Correct |
1 ms |
2384 KB |
Output is correct |
4 |
Correct |
1 ms |
2384 KB |
Output is correct |
5 |
Correct |
1 ms |
2384 KB |
Output is correct |
6 |
Correct |
1 ms |
2384 KB |
Output is correct |
7 |
Correct |
2 ms |
2384 KB |
Output is correct |
8 |
Correct |
1 ms |
2384 KB |
Output is correct |
9 |
Correct |
1 ms |
2384 KB |
Output is correct |
10 |
Correct |
1 ms |
2468 KB |
Output is correct |
11 |
Correct |
1 ms |
2384 KB |
Output is correct |
12 |
Correct |
1 ms |
2384 KB |
Output is correct |
13 |
Correct |
1 ms |
2552 KB |
Output is correct |
14 |
Correct |
1 ms |
2384 KB |
Output is correct |
15 |
Correct |
1 ms |
2384 KB |
Output is correct |
16 |
Correct |
1 ms |
2576 KB |
Output is correct |
17 |
Correct |
1 ms |
2384 KB |
Output is correct |
18 |
Correct |
1 ms |
2384 KB |
Output is correct |
19 |
Correct |
1 ms |
2384 KB |
Output is correct |
20 |
Correct |
1 ms |
2384 KB |
Output is correct |
21 |
Correct |
1 ms |
2384 KB |
Output is correct |
22 |
Correct |
1 ms |
2384 KB |
Output is correct |
23 |
Correct |
1 ms |
2384 KB |
Output is correct |
24 |
Correct |
1 ms |
2384 KB |
Output is correct |
25 |
Correct |
1 ms |
2384 KB |
Output is correct |
26 |
Correct |
1 ms |
2384 KB |
Output is correct |
27 |
Correct |
1 ms |
2384 KB |
Output is correct |
28 |
Correct |
1 ms |
2384 KB |
Output is correct |
29 |
Correct |
1 ms |
2548 KB |
Output is correct |
30 |
Correct |
1 ms |
2384 KB |
Output is correct |
31 |
Correct |
1 ms |
2384 KB |
Output is correct |
32 |
Correct |
1 ms |
2384 KB |
Output is correct |
33 |
Correct |
1 ms |
2384 KB |
Output is correct |
34 |
Correct |
1 ms |
2384 KB |
Output is correct |
35 |
Correct |
1 ms |
2384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2384 KB |
Output is correct |
2 |
Correct |
1 ms |
2384 KB |
Output is correct |
3 |
Correct |
1 ms |
2384 KB |
Output is correct |
4 |
Correct |
1 ms |
2384 KB |
Output is correct |
5 |
Correct |
1 ms |
2384 KB |
Output is correct |
6 |
Correct |
1 ms |
2384 KB |
Output is correct |
7 |
Correct |
2 ms |
2384 KB |
Output is correct |
8 |
Correct |
1 ms |
2384 KB |
Output is correct |
9 |
Correct |
1 ms |
2384 KB |
Output is correct |
10 |
Correct |
1 ms |
2468 KB |
Output is correct |
11 |
Correct |
1 ms |
2384 KB |
Output is correct |
12 |
Correct |
1 ms |
2384 KB |
Output is correct |
13 |
Correct |
1 ms |
2552 KB |
Output is correct |
14 |
Correct |
1 ms |
2384 KB |
Output is correct |
15 |
Correct |
1 ms |
2384 KB |
Output is correct |
16 |
Correct |
1 ms |
2576 KB |
Output is correct |
17 |
Correct |
1 ms |
2384 KB |
Output is correct |
18 |
Correct |
1 ms |
2384 KB |
Output is correct |
19 |
Correct |
1 ms |
2384 KB |
Output is correct |
20 |
Correct |
1 ms |
2384 KB |
Output is correct |
21 |
Correct |
1 ms |
2384 KB |
Output is correct |
22 |
Correct |
1 ms |
2384 KB |
Output is correct |
23 |
Correct |
1 ms |
2384 KB |
Output is correct |
24 |
Correct |
1 ms |
2384 KB |
Output is correct |
25 |
Correct |
1 ms |
2384 KB |
Output is correct |
26 |
Correct |
1 ms |
2384 KB |
Output is correct |
27 |
Correct |
1 ms |
2384 KB |
Output is correct |
28 |
Correct |
1 ms |
2384 KB |
Output is correct |
29 |
Correct |
1 ms |
2548 KB |
Output is correct |
30 |
Correct |
1 ms |
2384 KB |
Output is correct |
31 |
Correct |
1 ms |
2384 KB |
Output is correct |
32 |
Correct |
1 ms |
2384 KB |
Output is correct |
33 |
Correct |
1 ms |
2384 KB |
Output is correct |
34 |
Correct |
1 ms |
2384 KB |
Output is correct |
35 |
Correct |
1 ms |
2384 KB |
Output is correct |
36 |
Correct |
6 ms |
4688 KB |
Output is correct |
37 |
Correct |
6 ms |
5008 KB |
Output is correct |
38 |
Correct |
7 ms |
4944 KB |
Output is correct |
39 |
Correct |
6 ms |
4944 KB |
Output is correct |
40 |
Correct |
6 ms |
5116 KB |
Output is correct |
41 |
Correct |
6 ms |
4944 KB |
Output is correct |
42 |
Correct |
8 ms |
4944 KB |
Output is correct |
43 |
Correct |
4 ms |
3920 KB |
Output is correct |
44 |
Correct |
7 ms |
4320 KB |
Output is correct |
45 |
Correct |
6 ms |
4944 KB |
Output is correct |
46 |
Correct |
6 ms |
4944 KB |
Output is correct |
47 |
Correct |
7 ms |
4944 KB |
Output is correct |
48 |
Correct |
6 ms |
4944 KB |
Output is correct |
49 |
Correct |
4 ms |
4176 KB |
Output is correct |
50 |
Correct |
7 ms |
4944 KB |
Output is correct |
51 |
Correct |
6 ms |
4944 KB |
Output is correct |
52 |
Correct |
7 ms |
5112 KB |
Output is correct |
53 |
Correct |
6 ms |
5012 KB |
Output is correct |
54 |
Correct |
5 ms |
5112 KB |
Output is correct |
55 |
Correct |
5 ms |
5008 KB |
Output is correct |
56 |
Correct |
7 ms |
4896 KB |
Output is correct |