#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;
// pos. of color 'pre', pos. of color 'at' and pos. of other color 'dif'
int p1, p2, p3, dif;
// These ifs are used only to find the correct values for p1, p2 and p3
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 of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
23 | 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]
18 | scanf("%d", &n);
| ~~~~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8540 KB |
Output is correct |
2 |
Correct |
1 ms |
8540 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
10588 KB |
Output is correct |
5 |
Correct |
2 ms |
14684 KB |
Output is correct |
6 |
Correct |
2 ms |
16728 KB |
Output is correct |
7 |
Correct |
2 ms |
20828 KB |
Output is correct |
8 |
Correct |
2 ms |
16732 KB |
Output is correct |
9 |
Correct |
2 ms |
16732 KB |
Output is correct |
10 |
Correct |
3 ms |
26972 KB |
Output is correct |
11 |
Correct |
2 ms |
14684 KB |
Output is correct |
12 |
Correct |
2 ms |
14684 KB |
Output is correct |
13 |
Correct |
1 ms |
10584 KB |
Output is correct |
14 |
Correct |
2 ms |
16732 KB |
Output is correct |
15 |
Correct |
2 ms |
14680 KB |
Output is correct |
16 |
Correct |
3 ms |
27120 KB |
Output is correct |
17 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8540 KB |
Output is correct |
2 |
Correct |
1 ms |
8540 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
10588 KB |
Output is correct |
5 |
Correct |
2 ms |
14684 KB |
Output is correct |
6 |
Correct |
2 ms |
16728 KB |
Output is correct |
7 |
Correct |
2 ms |
20828 KB |
Output is correct |
8 |
Correct |
2 ms |
16732 KB |
Output is correct |
9 |
Correct |
2 ms |
16732 KB |
Output is correct |
10 |
Correct |
3 ms |
26972 KB |
Output is correct |
11 |
Correct |
2 ms |
14684 KB |
Output is correct |
12 |
Correct |
2 ms |
14684 KB |
Output is correct |
13 |
Correct |
1 ms |
10584 KB |
Output is correct |
14 |
Correct |
2 ms |
16732 KB |
Output is correct |
15 |
Correct |
2 ms |
14680 KB |
Output is correct |
16 |
Correct |
3 ms |
27120 KB |
Output is correct |
17 |
Correct |
1 ms |
4444 KB |
Output is correct |
18 |
Correct |
7 ms |
51544 KB |
Output is correct |
19 |
Correct |
5 ms |
43356 KB |
Output is correct |
20 |
Correct |
6 ms |
53596 KB |
Output is correct |
21 |
Correct |
5 ms |
45404 KB |
Output is correct |
22 |
Correct |
6 ms |
53596 KB |
Output is correct |
23 |
Correct |
4 ms |
39260 KB |
Output is correct |
24 |
Correct |
3 ms |
26972 KB |
Output is correct |
25 |
Correct |
11 ms |
78428 KB |
Output is correct |
26 |
Correct |
7 ms |
66136 KB |
Output is correct |
27 |
Correct |
6 ms |
59996 KB |
Output is correct |
28 |
Correct |
5 ms |
47452 KB |
Output is correct |
29 |
Correct |
5 ms |
47448 KB |
Output is correct |
30 |
Correct |
4 ms |
39260 KB |
Output is correct |
31 |
Correct |
4 ms |
35164 KB |
Output is correct |
32 |
Correct |
5 ms |
41308 KB |
Output is correct |
33 |
Correct |
6 ms |
53608 KB |
Output is correct |
34 |
Correct |
6 ms |
47452 KB |
Output is correct |
35 |
Correct |
6 ms |
51548 KB |
Output is correct |
36 |
Correct |
5 ms |
45628 KB |
Output is correct |
37 |
Correct |
4 ms |
33116 KB |
Output is correct |
38 |
Correct |
4 ms |
33116 KB |
Output is correct |
39 |
Correct |
5 ms |
43356 KB |
Output is correct |
40 |
Correct |
1 ms |
4444 KB |
Output is correct |
41 |
Correct |
3 ms |
22876 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
10588 KB |
Output is correct |
2 |
Correct |
98 ms |
379052 KB |
Output is correct |
3 |
Correct |
49 ms |
379984 KB |
Output is correct |
4 |
Correct |
49 ms |
379020 KB |
Output is correct |
5 |
Correct |
48 ms |
378896 KB |
Output is correct |
6 |
Correct |
48 ms |
379008 KB |
Output is correct |
7 |
Correct |
47 ms |
377756 KB |
Output is correct |
8 |
Correct |
47 ms |
377680 KB |
Output is correct |
9 |
Correct |
52 ms |
377512 KB |
Output is correct |
10 |
Correct |
48 ms |
380084 KB |
Output is correct |
11 |
Correct |
48 ms |
378964 KB |
Output is correct |
12 |
Correct |
21 ms |
209752 KB |
Output is correct |
13 |
Correct |
30 ms |
275352 KB |
Output is correct |
14 |
Correct |
37 ms |
330836 KB |
Output is correct |
15 |
Correct |
49 ms |
373592 KB |
Output is correct |
16 |
Correct |
50 ms |
374612 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8540 KB |
Output is correct |
2 |
Correct |
1 ms |
8540 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
10588 KB |
Output is correct |
5 |
Correct |
2 ms |
14684 KB |
Output is correct |
6 |
Correct |
2 ms |
16728 KB |
Output is correct |
7 |
Correct |
2 ms |
20828 KB |
Output is correct |
8 |
Correct |
2 ms |
16732 KB |
Output is correct |
9 |
Correct |
2 ms |
16732 KB |
Output is correct |
10 |
Correct |
3 ms |
26972 KB |
Output is correct |
11 |
Correct |
2 ms |
14684 KB |
Output is correct |
12 |
Correct |
2 ms |
14684 KB |
Output is correct |
13 |
Correct |
1 ms |
10584 KB |
Output is correct |
14 |
Correct |
2 ms |
16732 KB |
Output is correct |
15 |
Correct |
2 ms |
14680 KB |
Output is correct |
16 |
Correct |
3 ms |
27120 KB |
Output is correct |
17 |
Correct |
1 ms |
4444 KB |
Output is correct |
18 |
Correct |
7 ms |
51544 KB |
Output is correct |
19 |
Correct |
5 ms |
43356 KB |
Output is correct |
20 |
Correct |
6 ms |
53596 KB |
Output is correct |
21 |
Correct |
5 ms |
45404 KB |
Output is correct |
22 |
Correct |
6 ms |
53596 KB |
Output is correct |
23 |
Correct |
4 ms |
39260 KB |
Output is correct |
24 |
Correct |
3 ms |
26972 KB |
Output is correct |
25 |
Correct |
11 ms |
78428 KB |
Output is correct |
26 |
Correct |
7 ms |
66136 KB |
Output is correct |
27 |
Correct |
6 ms |
59996 KB |
Output is correct |
28 |
Correct |
5 ms |
47452 KB |
Output is correct |
29 |
Correct |
5 ms |
47448 KB |
Output is correct |
30 |
Correct |
4 ms |
39260 KB |
Output is correct |
31 |
Correct |
4 ms |
35164 KB |
Output is correct |
32 |
Correct |
5 ms |
41308 KB |
Output is correct |
33 |
Correct |
6 ms |
53608 KB |
Output is correct |
34 |
Correct |
6 ms |
47452 KB |
Output is correct |
35 |
Correct |
6 ms |
51548 KB |
Output is correct |
36 |
Correct |
5 ms |
45628 KB |
Output is correct |
37 |
Correct |
4 ms |
33116 KB |
Output is correct |
38 |
Correct |
4 ms |
33116 KB |
Output is correct |
39 |
Correct |
5 ms |
43356 KB |
Output is correct |
40 |
Correct |
1 ms |
4444 KB |
Output is correct |
41 |
Correct |
3 ms |
22876 KB |
Output is correct |
42 |
Correct |
1 ms |
10588 KB |
Output is correct |
43 |
Correct |
98 ms |
379052 KB |
Output is correct |
44 |
Correct |
49 ms |
379984 KB |
Output is correct |
45 |
Correct |
49 ms |
379020 KB |
Output is correct |
46 |
Correct |
48 ms |
378896 KB |
Output is correct |
47 |
Correct |
48 ms |
379008 KB |
Output is correct |
48 |
Correct |
47 ms |
377756 KB |
Output is correct |
49 |
Correct |
47 ms |
377680 KB |
Output is correct |
50 |
Correct |
52 ms |
377512 KB |
Output is correct |
51 |
Correct |
48 ms |
380084 KB |
Output is correct |
52 |
Correct |
48 ms |
378964 KB |
Output is correct |
53 |
Correct |
21 ms |
209752 KB |
Output is correct |
54 |
Correct |
30 ms |
275352 KB |
Output is correct |
55 |
Correct |
37 ms |
330836 KB |
Output is correct |
56 |
Correct |
49 ms |
373592 KB |
Output is correct |
57 |
Correct |
50 ms |
374612 KB |
Output is correct |
58 |
Correct |
94 ms |
248896 KB |
Output is correct |
59 |
Correct |
101 ms |
291984 KB |
Output is correct |
60 |
Correct |
105 ms |
285828 KB |
Output is correct |
61 |
Correct |
100 ms |
269392 KB |
Output is correct |
62 |
Correct |
58 ms |
387412 KB |
Output is correct |
63 |
Correct |
58 ms |
372048 KB |
Output is correct |
64 |
Correct |
77 ms |
364660 KB |
Output is correct |
65 |
Correct |
87 ms |
320808 KB |
Output is correct |
66 |
Correct |
99 ms |
275536 KB |
Output is correct |
67 |
Correct |
91 ms |
226324 KB |
Output is correct |
68 |
Correct |
99 ms |
275568 KB |
Output is correct |
69 |
Correct |
102 ms |
275568 KB |
Output is correct |
70 |
Correct |
101 ms |
273496 KB |
Output is correct |
71 |
Correct |
98 ms |
304296 KB |
Output is correct |
72 |
Correct |
61 ms |
375336 KB |
Output is correct |
73 |
Correct |
6 ms |
14988 KB |
Output is correct |