#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
const bool typetest = 0;
const int N = 4e2 + 5;
const int Inf = 1e9 + 7;
int n;
string s;
int dp[N][N][N][3];
int num[3][3][N][N];
int r[N], g[N], y[N];
int a, b, c;
void Read()
{
cin >> n >> s;
s = " " + s;
for (int i = 1; i <= n; ++i)
{
if (s[i] == 'R')
r[++a] = i;
else if (s[i] == 'G')
g[++b] = i;
else
y[++c] = i;
}
}
inline void Min(int &x, int y)
{
if (x > y)
x = y;
}
void Solve()
{
for (int i = 1; i <= a; ++i)
{
int cnt(0);
for (int j = 0; j <= b; ++j)
{
if (g[j] > r[i])
++cnt;
num[0][1][i][j] = cnt;
}
cnt = 0;
for (int j = 0; j <= c; ++j)
{
if (y[j] > r[i])
++cnt;
num[0][2][i][j] = cnt;
}
}
for (int i = 1; i <= b; ++i)
{
int cnt(0);
for (int j = 0; j <= a; ++j)
{
if (r[j] > g[i])
++cnt;
num[1][0][i][j] = cnt;
}
cnt = 0;
for (int j = 0; j <= c; ++j)
{
if (y[j] > g[i])
++cnt;
num[1][2][i][j] = cnt;
}
}
for (int i = 1; i <= c; ++i)
{
int cnt(0);
for (int j = 0; j <= b; ++j)
{
if (g[j] > y[i])
++cnt;
num[2][1][i][j] = cnt;
}
cnt = 0;
for (int j = 0; j <= a; ++j)
{
if (r[j] > y[i])
++cnt;
num[2][0][i][j] = cnt;
}
}
for (int i = 0; i <= a; ++i)
for (int j = 0; j <= b; ++j)
for (int t = 0; t <= c; ++t)
dp[i][j][t][0] = dp[i][j][t][1] = dp[i][j][t][2] = Inf;
dp[0][0][0][0] = dp[0][0][0][1] = dp[0][0][0][2] = 0;
for (int i = 0; i <= a; ++i)
for (int j = 0; j <= b; ++j)
for (int t = 0; t <= c; ++t)
{
if (i != 0)
dp[i][j][t][0] = min(dp[i - 1][j][t][1], dp[i - 1][j][t][2]) + (r[i] + num[0][1][i][j] + num[0][2][i][t]) - (i + j + t);
if (j != 0)
dp[i][j][t][1] = min(dp[i][j - 1][t][0], dp[i][j - 1][t][2]) + (g[j] + num[1][0][j][i] + num[1][2][j][t]) - (i + j + t);
if (t != 0)
dp[i][j][t][2] = min(dp[i][j][t - 1][0], dp[i][j][t - 1][1]) + (y[t] + num[2][0][t][i] + num[2][1][t][j]) - (i + j + t);
}
int ans = min(dp[a][b][c][0], min(dp[a][b][c][1], dp[a][b][c][2]));
cout << (ans >= Inf ? -1 : ans);
}
int32_t main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t(1);
if (typetest)
cin >> t;
for (int _ = 1; _ <= t; ++_)
{
Read();
Solve();
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
492 KB |
Output is correct |
5 |
Correct |
1 ms |
492 KB |
Output is correct |
6 |
Correct |
1 ms |
620 KB |
Output is correct |
7 |
Correct |
1 ms |
620 KB |
Output is correct |
8 |
Correct |
1 ms |
620 KB |
Output is correct |
9 |
Correct |
1 ms |
748 KB |
Output is correct |
10 |
Correct |
1 ms |
748 KB |
Output is correct |
11 |
Correct |
1 ms |
620 KB |
Output is correct |
12 |
Correct |
1 ms |
620 KB |
Output is correct |
13 |
Correct |
1 ms |
492 KB |
Output is correct |
14 |
Correct |
1 ms |
620 KB |
Output is correct |
15 |
Correct |
1 ms |
492 KB |
Output is correct |
16 |
Correct |
1 ms |
620 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
492 KB |
Output is correct |
5 |
Correct |
1 ms |
492 KB |
Output is correct |
6 |
Correct |
1 ms |
620 KB |
Output is correct |
7 |
Correct |
1 ms |
620 KB |
Output is correct |
8 |
Correct |
1 ms |
620 KB |
Output is correct |
9 |
Correct |
1 ms |
748 KB |
Output is correct |
10 |
Correct |
1 ms |
748 KB |
Output is correct |
11 |
Correct |
1 ms |
620 KB |
Output is correct |
12 |
Correct |
1 ms |
620 KB |
Output is correct |
13 |
Correct |
1 ms |
492 KB |
Output is correct |
14 |
Correct |
1 ms |
620 KB |
Output is correct |
15 |
Correct |
1 ms |
492 KB |
Output is correct |
16 |
Correct |
1 ms |
620 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
2 ms |
2540 KB |
Output is correct |
19 |
Correct |
1 ms |
2156 KB |
Output is correct |
20 |
Correct |
2 ms |
2540 KB |
Output is correct |
21 |
Correct |
2 ms |
2668 KB |
Output is correct |
22 |
Correct |
1 ms |
1900 KB |
Output is correct |
23 |
Correct |
2 ms |
2412 KB |
Output is correct |
24 |
Correct |
1 ms |
1900 KB |
Output is correct |
25 |
Correct |
2 ms |
4204 KB |
Output is correct |
26 |
Correct |
3 ms |
4460 KB |
Output is correct |
27 |
Correct |
2 ms |
3308 KB |
Output is correct |
28 |
Correct |
2 ms |
2560 KB |
Output is correct |
29 |
Correct |
2 ms |
2540 KB |
Output is correct |
30 |
Correct |
2 ms |
2540 KB |
Output is correct |
31 |
Correct |
2 ms |
2284 KB |
Output is correct |
32 |
Correct |
2 ms |
2668 KB |
Output is correct |
33 |
Correct |
2 ms |
4076 KB |
Output is correct |
34 |
Correct |
2 ms |
3820 KB |
Output is correct |
35 |
Correct |
2 ms |
2924 KB |
Output is correct |
36 |
Correct |
1 ms |
2156 KB |
Output is correct |
37 |
Correct |
2 ms |
2028 KB |
Output is correct |
38 |
Correct |
2 ms |
2412 KB |
Output is correct |
39 |
Correct |
2 ms |
2540 KB |
Output is correct |
40 |
Correct |
1 ms |
620 KB |
Output is correct |
41 |
Correct |
2 ms |
2412 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
85 ms |
164276 KB |
Output is correct |
3 |
Correct |
84 ms |
163564 KB |
Output is correct |
4 |
Correct |
86 ms |
164332 KB |
Output is correct |
5 |
Correct |
84 ms |
164340 KB |
Output is correct |
6 |
Correct |
90 ms |
164332 KB |
Output is correct |
7 |
Correct |
85 ms |
163460 KB |
Output is correct |
8 |
Correct |
86 ms |
163436 KB |
Output is correct |
9 |
Correct |
84 ms |
162668 KB |
Output is correct |
10 |
Correct |
84 ms |
164332 KB |
Output is correct |
11 |
Correct |
85 ms |
164332 KB |
Output is correct |
12 |
Correct |
23 ms |
44692 KB |
Output is correct |
13 |
Correct |
40 ms |
78060 KB |
Output is correct |
14 |
Correct |
59 ms |
112620 KB |
Output is correct |
15 |
Correct |
84 ms |
164332 KB |
Output is correct |
16 |
Correct |
84 ms |
164332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
492 KB |
Output is correct |
5 |
Correct |
1 ms |
492 KB |
Output is correct |
6 |
Correct |
1 ms |
620 KB |
Output is correct |
7 |
Correct |
1 ms |
620 KB |
Output is correct |
8 |
Correct |
1 ms |
620 KB |
Output is correct |
9 |
Correct |
1 ms |
748 KB |
Output is correct |
10 |
Correct |
1 ms |
748 KB |
Output is correct |
11 |
Correct |
1 ms |
620 KB |
Output is correct |
12 |
Correct |
1 ms |
620 KB |
Output is correct |
13 |
Correct |
1 ms |
492 KB |
Output is correct |
14 |
Correct |
1 ms |
620 KB |
Output is correct |
15 |
Correct |
1 ms |
492 KB |
Output is correct |
16 |
Correct |
1 ms |
620 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
2 ms |
2540 KB |
Output is correct |
19 |
Correct |
1 ms |
2156 KB |
Output is correct |
20 |
Correct |
2 ms |
2540 KB |
Output is correct |
21 |
Correct |
2 ms |
2668 KB |
Output is correct |
22 |
Correct |
1 ms |
1900 KB |
Output is correct |
23 |
Correct |
2 ms |
2412 KB |
Output is correct |
24 |
Correct |
1 ms |
1900 KB |
Output is correct |
25 |
Correct |
2 ms |
4204 KB |
Output is correct |
26 |
Correct |
3 ms |
4460 KB |
Output is correct |
27 |
Correct |
2 ms |
3308 KB |
Output is correct |
28 |
Correct |
2 ms |
2560 KB |
Output is correct |
29 |
Correct |
2 ms |
2540 KB |
Output is correct |
30 |
Correct |
2 ms |
2540 KB |
Output is correct |
31 |
Correct |
2 ms |
2284 KB |
Output is correct |
32 |
Correct |
2 ms |
2668 KB |
Output is correct |
33 |
Correct |
2 ms |
4076 KB |
Output is correct |
34 |
Correct |
2 ms |
3820 KB |
Output is correct |
35 |
Correct |
2 ms |
2924 KB |
Output is correct |
36 |
Correct |
1 ms |
2156 KB |
Output is correct |
37 |
Correct |
2 ms |
2028 KB |
Output is correct |
38 |
Correct |
2 ms |
2412 KB |
Output is correct |
39 |
Correct |
2 ms |
2540 KB |
Output is correct |
40 |
Correct |
1 ms |
620 KB |
Output is correct |
41 |
Correct |
2 ms |
2412 KB |
Output is correct |
42 |
Correct |
1 ms |
364 KB |
Output is correct |
43 |
Correct |
85 ms |
164276 KB |
Output is correct |
44 |
Correct |
84 ms |
163564 KB |
Output is correct |
45 |
Correct |
86 ms |
164332 KB |
Output is correct |
46 |
Correct |
84 ms |
164340 KB |
Output is correct |
47 |
Correct |
90 ms |
164332 KB |
Output is correct |
48 |
Correct |
85 ms |
163460 KB |
Output is correct |
49 |
Correct |
86 ms |
163436 KB |
Output is correct |
50 |
Correct |
84 ms |
162668 KB |
Output is correct |
51 |
Correct |
84 ms |
164332 KB |
Output is correct |
52 |
Correct |
85 ms |
164332 KB |
Output is correct |
53 |
Correct |
23 ms |
44692 KB |
Output is correct |
54 |
Correct |
40 ms |
78060 KB |
Output is correct |
55 |
Correct |
59 ms |
112620 KB |
Output is correct |
56 |
Correct |
84 ms |
164332 KB |
Output is correct |
57 |
Correct |
84 ms |
164332 KB |
Output is correct |
58 |
Correct |
60 ms |
77548 KB |
Output is correct |
59 |
Correct |
72 ms |
97900 KB |
Output is correct |
60 |
Correct |
70 ms |
89196 KB |
Output is correct |
61 |
Correct |
65 ms |
86264 KB |
Output is correct |
62 |
Correct |
90 ms |
162028 KB |
Output is correct |
63 |
Correct |
90 ms |
161664 KB |
Output is correct |
64 |
Correct |
92 ms |
151532 KB |
Output is correct |
65 |
Correct |
90 ms |
139884 KB |
Output is correct |
66 |
Correct |
66 ms |
87404 KB |
Output is correct |
67 |
Correct |
63 ms |
84204 KB |
Output is correct |
68 |
Correct |
70 ms |
90092 KB |
Output is correct |
69 |
Correct |
65 ms |
86764 KB |
Output is correct |
70 |
Correct |
69 ms |
94060 KB |
Output is correct |
71 |
Correct |
64 ms |
85228 KB |
Output is correct |
72 |
Correct |
28 ms |
43116 KB |
Output is correct |
73 |
Correct |
4 ms |
4204 KB |
Output is correct |