#include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define mp make_pair
#define eb emplace_back
#define pb push_back
#define pii pair<int,int>
#define X first
#define Y second
#define all(x) x.begin(), x.end()
void abc() {cout << endl;}
template <typename T, typename ...U> void abc(T i, U ...j) {
cout << i << ' ', abc(j...);
}
template <typename T> void printv(T l, T r) {
for (; l != r; ++l)
cout << *l << " \n"[l + 1 == r];
}
#ifdef Doludu
#define test(x...) abc("[" + string(#x) + "]", x)
#define owo freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout)
#else
#define test(x...) void(0)
#define owo ios::sync_with_stdio(false), cin.tie(0)
#endif
const int N = 401;
int dp[N][N][N][3]; // len, Y num, R num, lst 0Y 1R 2G
int main () {
owo;
int n;
string s;
cin >> n >> s;
int bound = n + 1 >> 1;
int r_all = count(all(s), 'R'), y_all = count(all(s), 'Y'), g_all = count(all(s), 'G');
if (r_all > bound || y_all > bound || g_all > bound)
return cout << -1 << '\n', 0;
for (int i = 0; i <= n; ++i) for (int j = 0; j <= n; ++j) for (int k = 0; k <= n; ++k)
dp[i][j][k][0] = dp[i][j][k][1] = dp[i][j][k][2] = 1 << 30;
dp[0][0][0][0] = dp[0][0][0][1] = 0;
for (int len = 0; len < n; ++len) for (int y_num = 0; y_num <= len; y_num++) for (int r_num = 0; r_num + y_num <= len; ++r_num) {
int g_num = len - y_num - r_num;
if (y_num > y_all || r_num > r_all || g_num > g_all)
continue;
int y_cnt = y_num, r_cnt = r_num, g_cnt = g_num;
int y_nxt = -1, r_nxt = -1, g_nxt = -1, tmp = 0;
for (char c : s) {
if (c == 'Y') {
if (y_cnt)
y_cnt--;
else {
if (y_nxt == -1)
y_nxt = tmp;
tmp++;
}
} else if (c == 'R') {
if (r_cnt)
r_cnt--;
else {
if (r_nxt == -1)
r_nxt = tmp;
tmp++;
}
} else {
if (g_cnt)
g_cnt--;
else {
if (g_nxt == -1)
g_nxt = tmp;
tmp++;
}
}
for (int lst = 0; lst < 3; ++lst) {
if (y_nxt != -1 && lst != 0)
dp[len + 1][y_num + 1][r_num][0] = min(dp[len + 1][y_num + 1][r_num][0], dp[len][y_num][r_num][lst] + y_nxt);
if (r_nxt != -1 && lst != 1)
dp[len + 1][y_num][r_num + 1][1] = min(dp[len + 1][y_num][r_num + 1][1], dp[len][y_num][r_num][lst] + r_nxt);
if (g_nxt != -1 && lst != 2)
dp[len + 1][y_num][r_num][2] = min(dp[len + 1][y_num][r_num][2], dp[len][y_num][r_num][lst] + g_nxt);
}
}
}
cout << min({dp[n][y_all][r_all][0], dp[n][y_all][r_all][1], dp[n][y_all][r_all][2]}) << '\n';
}
Compilation message
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:35:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
35 | int bound = n + 1 >> 1;
| ~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
844 KB |
Output is correct |
5 |
Correct |
1 ms |
1356 KB |
Output is correct |
6 |
Correct |
1 ms |
1356 KB |
Output is correct |
7 |
Correct |
1 ms |
1356 KB |
Output is correct |
8 |
Correct |
1 ms |
1356 KB |
Output is correct |
9 |
Correct |
1 ms |
1356 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
1356 KB |
Output is correct |
12 |
Correct |
1 ms |
1376 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
1228 KB |
Output is correct |
15 |
Correct |
1 ms |
1356 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
844 KB |
Output is correct |
5 |
Correct |
1 ms |
1356 KB |
Output is correct |
6 |
Correct |
1 ms |
1356 KB |
Output is correct |
7 |
Correct |
1 ms |
1356 KB |
Output is correct |
8 |
Correct |
1 ms |
1356 KB |
Output is correct |
9 |
Correct |
1 ms |
1356 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
1356 KB |
Output is correct |
12 |
Correct |
1 ms |
1376 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
1228 KB |
Output is correct |
15 |
Correct |
1 ms |
1356 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
17 ms |
17996 KB |
Output is correct |
19 |
Correct |
17 ms |
17996 KB |
Output is correct |
20 |
Correct |
15 ms |
17996 KB |
Output is correct |
21 |
Correct |
15 ms |
18020 KB |
Output is correct |
22 |
Correct |
17 ms |
18020 KB |
Output is correct |
23 |
Correct |
15 ms |
18020 KB |
Output is correct |
24 |
Correct |
16 ms |
17996 KB |
Output is correct |
25 |
Correct |
0 ms |
204 KB |
Output is correct |
26 |
Correct |
1 ms |
204 KB |
Output is correct |
27 |
Correct |
14 ms |
17996 KB |
Output is correct |
28 |
Correct |
14 ms |
18020 KB |
Output is correct |
29 |
Correct |
14 ms |
17996 KB |
Output is correct |
30 |
Correct |
26 ms |
18024 KB |
Output is correct |
31 |
Correct |
14 ms |
18000 KB |
Output is correct |
32 |
Correct |
13 ms |
17996 KB |
Output is correct |
33 |
Correct |
0 ms |
204 KB |
Output is correct |
34 |
Correct |
0 ms |
204 KB |
Output is correct |
35 |
Correct |
13 ms |
16716 KB |
Output is correct |
36 |
Correct |
14 ms |
17356 KB |
Output is correct |
37 |
Correct |
14 ms |
16204 KB |
Output is correct |
38 |
Correct |
14 ms |
17944 KB |
Output is correct |
39 |
Correct |
14 ms |
17912 KB |
Output is correct |
40 |
Correct |
0 ms |
204 KB |
Output is correct |
41 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Execution timed out |
528 ms |
757348 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
844 KB |
Output is correct |
5 |
Correct |
1 ms |
1356 KB |
Output is correct |
6 |
Correct |
1 ms |
1356 KB |
Output is correct |
7 |
Correct |
1 ms |
1356 KB |
Output is correct |
8 |
Correct |
1 ms |
1356 KB |
Output is correct |
9 |
Correct |
1 ms |
1356 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
1356 KB |
Output is correct |
12 |
Correct |
1 ms |
1376 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
1228 KB |
Output is correct |
15 |
Correct |
1 ms |
1356 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
17 ms |
17996 KB |
Output is correct |
19 |
Correct |
17 ms |
17996 KB |
Output is correct |
20 |
Correct |
15 ms |
17996 KB |
Output is correct |
21 |
Correct |
15 ms |
18020 KB |
Output is correct |
22 |
Correct |
17 ms |
18020 KB |
Output is correct |
23 |
Correct |
15 ms |
18020 KB |
Output is correct |
24 |
Correct |
16 ms |
17996 KB |
Output is correct |
25 |
Correct |
0 ms |
204 KB |
Output is correct |
26 |
Correct |
1 ms |
204 KB |
Output is correct |
27 |
Correct |
14 ms |
17996 KB |
Output is correct |
28 |
Correct |
14 ms |
18020 KB |
Output is correct |
29 |
Correct |
14 ms |
17996 KB |
Output is correct |
30 |
Correct |
26 ms |
18024 KB |
Output is correct |
31 |
Correct |
14 ms |
18000 KB |
Output is correct |
32 |
Correct |
13 ms |
17996 KB |
Output is correct |
33 |
Correct |
0 ms |
204 KB |
Output is correct |
34 |
Correct |
0 ms |
204 KB |
Output is correct |
35 |
Correct |
13 ms |
16716 KB |
Output is correct |
36 |
Correct |
14 ms |
17356 KB |
Output is correct |
37 |
Correct |
14 ms |
16204 KB |
Output is correct |
38 |
Correct |
14 ms |
17944 KB |
Output is correct |
39 |
Correct |
14 ms |
17912 KB |
Output is correct |
40 |
Correct |
0 ms |
204 KB |
Output is correct |
41 |
Correct |
0 ms |
204 KB |
Output is correct |
42 |
Correct |
1 ms |
332 KB |
Output is correct |
43 |
Execution timed out |
528 ms |
757348 KB |
Time limit exceeded |
44 |
Halted |
0 ms |
0 KB |
- |