//#pragma GCC optimize("O3")
//#pragma GCC target("avx2")
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <unordered_map>
using namespace std;
#define fastInp cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0);
typedef int ll;
typedef long double ld;
const ll K = 410, INF = 1e9;
ll n;
ll dp[K][K][K][3];
ll cnt[3];
ll pos[3][K];
tuple<ll, ll, ll> prefCnt[K];
int main() {
fastInp;
cin >> n;
string s;
cin >> s;
int i = 1;
for (auto &c : s) {
if (c == 'R') c = 0;
if (c == 'G') c = 1;
if (c == 'Y') c = 2;
cnt[c]++;
pos[c][cnt[c]] = i;
prefCnt[i] = { cnt[0], cnt[1], cnt[2] };
i++;
}
for (int i = 0; i <= cnt[0]; i++) {
for (int j = 0; j <= cnt[1]; j++) {
for (int k = 0; k <= cnt[2]; k++) {
for (int t = 0; t < 3; t++) dp[i][j][k][t] = INF;
}
}
}
dp[0][0][0][0] = 0;
dp[0][0][0][1] = 0;
dp[0][0][0][2] = 0;
for (int i = 0; i <= cnt[0]; i++) {
for (int j = 0; j <= cnt[1]; j++) {
for (int k = 0; k <= cnt[2]; k++) {
for (int add = 0; add < 3; add++) {
ll pn = 0;
ll ind;
if (add == 0) {
ind = pos[add][i + 1];
pn = max(0, get<1>(prefCnt[ind]) - j) + max(0, get<2>(prefCnt[ind]) - k);
} else if (add == 1) {
ind = pos[add][j + 1];
pn = max(0, get<0>(prefCnt[ind]) - i) + max(0, get<2>(prefCnt[ind]) - k);
} else {
ind = pos[add][k + 1];
pn = max(0, get<0>(prefCnt[ind]) - i) + max(0, get<1>(prefCnt[ind]) - j);
}
for (int t = 0; t < 3; t++) {
if (add == t) continue;
ll cnt = dp[i][j][k][t] + pn;
if (add == 0) {
dp[i + 1][j][k][add] = min(dp[i + 1][j][k][add], cnt);
} else if (add == 1) {
dp[i][j + 1][k][add] = min(dp[i][j + 1][k][add], cnt);
} else {
dp[i][j][k + 1][add] = min(dp[i][j][k + 1][add], cnt);
}
}
}
}
}
}
ll a = INF;
for (int i = 0; i < 3; i++) a = min(a, dp[cnt[0]][cnt[1]][cnt[2]][i]);
if (a >= INF) {
cout << "-1\n";
} else {
cout << a;
}
return 0;
}
Compilation message
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:39:7: warning: array subscript has type 'char' [-Wchar-subscripts]
39 | cnt[c]++;
| ^
joi2019_ho_t3.cpp:40:7: warning: array subscript has type 'char' [-Wchar-subscripts]
40 | pos[c][cnt[c]] = i;
| ^
joi2019_ho_t3.cpp:40:14: warning: array subscript has type 'char' [-Wchar-subscripts]
40 | pos[c][cnt[c]] = i;
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
460 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
1 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
588 KB |
Output is correct |
10 |
Correct |
1 ms |
588 KB |
Output is correct |
11 |
Correct |
1 ms |
588 KB |
Output is correct |
12 |
Correct |
1 ms |
460 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
15 |
Correct |
0 ms |
460 KB |
Output is correct |
16 |
Correct |
1 ms |
460 KB |
Output is correct |
17 |
Correct |
0 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
460 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
1 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
588 KB |
Output is correct |
10 |
Correct |
1 ms |
588 KB |
Output is correct |
11 |
Correct |
1 ms |
588 KB |
Output is correct |
12 |
Correct |
1 ms |
460 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
15 |
Correct |
0 ms |
460 KB |
Output is correct |
16 |
Correct |
1 ms |
460 KB |
Output is correct |
17 |
Correct |
0 ms |
332 KB |
Output is correct |
18 |
Correct |
2 ms |
2508 KB |
Output is correct |
19 |
Correct |
1 ms |
2024 KB |
Output is correct |
20 |
Correct |
2 ms |
2508 KB |
Output is correct |
21 |
Correct |
2 ms |
2636 KB |
Output is correct |
22 |
Correct |
1 ms |
1868 KB |
Output is correct |
23 |
Correct |
2 ms |
2252 KB |
Output is correct |
24 |
Correct |
1 ms |
1740 KB |
Output is correct |
25 |
Correct |
2 ms |
4172 KB |
Output is correct |
26 |
Correct |
2 ms |
4428 KB |
Output is correct |
27 |
Correct |
2 ms |
3276 KB |
Output is correct |
28 |
Correct |
3 ms |
2508 KB |
Output is correct |
29 |
Correct |
2 ms |
2380 KB |
Output is correct |
30 |
Correct |
2 ms |
2508 KB |
Output is correct |
31 |
Correct |
2 ms |
2124 KB |
Output is correct |
32 |
Correct |
2 ms |
2508 KB |
Output is correct |
33 |
Correct |
2 ms |
4044 KB |
Output is correct |
34 |
Correct |
2 ms |
3788 KB |
Output is correct |
35 |
Correct |
2 ms |
2892 KB |
Output is correct |
36 |
Correct |
1 ms |
2124 KB |
Output is correct |
37 |
Correct |
1 ms |
1868 KB |
Output is correct |
38 |
Correct |
1 ms |
2252 KB |
Output is correct |
39 |
Correct |
2 ms |
2508 KB |
Output is correct |
40 |
Correct |
1 ms |
460 KB |
Output is correct |
41 |
Correct |
1 ms |
2380 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
69 ms |
164880 KB |
Output is correct |
3 |
Correct |
65 ms |
164164 KB |
Output is correct |
4 |
Correct |
67 ms |
164912 KB |
Output is correct |
5 |
Correct |
66 ms |
164860 KB |
Output is correct |
6 |
Correct |
69 ms |
164804 KB |
Output is correct |
7 |
Correct |
69 ms |
164012 KB |
Output is correct |
8 |
Correct |
68 ms |
164084 KB |
Output is correct |
9 |
Correct |
66 ms |
163396 KB |
Output is correct |
10 |
Correct |
67 ms |
164884 KB |
Output is correct |
11 |
Correct |
66 ms |
164824 KB |
Output is correct |
12 |
Correct |
19 ms |
44984 KB |
Output is correct |
13 |
Correct |
32 ms |
78372 KB |
Output is correct |
14 |
Correct |
46 ms |
112820 KB |
Output is correct |
15 |
Correct |
67 ms |
164812 KB |
Output is correct |
16 |
Correct |
68 ms |
164856 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
460 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
1 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
588 KB |
Output is correct |
10 |
Correct |
1 ms |
588 KB |
Output is correct |
11 |
Correct |
1 ms |
588 KB |
Output is correct |
12 |
Correct |
1 ms |
460 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
15 |
Correct |
0 ms |
460 KB |
Output is correct |
16 |
Correct |
1 ms |
460 KB |
Output is correct |
17 |
Correct |
0 ms |
332 KB |
Output is correct |
18 |
Correct |
2 ms |
2508 KB |
Output is correct |
19 |
Correct |
1 ms |
2024 KB |
Output is correct |
20 |
Correct |
2 ms |
2508 KB |
Output is correct |
21 |
Correct |
2 ms |
2636 KB |
Output is correct |
22 |
Correct |
1 ms |
1868 KB |
Output is correct |
23 |
Correct |
2 ms |
2252 KB |
Output is correct |
24 |
Correct |
1 ms |
1740 KB |
Output is correct |
25 |
Correct |
2 ms |
4172 KB |
Output is correct |
26 |
Correct |
2 ms |
4428 KB |
Output is correct |
27 |
Correct |
2 ms |
3276 KB |
Output is correct |
28 |
Correct |
3 ms |
2508 KB |
Output is correct |
29 |
Correct |
2 ms |
2380 KB |
Output is correct |
30 |
Correct |
2 ms |
2508 KB |
Output is correct |
31 |
Correct |
2 ms |
2124 KB |
Output is correct |
32 |
Correct |
2 ms |
2508 KB |
Output is correct |
33 |
Correct |
2 ms |
4044 KB |
Output is correct |
34 |
Correct |
2 ms |
3788 KB |
Output is correct |
35 |
Correct |
2 ms |
2892 KB |
Output is correct |
36 |
Correct |
1 ms |
2124 KB |
Output is correct |
37 |
Correct |
1 ms |
1868 KB |
Output is correct |
38 |
Correct |
1 ms |
2252 KB |
Output is correct |
39 |
Correct |
2 ms |
2508 KB |
Output is correct |
40 |
Correct |
1 ms |
460 KB |
Output is correct |
41 |
Correct |
1 ms |
2380 KB |
Output is correct |
42 |
Correct |
1 ms |
332 KB |
Output is correct |
43 |
Correct |
69 ms |
164880 KB |
Output is correct |
44 |
Correct |
65 ms |
164164 KB |
Output is correct |
45 |
Correct |
67 ms |
164912 KB |
Output is correct |
46 |
Correct |
66 ms |
164860 KB |
Output is correct |
47 |
Correct |
69 ms |
164804 KB |
Output is correct |
48 |
Correct |
69 ms |
164012 KB |
Output is correct |
49 |
Correct |
68 ms |
164084 KB |
Output is correct |
50 |
Correct |
66 ms |
163396 KB |
Output is correct |
51 |
Correct |
67 ms |
164884 KB |
Output is correct |
52 |
Correct |
66 ms |
164824 KB |
Output is correct |
53 |
Correct |
19 ms |
44984 KB |
Output is correct |
54 |
Correct |
32 ms |
78372 KB |
Output is correct |
55 |
Correct |
46 ms |
112820 KB |
Output is correct |
56 |
Correct |
67 ms |
164812 KB |
Output is correct |
57 |
Correct |
68 ms |
164856 KB |
Output is correct |
58 |
Correct |
95 ms |
78376 KB |
Output is correct |
59 |
Correct |
101 ms |
99036 KB |
Output is correct |
60 |
Correct |
91 ms |
90180 KB |
Output is correct |
61 |
Correct |
91 ms |
87144 KB |
Output is correct |
62 |
Correct |
72 ms |
162520 KB |
Output is correct |
63 |
Correct |
75 ms |
162060 KB |
Output is correct |
64 |
Correct |
92 ms |
151948 KB |
Output is correct |
65 |
Correct |
98 ms |
140420 KB |
Output is correct |
66 |
Correct |
91 ms |
88388 KB |
Output is correct |
67 |
Correct |
92 ms |
85288 KB |
Output is correct |
68 |
Correct |
93 ms |
91092 KB |
Output is correct |
69 |
Correct |
91 ms |
87748 KB |
Output is correct |
70 |
Correct |
94 ms |
94956 KB |
Output is correct |
71 |
Correct |
89 ms |
86164 KB |
Output is correct |
72 |
Correct |
33 ms |
43596 KB |
Output is correct |
73 |
Correct |
5 ms |
3404 KB |
Output is correct |