/**
* data : 09.07.2024
*
**/
#include <bits/stdc++.h>
// #include "algo/turnikmen.h"
using namespace std;
#define int long long
#define bitt __builtin_popcountll
#define bitzero __builtin_clz
void fReopen () {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}
signed main (/* time : 9:17 AM */) {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int n; cin >> n;
string s; cin >> s; s = '1' + s;
vector<int> g[4];
int pref[4][n + 2]; for (int i = 0; i < 3; i++) for (int j = 0; j <= n; j++) pref[i][j] = 0;
int R = 0, G = 1, Y = 2;
int cntr, cntg, cnty;
cntr = cntg = cnty = 0;
pref[1][0] = pref[0][0] = pref[2][0] = 0;
for (int i = 1; i <= n; i++) {
for (int j = 0; j < 3; j++) pref[j][i] += pref[j][i - 1];
if (s[i] == 'R') {
cntr++;
g[0].push_back(i);
pref[0][i]++;
}else if(s[i] == 'G') {
cntg++;
g[2].push_back(i);
pref[2][i]++;
}else if(s[i] == 'Y') {
cnty++;
g[1].push_back(i);
pref[1][i]++;
}
}
int dp[n + 1][cntr + 1][cnty + 1][3] = { };
for (int i = 1; i <= n; i++) {
for (int a = 0; a <= cntr; a++) {
for (int b = 0; b <= cnty; b++) {
for (int j = 0; j < 3; j++)
dp[i][a][b][j] = 1e18;
}
}
}
for (int i = 0; i < 3; i++) dp[0][0][0][i] = 0;
for (int i = 1; i <= n; i++) {
for (int a = 0; a <= cntr; a++) {
for (int b = 0; b <= cnty; b++) {
if (a + b > i) continue;
int c = i - a - b;
if (c > cntg) continue;
for (int last = 0; last < 3; last++) {
if (last != 0 && a) {
int j = g[0][a - 1];
int adj = max(0ll, b - pref[1][j]) + max(0ll, c - pref[2][j]);
int val = max(0ll, j + adj - i);
dp[i][a][b][0] = min(dp[i][a][b][0], dp[i - 1][a - 1][b][last] + val);
}if (last != 1 && b) {
int j = g[1][b - 1];
int adj = max(0ll, a - pref[0][j]) + max(0ll, c - pref[2][j]);
int val = max(0ll, j + adj - i);
dp[i][a][b][1] = min(dp[i][a][b][1], dp[i - 1][a][b - 1][last] + val);
}if (last != 2 && c) {
int j = g[2][c - 1];
int adj = max(0ll, b - pref[1][j]) + max(0ll, a - pref[0][j]);
int val = max(0ll, j + adj - i);
dp[i][a][b][2] = min(dp[i][a][b][2], dp[i - 1][a][b][last] + val);
}
}
// if (a > 0) {
// int pos = g[0][a - 1];
// int cnt = max(0LL, b - pref[{1, pos}]) + max(0LL, c - pref[{2, pos}]);
// dp[i][a][b][0] = min(dp[i][a][b][0], min(dp[i - 1][a - 1][b][1], dp[i - 1][a - 1][b][2]) + max(0LL, pos + cnt - i));
// }
// if (b > 0) {
// int pos = g[1][b - 1];
// int cnt = max(0LL, a - pref[{0, pos}]) + max(0LL, c - pref[{2, pos}]);
// dp[i][a][b][1] = min(dp[i][a][b][1], min(dp[i - 1][a][b - 1][0], dp[i - 1][a][b - 1][2]) + max(0LL, pos + cnt - i));
// }
// if (c > 0) {
// int pos = g[2][c - 1];
// int cnt = max(0LL, b - pref[{1, pos}]) + max(0LL, a - pref[{0, pos}]);
// dp[i][a][b][2] = min(dp[i][a][b][2], min(dp[i - 1][a][b][0], dp[i - 1][a][b][1]) + max(0LL, pos + cnt - i));
// }
}
}
}
// for (int i = 1; i <= n; i++) {
// for (int a = 0; a <= cntr; a++) {
// for (int b = 0; b <= cnty; b++) {
// if (a + b > i) continue;
// int c = i - a - b;
// if (c > cntg) continue;
// cout << dp[i][]
// }
// }
// }
int res = min({dp[n][cntr][cnty][0], dp[n][cntr][cnty][1], dp[n][cntr][cnty][2]});
cout << (res == 1e18 ? -1 : res);
}
Compilation message
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:36:13: warning: unused variable 'R' [-Wunused-variable]
36 | int R = 0, G = 1, Y = 2;
| ^
joi2019_ho_t3.cpp:36:20: warning: unused variable 'G' [-Wunused-variable]
36 | int R = 0, G = 1, Y = 2;
| ^
joi2019_ho_t3.cpp:36:27: warning: unused variable 'Y' [-Wunused-variable]
36 | int R = 0, G = 1, Y = 2;
| ^
joi2019_ho_t3.cpp: In function 'void fReopen()':
joi2019_ho_t3.cpp:19:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
19 | freopen("input.txt", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:20:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
20 | freopen("output.txt", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
412 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
412 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
1116 KB |
Output is correct |
19 |
Correct |
1 ms |
1116 KB |
Output is correct |
20 |
Correct |
1 ms |
1116 KB |
Output is correct |
21 |
Correct |
1 ms |
860 KB |
Output is correct |
22 |
Correct |
2 ms |
1384 KB |
Output is correct |
23 |
Correct |
1 ms |
860 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
860 KB |
Output is correct |
28 |
Correct |
1 ms |
860 KB |
Output is correct |
29 |
Correct |
1 ms |
1116 KB |
Output is correct |
30 |
Correct |
1 ms |
972 KB |
Output is correct |
31 |
Correct |
1 ms |
856 KB |
Output is correct |
32 |
Correct |
1 ms |
856 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
1 ms |
604 KB |
Output is correct |
35 |
Correct |
1 ms |
860 KB |
Output is correct |
36 |
Correct |
1 ms |
1116 KB |
Output is correct |
37 |
Correct |
1 ms |
860 KB |
Output is correct |
38 |
Correct |
0 ms |
860 KB |
Output is correct |
39 |
Correct |
1 ms |
860 KB |
Output is correct |
40 |
Correct |
0 ms |
348 KB |
Output is correct |
41 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
2 ms |
2332 KB |
Output is correct |
3 |
Correct |
2 ms |
2180 KB |
Output is correct |
4 |
Correct |
2 ms |
2140 KB |
Output is correct |
5 |
Correct |
3 ms |
2140 KB |
Output is correct |
6 |
Correct |
2 ms |
2364 KB |
Output is correct |
7 |
Correct |
3 ms |
2136 KB |
Output is correct |
8 |
Correct |
2 ms |
2136 KB |
Output is correct |
9 |
Correct |
2 ms |
2140 KB |
Output is correct |
10 |
Correct |
2 ms |
2136 KB |
Output is correct |
11 |
Correct |
3 ms |
2140 KB |
Output is correct |
12 |
Correct |
1 ms |
860 KB |
Output is correct |
13 |
Correct |
1 ms |
1116 KB |
Output is correct |
14 |
Correct |
1 ms |
1628 KB |
Output is correct |
15 |
Correct |
2 ms |
2140 KB |
Output is correct |
16 |
Correct |
2 ms |
2140 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
412 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
1116 KB |
Output is correct |
19 |
Correct |
1 ms |
1116 KB |
Output is correct |
20 |
Correct |
1 ms |
1116 KB |
Output is correct |
21 |
Correct |
1 ms |
860 KB |
Output is correct |
22 |
Correct |
2 ms |
1384 KB |
Output is correct |
23 |
Correct |
1 ms |
860 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
860 KB |
Output is correct |
28 |
Correct |
1 ms |
860 KB |
Output is correct |
29 |
Correct |
1 ms |
1116 KB |
Output is correct |
30 |
Correct |
1 ms |
972 KB |
Output is correct |
31 |
Correct |
1 ms |
856 KB |
Output is correct |
32 |
Correct |
1 ms |
856 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
1 ms |
604 KB |
Output is correct |
35 |
Correct |
1 ms |
860 KB |
Output is correct |
36 |
Correct |
1 ms |
1116 KB |
Output is correct |
37 |
Correct |
1 ms |
860 KB |
Output is correct |
38 |
Correct |
0 ms |
860 KB |
Output is correct |
39 |
Correct |
1 ms |
860 KB |
Output is correct |
40 |
Correct |
0 ms |
348 KB |
Output is correct |
41 |
Correct |
0 ms |
348 KB |
Output is correct |
42 |
Correct |
0 ms |
348 KB |
Output is correct |
43 |
Correct |
2 ms |
2332 KB |
Output is correct |
44 |
Correct |
2 ms |
2180 KB |
Output is correct |
45 |
Correct |
2 ms |
2140 KB |
Output is correct |
46 |
Correct |
3 ms |
2140 KB |
Output is correct |
47 |
Correct |
2 ms |
2364 KB |
Output is correct |
48 |
Correct |
3 ms |
2136 KB |
Output is correct |
49 |
Correct |
2 ms |
2136 KB |
Output is correct |
50 |
Correct |
2 ms |
2140 KB |
Output is correct |
51 |
Correct |
2 ms |
2136 KB |
Output is correct |
52 |
Correct |
3 ms |
2140 KB |
Output is correct |
53 |
Correct |
1 ms |
860 KB |
Output is correct |
54 |
Correct |
1 ms |
1116 KB |
Output is correct |
55 |
Correct |
1 ms |
1628 KB |
Output is correct |
56 |
Correct |
2 ms |
2140 KB |
Output is correct |
57 |
Correct |
2 ms |
2140 KB |
Output is correct |
58 |
Correct |
149 ms |
176724 KB |
Output is correct |
59 |
Correct |
134 ms |
162896 KB |
Output is correct |
60 |
Correct |
155 ms |
174420 KB |
Output is correct |
61 |
Correct |
147 ms |
171604 KB |
Output is correct |
62 |
Correct |
9 ms |
10588 KB |
Output is correct |
63 |
Correct |
15 ms |
16904 KB |
Output is correct |
64 |
Correct |
55 ms |
62416 KB |
Output is correct |
65 |
Correct |
80 ms |
87380 KB |
Output is correct |
66 |
Correct |
156 ms |
169516 KB |
Output is correct |
67 |
Correct |
133 ms |
146004 KB |
Output is correct |
68 |
Correct |
149 ms |
167548 KB |
Output is correct |
69 |
Correct |
147 ms |
175700 KB |
Output is correct |
70 |
Correct |
149 ms |
160536 KB |
Output is correct |
71 |
Correct |
161 ms |
193192 KB |
Output is correct |
72 |
Correct |
150 ms |
204596 KB |
Output is correct |
73 |
Correct |
12 ms |
13916 KB |
Output is correct |