/*input
20
YYGYYYGGGGRGYYGRGRYG
*/
#pragma GCC optimize ("O3")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
int code(char c)
{
if (c == 'R')
return 0;
if (c == 'G')
return 1;
return 2;
}
int main()
{
ios_base::sync_with_stdio(false);
int n;
string s;
cin >> n >> s;
vector<int>p[3];
for (int i = 0; i < n; i++)
p[code(s[i])].push_back(i);
int dp[p[0].size() + 2][p[1].size() + 2][p[2].size() + 2][3];
for (int k0 = 0; k0 <= (int)p[0].size(); k0++)
{
for (int k1 = 0; k1 <= (int)p[1].size(); k1++)
{
for (int k2 = 0; k2 <= (int)p[2].size(); k2++)
{
for (int a = 0; a < 3; a++)
dp[k0][k1][k2][a] = 2e8;
}
}
}
dp[0][0][0][0] = 0;
dp[0][0][0][1] = 0;
dp[0][0][0][2] = 0;
int k[3];
for (k[0] = 0; k[0] <= (int)p[0].size(); k[0]++)
{
for (k[1] = 0; k[1] <= (int)p[1].size(); k[1]++)
{
for (k[2] = 0; k[2] <= (int)p[2].size(); k[2]++)
{
for (int a = 0; a < 3; a++)
{
int cost = 0;
if (k[a] == (int)p[a].size())
continue;
for (int b = 0; b < 3; b++)
if (b != a)
{
cost += lower_bound(p[b].begin() + k[b], p[b].end(), p[a][k[a]]) - (p[b].begin() + k[b]);
}
for (int b = 0; b < 3; b++)
{
if (a == b)
continue;
dp[k[0] + (a == 0)][k[1] + (a == 1)][k[2] + (a == 2)][b] =
min(dp[k[0] + (a == 0)][k[1] + (a == 1)][k[2] + (a == 2)][b],
cost + dp[k[0]][k[1]][k[2]][a]);
}
}
}
}
}
int ans = min({dp[p[0].size()][p[1].size()][p[2].size()][0],
dp[p[0].size()][p[1].size()][p[2].size()][1],
dp[p[0].size()][p[1].size()][p[2].size()][2]
});
if (ans > 1e8)
ans = -1;
cout << ans << "\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 |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
256 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
256 KB |
Output is correct |
9 |
Correct |
1 ms |
256 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
2 ms |
384 KB |
Output is correct |
14 |
Correct |
2 ms |
384 KB |
Output is correct |
15 |
Correct |
2 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
2 ms |
384 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 |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
256 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
256 KB |
Output is correct |
9 |
Correct |
1 ms |
256 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
2 ms |
384 KB |
Output is correct |
14 |
Correct |
2 ms |
384 KB |
Output is correct |
15 |
Correct |
2 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
2 ms |
384 KB |
Output is correct |
18 |
Correct |
3 ms |
512 KB |
Output is correct |
19 |
Correct |
3 ms |
512 KB |
Output is correct |
20 |
Correct |
3 ms |
384 KB |
Output is correct |
21 |
Correct |
3 ms |
384 KB |
Output is correct |
22 |
Correct |
2 ms |
384 KB |
Output is correct |
23 |
Correct |
3 ms |
384 KB |
Output is correct |
24 |
Correct |
3 ms |
512 KB |
Output is correct |
25 |
Correct |
2 ms |
384 KB |
Output is correct |
26 |
Correct |
2 ms |
384 KB |
Output is correct |
27 |
Correct |
2 ms |
512 KB |
Output is correct |
28 |
Correct |
2 ms |
512 KB |
Output is correct |
29 |
Correct |
2 ms |
384 KB |
Output is correct |
30 |
Correct |
2 ms |
384 KB |
Output is correct |
31 |
Correct |
2 ms |
384 KB |
Output is correct |
32 |
Correct |
3 ms |
384 KB |
Output is correct |
33 |
Correct |
2 ms |
384 KB |
Output is correct |
34 |
Correct |
2 ms |
384 KB |
Output is correct |
35 |
Correct |
3 ms |
512 KB |
Output is correct |
36 |
Correct |
2 ms |
512 KB |
Output is correct |
37 |
Correct |
2 ms |
512 KB |
Output is correct |
38 |
Correct |
3 ms |
384 KB |
Output is correct |
39 |
Correct |
3 ms |
512 KB |
Output is correct |
40 |
Correct |
2 ms |
384 KB |
Output is correct |
41 |
Correct |
3 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
1280 KB |
Output is correct |
3 |
Correct |
4 ms |
1280 KB |
Output is correct |
4 |
Correct |
5 ms |
1280 KB |
Output is correct |
5 |
Correct |
5 ms |
1280 KB |
Output is correct |
6 |
Correct |
5 ms |
1280 KB |
Output is correct |
7 |
Correct |
5 ms |
1280 KB |
Output is correct |
8 |
Correct |
5 ms |
1280 KB |
Output is correct |
9 |
Correct |
5 ms |
1280 KB |
Output is correct |
10 |
Correct |
5 ms |
1280 KB |
Output is correct |
11 |
Correct |
5 ms |
1280 KB |
Output is correct |
12 |
Correct |
3 ms |
640 KB |
Output is correct |
13 |
Correct |
3 ms |
740 KB |
Output is correct |
14 |
Correct |
4 ms |
1024 KB |
Output is correct |
15 |
Correct |
5 ms |
1280 KB |
Output is correct |
16 |
Correct |
5 ms |
1280 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 |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
256 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
256 KB |
Output is correct |
9 |
Correct |
1 ms |
256 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
2 ms |
384 KB |
Output is correct |
14 |
Correct |
2 ms |
384 KB |
Output is correct |
15 |
Correct |
2 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
2 ms |
384 KB |
Output is correct |
18 |
Correct |
3 ms |
512 KB |
Output is correct |
19 |
Correct |
3 ms |
512 KB |
Output is correct |
20 |
Correct |
3 ms |
384 KB |
Output is correct |
21 |
Correct |
3 ms |
384 KB |
Output is correct |
22 |
Correct |
2 ms |
384 KB |
Output is correct |
23 |
Correct |
3 ms |
384 KB |
Output is correct |
24 |
Correct |
3 ms |
512 KB |
Output is correct |
25 |
Correct |
2 ms |
384 KB |
Output is correct |
26 |
Correct |
2 ms |
384 KB |
Output is correct |
27 |
Correct |
2 ms |
512 KB |
Output is correct |
28 |
Correct |
2 ms |
512 KB |
Output is correct |
29 |
Correct |
2 ms |
384 KB |
Output is correct |
30 |
Correct |
2 ms |
384 KB |
Output is correct |
31 |
Correct |
2 ms |
384 KB |
Output is correct |
32 |
Correct |
3 ms |
384 KB |
Output is correct |
33 |
Correct |
2 ms |
384 KB |
Output is correct |
34 |
Correct |
2 ms |
384 KB |
Output is correct |
35 |
Correct |
3 ms |
512 KB |
Output is correct |
36 |
Correct |
2 ms |
512 KB |
Output is correct |
37 |
Correct |
2 ms |
512 KB |
Output is correct |
38 |
Correct |
3 ms |
384 KB |
Output is correct |
39 |
Correct |
3 ms |
512 KB |
Output is correct |
40 |
Correct |
2 ms |
384 KB |
Output is correct |
41 |
Correct |
3 ms |
384 KB |
Output is correct |
42 |
Correct |
2 ms |
384 KB |
Output is correct |
43 |
Correct |
4 ms |
1280 KB |
Output is correct |
44 |
Correct |
4 ms |
1280 KB |
Output is correct |
45 |
Correct |
5 ms |
1280 KB |
Output is correct |
46 |
Correct |
5 ms |
1280 KB |
Output is correct |
47 |
Correct |
5 ms |
1280 KB |
Output is correct |
48 |
Correct |
5 ms |
1280 KB |
Output is correct |
49 |
Correct |
5 ms |
1280 KB |
Output is correct |
50 |
Correct |
5 ms |
1280 KB |
Output is correct |
51 |
Correct |
5 ms |
1280 KB |
Output is correct |
52 |
Correct |
5 ms |
1280 KB |
Output is correct |
53 |
Correct |
3 ms |
640 KB |
Output is correct |
54 |
Correct |
3 ms |
740 KB |
Output is correct |
55 |
Correct |
4 ms |
1024 KB |
Output is correct |
56 |
Correct |
5 ms |
1280 KB |
Output is correct |
57 |
Correct |
5 ms |
1280 KB |
Output is correct |
58 |
Correct |
287 ms |
28928 KB |
Output is correct |
59 |
Correct |
285 ms |
29000 KB |
Output is correct |
60 |
Correct |
293 ms |
28776 KB |
Output is correct |
61 |
Correct |
297 ms |
29048 KB |
Output is correct |
62 |
Correct |
18 ms |
3200 KB |
Output is correct |
63 |
Correct |
32 ms |
4864 KB |
Output is correct |
64 |
Correct |
121 ms |
14720 KB |
Output is correct |
65 |
Correct |
234 ms |
20896 KB |
Output is correct |
66 |
Correct |
187 ms |
28628 KB |
Output is correct |
67 |
Correct |
188 ms |
28608 KB |
Output is correct |
68 |
Correct |
200 ms |
29056 KB |
Output is correct |
69 |
Correct |
205 ms |
29256 KB |
Output is correct |
70 |
Correct |
231 ms |
29156 KB |
Output is correct |
71 |
Correct |
246 ms |
28576 KB |
Output is correct |
72 |
Correct |
80 ms |
7896 KB |
Output is correct |
73 |
Correct |
16 ms |
2176 KB |
Output is correct |