#include <bits/stdc++.h>
using namespace std;
const int maxn = 410;
const int inf = 1e9+10;
int n;
int a[maxn];
int qtd[3], pos[3][maxn];
int pref[3][maxn];
int dp[3][maxn][maxn][maxn];
int main(void)
{
scanf("%d", &n);
string s;
cin >> s;
for (int i = 0; i < s.size(); i++)
{
if (s[i] == 'R')
{
a[i+1] = 0, ++qtd[0];
pos[0][qtd[0]] = i+1;
}
else if (s[i] == 'G')
{
a[i+1] = 1, ++qtd[1];
pos[1][qtd[1]] = i+1;
}
else
{
a[i+1] = 2, ++qtd[2];
pos[2][qtd[2]] = i+1;
}
}
for (int t = 0; t < 3; t++)
{
for (int i = 1; i <= n; i++)
{
pref[t][i] = pref[t][i-1];
if (a[i] == t) ++pref[t][i];
}
}
for (int i = 0; i < 3; i++)
for (int r = 0; r <= qtd[0]; r++)
for (int g = 0; g <= qtd[1]; g++)
for (int y = 0; y <= qtd[2]; y++)
dp[i][r][g][y] = inf;
dp[0][0][0][0] = 0;
dp[1][0][0][0] = 0;
dp[2][0][0][0] = 0;
for (int r = 0; r <= qtd[0]; r++)
{
for (int g = 0; g <= qtd[1]; g++)
{
for (int y = 0; y <= qtd[2]; y++)
{
for (int pre = 0; pre < 3; pre++)
{
for (int at = 0; at < 3; at++)
{
if (at == pre) continue;
if ((at == 0 && r == qtd[0]) || (at == 1 && g == qtd[1]) || (at == 2 && y == qtd[2])) continue;
int ans = 0;
int p1, p2, p3, dif;
if (pre == 0)
{
p1 = pos[0][r];
if (at == 1) p2 = pos[1][g+1], p3 = pos[2][y], dif = 2;
else p2 = pos[2][y+1], p3 = pos[1][g], dif = 1;
}
else if (pre == 1)
{
p1 = pos[1][g];
if (at == 0) p2 = pos[0][r+1], p3 = pos[2][y], dif = 2;
else p2 = pos[2][y+1], p3 = pos[0][r], dif = 0;
}
else
{
p1 = pos[2][y];
if (at == 0) p2 = pos[0][r+1], p3 = pos[1][g], dif = 1;
else p2 = pos[1][g+1], p3 = pos[0][r], dif = 0;
}
ans += max(0, pref[pre][p2]-pref[pre][p1]);
ans += max(0, pref[dif][p2]-pref[dif][p3]);
ans += dp[pre][r][g][y];
if (at == 0)
dp[at][r+1][g][y] = min(dp[at][r+1][g][y], ans);
else if (at == 1)
dp[at][r][g+1][y] = min(dp[at][r][g+1][y], ans);
else
dp[at][r][g][y+1] = min(dp[at][r][g][y+1], ans);
}
}
}
}
}
int ans = inf;
for (int i = 0; i < 3; i++)
ans = min(ans, dp[i][qtd[0]][qtd[1]][qtd[2]]);
if (ans == inf)
printf("-1\n");
else
printf("%d\n", ans);
}
Compilation message
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:23:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < s.size(); i++)
~~^~~~~~~~~~
joi2019_ho_t3.cpp:18:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &n);
~~~~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
512 KB |
Output is correct |
5 |
Correct |
2 ms |
512 KB |
Output is correct |
6 |
Correct |
2 ms |
640 KB |
Output is correct |
7 |
Correct |
2 ms |
640 KB |
Output is correct |
8 |
Correct |
2 ms |
640 KB |
Output is correct |
9 |
Correct |
2 ms |
768 KB |
Output is correct |
10 |
Correct |
3 ms |
896 KB |
Output is correct |
11 |
Correct |
2 ms |
640 KB |
Output is correct |
12 |
Correct |
2 ms |
512 KB |
Output is correct |
13 |
Correct |
2 ms |
512 KB |
Output is correct |
14 |
Correct |
2 ms |
640 KB |
Output is correct |
15 |
Correct |
2 ms |
512 KB |
Output is correct |
16 |
Correct |
2 ms |
512 KB |
Output is correct |
17 |
Correct |
2 ms |
256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
512 KB |
Output is correct |
5 |
Correct |
2 ms |
512 KB |
Output is correct |
6 |
Correct |
2 ms |
640 KB |
Output is correct |
7 |
Correct |
2 ms |
640 KB |
Output is correct |
8 |
Correct |
2 ms |
640 KB |
Output is correct |
9 |
Correct |
2 ms |
768 KB |
Output is correct |
10 |
Correct |
3 ms |
896 KB |
Output is correct |
11 |
Correct |
2 ms |
640 KB |
Output is correct |
12 |
Correct |
2 ms |
512 KB |
Output is correct |
13 |
Correct |
2 ms |
512 KB |
Output is correct |
14 |
Correct |
2 ms |
640 KB |
Output is correct |
15 |
Correct |
2 ms |
512 KB |
Output is correct |
16 |
Correct |
2 ms |
512 KB |
Output is correct |
17 |
Correct |
2 ms |
256 KB |
Output is correct |
18 |
Correct |
4 ms |
2816 KB |
Output is correct |
19 |
Correct |
4 ms |
2304 KB |
Output is correct |
20 |
Correct |
4 ms |
2816 KB |
Output is correct |
21 |
Correct |
4 ms |
2944 KB |
Output is correct |
22 |
Correct |
4 ms |
2048 KB |
Output is correct |
23 |
Correct |
4 ms |
2560 KB |
Output is correct |
24 |
Correct |
4 ms |
1920 KB |
Output is correct |
25 |
Correct |
5 ms |
4992 KB |
Output is correct |
26 |
Correct |
5 ms |
5120 KB |
Output is correct |
27 |
Correct |
5 ms |
3712 KB |
Output is correct |
28 |
Correct |
4 ms |
2816 KB |
Output is correct |
29 |
Correct |
4 ms |
2688 KB |
Output is correct |
30 |
Correct |
4 ms |
2688 KB |
Output is correct |
31 |
Correct |
4 ms |
2304 KB |
Output is correct |
32 |
Correct |
4 ms |
2816 KB |
Output is correct |
33 |
Correct |
5 ms |
4736 KB |
Output is correct |
34 |
Correct |
5 ms |
4352 KB |
Output is correct |
35 |
Correct |
4 ms |
3200 KB |
Output is correct |
36 |
Correct |
4 ms |
2304 KB |
Output is correct |
37 |
Correct |
3 ms |
2048 KB |
Output is correct |
38 |
Correct |
4 ms |
2560 KB |
Output is correct |
39 |
Correct |
4 ms |
2816 KB |
Output is correct |
40 |
Correct |
2 ms |
384 KB |
Output is correct |
41 |
Correct |
3 ms |
2688 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
136 ms |
196728 KB |
Output is correct |
3 |
Correct |
137 ms |
195636 KB |
Output is correct |
4 |
Correct |
136 ms |
196728 KB |
Output is correct |
5 |
Correct |
134 ms |
196624 KB |
Output is correct |
6 |
Correct |
137 ms |
196620 KB |
Output is correct |
7 |
Correct |
139 ms |
195732 KB |
Output is correct |
8 |
Correct |
132 ms |
195652 KB |
Output is correct |
9 |
Correct |
135 ms |
194804 KB |
Output is correct |
10 |
Correct |
132 ms |
196652 KB |
Output is correct |
11 |
Correct |
148 ms |
196728 KB |
Output is correct |
12 |
Correct |
40 ms |
53424 KB |
Output is correct |
13 |
Correct |
65 ms |
93304 KB |
Output is correct |
14 |
Correct |
94 ms |
134520 KB |
Output is correct |
15 |
Correct |
131 ms |
196600 KB |
Output is correct |
16 |
Correct |
135 ms |
196632 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
512 KB |
Output is correct |
5 |
Correct |
2 ms |
512 KB |
Output is correct |
6 |
Correct |
2 ms |
640 KB |
Output is correct |
7 |
Correct |
2 ms |
640 KB |
Output is correct |
8 |
Correct |
2 ms |
640 KB |
Output is correct |
9 |
Correct |
2 ms |
768 KB |
Output is correct |
10 |
Correct |
3 ms |
896 KB |
Output is correct |
11 |
Correct |
2 ms |
640 KB |
Output is correct |
12 |
Correct |
2 ms |
512 KB |
Output is correct |
13 |
Correct |
2 ms |
512 KB |
Output is correct |
14 |
Correct |
2 ms |
640 KB |
Output is correct |
15 |
Correct |
2 ms |
512 KB |
Output is correct |
16 |
Correct |
2 ms |
512 KB |
Output is correct |
17 |
Correct |
2 ms |
256 KB |
Output is correct |
18 |
Correct |
4 ms |
2816 KB |
Output is correct |
19 |
Correct |
4 ms |
2304 KB |
Output is correct |
20 |
Correct |
4 ms |
2816 KB |
Output is correct |
21 |
Correct |
4 ms |
2944 KB |
Output is correct |
22 |
Correct |
4 ms |
2048 KB |
Output is correct |
23 |
Correct |
4 ms |
2560 KB |
Output is correct |
24 |
Correct |
4 ms |
1920 KB |
Output is correct |
25 |
Correct |
5 ms |
4992 KB |
Output is correct |
26 |
Correct |
5 ms |
5120 KB |
Output is correct |
27 |
Correct |
5 ms |
3712 KB |
Output is correct |
28 |
Correct |
4 ms |
2816 KB |
Output is correct |
29 |
Correct |
4 ms |
2688 KB |
Output is correct |
30 |
Correct |
4 ms |
2688 KB |
Output is correct |
31 |
Correct |
4 ms |
2304 KB |
Output is correct |
32 |
Correct |
4 ms |
2816 KB |
Output is correct |
33 |
Correct |
5 ms |
4736 KB |
Output is correct |
34 |
Correct |
5 ms |
4352 KB |
Output is correct |
35 |
Correct |
4 ms |
3200 KB |
Output is correct |
36 |
Correct |
4 ms |
2304 KB |
Output is correct |
37 |
Correct |
3 ms |
2048 KB |
Output is correct |
38 |
Correct |
4 ms |
2560 KB |
Output is correct |
39 |
Correct |
4 ms |
2816 KB |
Output is correct |
40 |
Correct |
2 ms |
384 KB |
Output is correct |
41 |
Correct |
3 ms |
2688 KB |
Output is correct |
42 |
Correct |
2 ms |
384 KB |
Output is correct |
43 |
Correct |
136 ms |
196728 KB |
Output is correct |
44 |
Correct |
137 ms |
195636 KB |
Output is correct |
45 |
Correct |
136 ms |
196728 KB |
Output is correct |
46 |
Correct |
134 ms |
196624 KB |
Output is correct |
47 |
Correct |
137 ms |
196620 KB |
Output is correct |
48 |
Correct |
139 ms |
195732 KB |
Output is correct |
49 |
Correct |
132 ms |
195652 KB |
Output is correct |
50 |
Correct |
135 ms |
194804 KB |
Output is correct |
51 |
Correct |
132 ms |
196652 KB |
Output is correct |
52 |
Correct |
148 ms |
196728 KB |
Output is correct |
53 |
Correct |
40 ms |
53424 KB |
Output is correct |
54 |
Correct |
65 ms |
93304 KB |
Output is correct |
55 |
Correct |
94 ms |
134520 KB |
Output is correct |
56 |
Correct |
131 ms |
196600 KB |
Output is correct |
57 |
Correct |
135 ms |
196632 KB |
Output is correct |
58 |
Correct |
170 ms |
78200 KB |
Output is correct |
59 |
Correct |
174 ms |
99064 KB |
Output is correct |
60 |
Correct |
170 ms |
90124 KB |
Output is correct |
61 |
Correct |
170 ms |
87160 KB |
Output is correct |
62 |
Correct |
142 ms |
191740 KB |
Output is correct |
63 |
Correct |
147 ms |
188964 KB |
Output is correct |
64 |
Correct |
176 ms |
165140 KB |
Output is correct |
65 |
Correct |
186 ms |
143692 KB |
Output is correct |
66 |
Correct |
168 ms |
88320 KB |
Output is correct |
67 |
Correct |
170 ms |
84856 KB |
Output is correct |
68 |
Correct |
172 ms |
91004 KB |
Output is correct |
69 |
Correct |
175 ms |
87800 KB |
Output is correct |
70 |
Correct |
175 ms |
94904 KB |
Output is correct |
71 |
Correct |
167 ms |
86264 KB |
Output is correct |
72 |
Correct |
65 ms |
44508 KB |
Output is correct |
73 |
Correct |
11 ms |
2944 KB |
Output is correct |