#include <bits/stdc++.h>
using namespace std;
#define fileio() freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout)
#define fastio() cin.tie(0), ios_base::sync_with_stdio(0)
#define pb push_back
#define pii pair<int, int>
#define st first
#define nd second
#define endl "\n"
#define sp " "
#define N 405
int dp[N][N][N][4];
vector<array<int, 3>> r, g, y;
int n;
const int INF = 1e9 + 7;
int f(int i, int j, int k, int last)
{
int t1 = INF, t2 = INF, t3 = INF;
int pos = i + j + k + 1;
if (pos > n) return 0;
if (dp[i][j][k][last] != -1) return dp[i][j][k][last];
if (i < r.size() && last != 0) t1 = f(i + 1, j, k, 0) + max(0, j - r[i][1]) + max(0, k - r[i][2]);
if (j < g.size() && last != 1) t2 = f(i, j + 1, k, 1) + max(0, i - g[j][0]) + max(0, k - g[j][2]);
if (k < y.size() && last != 2) t3 = f(i, j, k + 1, 2) + max(0, i - y[k][0]) + max(0, j - y[k][1]);
int res = min(t1, t2);
res = min(res, t3);
res = min(res, INF);
return dp[i][j][k][last] = res;
}
void print(int i ,int j, int k, int last)
{
int t1 = INF, t2 = INF, t3 = INF;
int pos = i + j + k + 1;
if (pos > n) return;
if (i < r.size() && last != 0) t1 = f(i + 1, j, k, 0) + max(0, j - r[i][1]) + max(0, k - r[i][2]);
if (j < g.size() && last != 1) t2 = f(i, j + 1, k, 1) + max(0, i - g[j][0]) + max(0, k - g[j][2]);
if (k < y.size() && last != 2) t3 = f(i, j, k + 1, 2) + max(0, i - y[k][0]) + max(0, j - y[k][1]);
if (t1 == f(i, j, k, last))
{
cout<<0;
print(i + 1, j, k, 0);
return;
}
if (t2 == f(i, j, k, last))
{
cout<<1;
print(i, j + 1, k, 1);
return;
}
cout<<2;
print(i, j, k + 1, 2);
}
int32_t main()
{
fastio();
cin>>n;
for (int i = 1; i <= n; i++)
{
char tmp;
cin>>tmp;
if (tmp == 'R') r.pb({(int)r.size(), (int)g.size(), (int)y.size()});
else if (tmp == 'G') g.pb({(int)r.size(), (int)g.size(), (int)y.size()});
else y.pb({(int)r.size(), (int)g.size(), (int)y.size()});
}
for (int i = 0; i <= r.size() + 5; i++)
{
for (int j = 0; j <= g.size() + 5; j++)
{
for (int k = 0; k <= y.size() + 5; k++)
{
for (int last = 0; last <= 3; last++) dp[i][j][k][last] = -1;
}
}
}
int ans = f(0, 0, 0, 3);
if (ans >= INF) cout<<-1<<endl;
else cout<<ans<<endl;
//print(0, 0, 0, 3);
cerr<<"time taken : "<<(float)clock() / CLOCKS_PER_SEC<<" seconds\n";
}
Compilation message
joi2019_ho_t3.cpp: In function 'int f(int, int, int, int)':
joi2019_ho_t3.cpp:26:8: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<int, 3> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
26 | if (i < r.size() && last != 0) t1 = f(i + 1, j, k, 0) + max(0, j - r[i][1]) + max(0, k - r[i][2]);
| ~~^~~~~~~~~~
joi2019_ho_t3.cpp:27:8: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<int, 3> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
27 | if (j < g.size() && last != 1) t2 = f(i, j + 1, k, 1) + max(0, i - g[j][0]) + max(0, k - g[j][2]);
| ~~^~~~~~~~~~
joi2019_ho_t3.cpp:28:8: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<int, 3> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
28 | if (k < y.size() && last != 2) t3 = f(i, j, k + 1, 2) + max(0, i - y[k][0]) + max(0, j - y[k][1]);
| ~~^~~~~~~~~~
joi2019_ho_t3.cpp: In function 'void print(int, int, int, int)':
joi2019_ho_t3.cpp:42:8: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<int, 3> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
42 | if (i < r.size() && last != 0) t1 = f(i + 1, j, k, 0) + max(0, j - r[i][1]) + max(0, k - r[i][2]);
| ~~^~~~~~~~~~
joi2019_ho_t3.cpp:43:8: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<int, 3> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
43 | if (j < g.size() && last != 1) t2 = f(i, j + 1, k, 1) + max(0, i - g[j][0]) + max(0, k - g[j][2]);
| ~~^~~~~~~~~~
joi2019_ho_t3.cpp:44:8: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<int, 3> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
44 | if (k < y.size() && last != 2) t3 = f(i, j, k + 1, 2) + max(0, i - y[k][0]) + max(0, j - y[k][1]);
| ~~^~~~~~~~~~
joi2019_ho_t3.cpp:39:26: warning: variable 't3' set but not used [-Wunused-but-set-variable]
39 | int t1 = INF, t2 = INF, t3 = INF;
| ^~
joi2019_ho_t3.cpp: In function 'int32_t main()':
joi2019_ho_t3.cpp:75:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<int, 3> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
75 | for (int i = 0; i <= r.size() + 5; i++)
| ~~^~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:77:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<int, 3> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
77 | for (int j = 0; j <= g.size() + 5; j++)
| ~~^~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:79:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<int, 3> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
79 | for (int k = 0; k <= y.size() + 5; k++)
| ~~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
588 KB |
Output is correct |
5 |
Correct |
1 ms |
724 KB |
Output is correct |
6 |
Correct |
1 ms |
844 KB |
Output is correct |
7 |
Correct |
1 ms |
840 KB |
Output is correct |
8 |
Correct |
1 ms |
840 KB |
Output is correct |
9 |
Correct |
1 ms |
972 KB |
Output is correct |
10 |
Correct |
1 ms |
1104 KB |
Output is correct |
11 |
Correct |
1 ms |
852 KB |
Output is correct |
12 |
Correct |
1 ms |
852 KB |
Output is correct |
13 |
Correct |
1 ms |
592 KB |
Output is correct |
14 |
Correct |
1 ms |
852 KB |
Output is correct |
15 |
Correct |
1 ms |
724 KB |
Output is correct |
16 |
Correct |
1 ms |
840 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
588 KB |
Output is correct |
5 |
Correct |
1 ms |
724 KB |
Output is correct |
6 |
Correct |
1 ms |
844 KB |
Output is correct |
7 |
Correct |
1 ms |
840 KB |
Output is correct |
8 |
Correct |
1 ms |
840 KB |
Output is correct |
9 |
Correct |
1 ms |
972 KB |
Output is correct |
10 |
Correct |
1 ms |
1104 KB |
Output is correct |
11 |
Correct |
1 ms |
852 KB |
Output is correct |
12 |
Correct |
1 ms |
852 KB |
Output is correct |
13 |
Correct |
1 ms |
592 KB |
Output is correct |
14 |
Correct |
1 ms |
852 KB |
Output is correct |
15 |
Correct |
1 ms |
724 KB |
Output is correct |
16 |
Correct |
1 ms |
840 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
2 ms |
3404 KB |
Output is correct |
19 |
Correct |
2 ms |
2872 KB |
Output is correct |
20 |
Correct |
2 ms |
3412 KB |
Output is correct |
21 |
Correct |
2 ms |
3540 KB |
Output is correct |
22 |
Correct |
1 ms |
2644 KB |
Output is correct |
23 |
Correct |
2 ms |
3156 KB |
Output is correct |
24 |
Correct |
2 ms |
2644 KB |
Output is correct |
25 |
Correct |
2 ms |
5460 KB |
Output is correct |
26 |
Correct |
2 ms |
5588 KB |
Output is correct |
27 |
Correct |
2 ms |
4296 KB |
Output is correct |
28 |
Correct |
2 ms |
3412 KB |
Output is correct |
29 |
Correct |
2 ms |
3284 KB |
Output is correct |
30 |
Correct |
2 ms |
3412 KB |
Output is correct |
31 |
Correct |
1 ms |
3028 KB |
Output is correct |
32 |
Correct |
2 ms |
3540 KB |
Output is correct |
33 |
Correct |
2 ms |
5204 KB |
Output is correct |
34 |
Correct |
2 ms |
4948 KB |
Output is correct |
35 |
Correct |
2 ms |
3924 KB |
Output is correct |
36 |
Correct |
2 ms |
2896 KB |
Output is correct |
37 |
Correct |
1 ms |
2764 KB |
Output is correct |
38 |
Correct |
2 ms |
3284 KB |
Output is correct |
39 |
Correct |
2 ms |
3408 KB |
Output is correct |
40 |
Correct |
1 ms |
980 KB |
Output is correct |
41 |
Correct |
2 ms |
3532 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
65 ms |
174436 KB |
Output is correct |
3 |
Correct |
61 ms |
173500 KB |
Output is correct |
4 |
Correct |
65 ms |
174364 KB |
Output is correct |
5 |
Correct |
70 ms |
174336 KB |
Output is correct |
6 |
Correct |
62 ms |
174400 KB |
Output is correct |
7 |
Correct |
63 ms |
173480 KB |
Output is correct |
8 |
Correct |
69 ms |
173516 KB |
Output is correct |
9 |
Correct |
68 ms |
172736 KB |
Output is correct |
10 |
Correct |
65 ms |
174408 KB |
Output is correct |
11 |
Correct |
62 ms |
174316 KB |
Output is correct |
12 |
Correct |
20 ms |
49236 KB |
Output is correct |
13 |
Correct |
32 ms |
84408 KB |
Output is correct |
14 |
Correct |
43 ms |
120400 KB |
Output is correct |
15 |
Correct |
62 ms |
174392 KB |
Output is correct |
16 |
Correct |
61 ms |
174364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
588 KB |
Output is correct |
5 |
Correct |
1 ms |
724 KB |
Output is correct |
6 |
Correct |
1 ms |
844 KB |
Output is correct |
7 |
Correct |
1 ms |
840 KB |
Output is correct |
8 |
Correct |
1 ms |
840 KB |
Output is correct |
9 |
Correct |
1 ms |
972 KB |
Output is correct |
10 |
Correct |
1 ms |
1104 KB |
Output is correct |
11 |
Correct |
1 ms |
852 KB |
Output is correct |
12 |
Correct |
1 ms |
852 KB |
Output is correct |
13 |
Correct |
1 ms |
592 KB |
Output is correct |
14 |
Correct |
1 ms |
852 KB |
Output is correct |
15 |
Correct |
1 ms |
724 KB |
Output is correct |
16 |
Correct |
1 ms |
840 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
2 ms |
3404 KB |
Output is correct |
19 |
Correct |
2 ms |
2872 KB |
Output is correct |
20 |
Correct |
2 ms |
3412 KB |
Output is correct |
21 |
Correct |
2 ms |
3540 KB |
Output is correct |
22 |
Correct |
1 ms |
2644 KB |
Output is correct |
23 |
Correct |
2 ms |
3156 KB |
Output is correct |
24 |
Correct |
2 ms |
2644 KB |
Output is correct |
25 |
Correct |
2 ms |
5460 KB |
Output is correct |
26 |
Correct |
2 ms |
5588 KB |
Output is correct |
27 |
Correct |
2 ms |
4296 KB |
Output is correct |
28 |
Correct |
2 ms |
3412 KB |
Output is correct |
29 |
Correct |
2 ms |
3284 KB |
Output is correct |
30 |
Correct |
2 ms |
3412 KB |
Output is correct |
31 |
Correct |
1 ms |
3028 KB |
Output is correct |
32 |
Correct |
2 ms |
3540 KB |
Output is correct |
33 |
Correct |
2 ms |
5204 KB |
Output is correct |
34 |
Correct |
2 ms |
4948 KB |
Output is correct |
35 |
Correct |
2 ms |
3924 KB |
Output is correct |
36 |
Correct |
2 ms |
2896 KB |
Output is correct |
37 |
Correct |
1 ms |
2764 KB |
Output is correct |
38 |
Correct |
2 ms |
3284 KB |
Output is correct |
39 |
Correct |
2 ms |
3408 KB |
Output is correct |
40 |
Correct |
1 ms |
980 KB |
Output is correct |
41 |
Correct |
2 ms |
3532 KB |
Output is correct |
42 |
Correct |
1 ms |
468 KB |
Output is correct |
43 |
Correct |
65 ms |
174436 KB |
Output is correct |
44 |
Correct |
61 ms |
173500 KB |
Output is correct |
45 |
Correct |
65 ms |
174364 KB |
Output is correct |
46 |
Correct |
70 ms |
174336 KB |
Output is correct |
47 |
Correct |
62 ms |
174400 KB |
Output is correct |
48 |
Correct |
63 ms |
173480 KB |
Output is correct |
49 |
Correct |
69 ms |
173516 KB |
Output is correct |
50 |
Correct |
68 ms |
172736 KB |
Output is correct |
51 |
Correct |
65 ms |
174408 KB |
Output is correct |
52 |
Correct |
62 ms |
174316 KB |
Output is correct |
53 |
Correct |
20 ms |
49236 KB |
Output is correct |
54 |
Correct |
32 ms |
84408 KB |
Output is correct |
55 |
Correct |
43 ms |
120400 KB |
Output is correct |
56 |
Correct |
62 ms |
174392 KB |
Output is correct |
57 |
Correct |
61 ms |
174364 KB |
Output is correct |
58 |
Correct |
149 ms |
109520 KB |
Output is correct |
59 |
Correct |
136 ms |
129004 KB |
Output is correct |
60 |
Correct |
140 ms |
121000 KB |
Output is correct |
61 |
Correct |
137 ms |
118828 KB |
Output is correct |
62 |
Correct |
62 ms |
172620 KB |
Output is correct |
63 |
Correct |
62 ms |
172744 KB |
Output is correct |
64 |
Correct |
75 ms |
165884 KB |
Output is correct |
65 |
Correct |
90 ms |
156124 KB |
Output is correct |
66 |
Correct |
134 ms |
119344 KB |
Output is correct |
67 |
Correct |
129 ms |
116584 KB |
Output is correct |
68 |
Correct |
135 ms |
122188 KB |
Output is correct |
69 |
Correct |
142 ms |
119704 KB |
Output is correct |
70 |
Correct |
135 ms |
125648 KB |
Output is correct |
71 |
Correct |
130 ms |
117396 KB |
Output is correct |
72 |
Correct |
26 ms |
54236 KB |
Output is correct |
73 |
Correct |
4 ms |
7492 KB |
Output is correct |