#include <bits/stdc++.h>
using namespace std;
const int MAXN = 405;
const int INF = 1e9;
int n;
vector <int> Pos[3];
int cnt[MAXN][3];
int dp[2][MAXN][MAXN][3];
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
for(int i = 1; i <= n; i++)
{
char c;
cin >> c;
for(int j = 0; j < 3; j++)
{
cnt[i][j] = cnt[i - 1][j];
}
if(c == 'R')
{
cnt[i][0]++;
Pos[0].push_back(i);
}
else if(c == 'Y')
{
cnt[i][1]++;
Pos[1].push_back(i);
}
else if(c == 'G')
{
cnt[i][2]++;
Pos[2].push_back(i);
}
}
int countcolor[3];
for(int i = 0; i < 3; i++)
{
countcolor[i] = cnt[n][i];
}
for(int i = 0; i <= countcolor[0]; i++)
{
for(int j = 0; j <= countcolor[1]; j++)
{
for(int k = 0; k < 3; k++)
{
dp[0][i][j][k] = INF;
}
}
}
dp[0][0][0][0] = 0;
dp[0][0][0][1] = 0;
dp[0][0][0][2] = 0;
for(int i = 1; i <= n; i++)
{
int c[3];
for(c[0] = 0; c[0] <= countcolor[0]; c[0]++)
{
for(c[1] = 0; c[1] <= countcolor[1]; c[1]++)
{
c[2] = i - c[0] - c[1];
for(int j = 0; j < 3; j++)
{
dp[i & 1][c[0]][c[1]][j] = INF;
}
if(c[2] < 0 or c[2] > countcolor[2])
{
continue;
}
for(int j = 0; j < 3; j++)
{
if(c[j])
{
int realpos = 0, id = Pos[j][c[j] - 1], t1 = INF;
for(int k = 0; k < 3; k++)
{
realpos += max(c[k], cnt[id][k]);
if(k != j)
{
c[j]--;
t1 = min(t1, dp[(i & 1) ^ 1][c[0]][c[1]][k]);
c[j]++;
}
}
dp[i & 1][c[0]][c[1]][j] = min(dp[i & 1][c[0]][c[1]][j], t1 + (realpos - i));
}
}
}
}
}
int ans = INF;
for(int i = 0; i < 3; i++)
{
ans = min(ans, dp[n & 1][countcolor[0]][countcolor[1]][i]);
}
if(ans == INF)
{
ans = -1;
}
cout << ans << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
320 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
0 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
0 ms |
332 KB |
Output is correct |
15 |
Correct |
0 ms |
332 KB |
Output is correct |
16 |
Correct |
0 ms |
332 KB |
Output is correct |
17 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
320 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
0 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
0 ms |
332 KB |
Output is correct |
15 |
Correct |
0 ms |
332 KB |
Output is correct |
16 |
Correct |
0 ms |
332 KB |
Output is correct |
17 |
Correct |
0 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
460 KB |
Output is correct |
19 |
Correct |
1 ms |
460 KB |
Output is correct |
20 |
Correct |
1 ms |
448 KB |
Output is correct |
21 |
Correct |
1 ms |
460 KB |
Output is correct |
22 |
Correct |
1 ms |
460 KB |
Output is correct |
23 |
Correct |
1 ms |
460 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
588 KB |
Output is correct |
26 |
Correct |
1 ms |
460 KB |
Output is correct |
27 |
Correct |
1 ms |
460 KB |
Output is correct |
28 |
Correct |
1 ms |
460 KB |
Output is correct |
29 |
Correct |
1 ms |
460 KB |
Output is correct |
30 |
Correct |
1 ms |
460 KB |
Output is correct |
31 |
Correct |
1 ms |
460 KB |
Output is correct |
32 |
Correct |
1 ms |
460 KB |
Output is correct |
33 |
Correct |
0 ms |
460 KB |
Output is correct |
34 |
Correct |
1 ms |
460 KB |
Output is correct |
35 |
Correct |
1 ms |
460 KB |
Output is correct |
36 |
Correct |
1 ms |
460 KB |
Output is correct |
37 |
Correct |
1 ms |
332 KB |
Output is correct |
38 |
Correct |
1 ms |
332 KB |
Output is correct |
39 |
Correct |
1 ms |
460 KB |
Output is correct |
40 |
Correct |
0 ms |
332 KB |
Output is correct |
41 |
Correct |
0 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
2 ms |
1868 KB |
Output is correct |
3 |
Correct |
2 ms |
1868 KB |
Output is correct |
4 |
Correct |
2 ms |
1868 KB |
Output is correct |
5 |
Correct |
3 ms |
1868 KB |
Output is correct |
6 |
Correct |
2 ms |
1868 KB |
Output is correct |
7 |
Correct |
4 ms |
1836 KB |
Output is correct |
8 |
Correct |
3 ms |
1868 KB |
Output is correct |
9 |
Correct |
2 ms |
1868 KB |
Output is correct |
10 |
Correct |
2 ms |
1868 KB |
Output is correct |
11 |
Correct |
3 ms |
1868 KB |
Output is correct |
12 |
Correct |
1 ms |
1100 KB |
Output is correct |
13 |
Correct |
1 ms |
1356 KB |
Output is correct |
14 |
Correct |
2 ms |
1600 KB |
Output is correct |
15 |
Correct |
2 ms |
1868 KB |
Output is correct |
16 |
Correct |
2 ms |
1868 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
320 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
0 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
0 ms |
332 KB |
Output is correct |
15 |
Correct |
0 ms |
332 KB |
Output is correct |
16 |
Correct |
0 ms |
332 KB |
Output is correct |
17 |
Correct |
0 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
460 KB |
Output is correct |
19 |
Correct |
1 ms |
460 KB |
Output is correct |
20 |
Correct |
1 ms |
448 KB |
Output is correct |
21 |
Correct |
1 ms |
460 KB |
Output is correct |
22 |
Correct |
1 ms |
460 KB |
Output is correct |
23 |
Correct |
1 ms |
460 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
588 KB |
Output is correct |
26 |
Correct |
1 ms |
460 KB |
Output is correct |
27 |
Correct |
1 ms |
460 KB |
Output is correct |
28 |
Correct |
1 ms |
460 KB |
Output is correct |
29 |
Correct |
1 ms |
460 KB |
Output is correct |
30 |
Correct |
1 ms |
460 KB |
Output is correct |
31 |
Correct |
1 ms |
460 KB |
Output is correct |
32 |
Correct |
1 ms |
460 KB |
Output is correct |
33 |
Correct |
0 ms |
460 KB |
Output is correct |
34 |
Correct |
1 ms |
460 KB |
Output is correct |
35 |
Correct |
1 ms |
460 KB |
Output is correct |
36 |
Correct |
1 ms |
460 KB |
Output is correct |
37 |
Correct |
1 ms |
332 KB |
Output is correct |
38 |
Correct |
1 ms |
332 KB |
Output is correct |
39 |
Correct |
1 ms |
460 KB |
Output is correct |
40 |
Correct |
0 ms |
332 KB |
Output is correct |
41 |
Correct |
0 ms |
332 KB |
Output is correct |
42 |
Correct |
0 ms |
332 KB |
Output is correct |
43 |
Correct |
2 ms |
1868 KB |
Output is correct |
44 |
Correct |
2 ms |
1868 KB |
Output is correct |
45 |
Correct |
2 ms |
1868 KB |
Output is correct |
46 |
Correct |
3 ms |
1868 KB |
Output is correct |
47 |
Correct |
2 ms |
1868 KB |
Output is correct |
48 |
Correct |
4 ms |
1836 KB |
Output is correct |
49 |
Correct |
3 ms |
1868 KB |
Output is correct |
50 |
Correct |
2 ms |
1868 KB |
Output is correct |
51 |
Correct |
2 ms |
1868 KB |
Output is correct |
52 |
Correct |
3 ms |
1868 KB |
Output is correct |
53 |
Correct |
1 ms |
1100 KB |
Output is correct |
54 |
Correct |
1 ms |
1356 KB |
Output is correct |
55 |
Correct |
2 ms |
1600 KB |
Output is correct |
56 |
Correct |
2 ms |
1868 KB |
Output is correct |
57 |
Correct |
2 ms |
1868 KB |
Output is correct |
58 |
Correct |
122 ms |
1488 KB |
Output is correct |
59 |
Correct |
116 ms |
1612 KB |
Output is correct |
60 |
Correct |
121 ms |
1668 KB |
Output is correct |
61 |
Correct |
118 ms |
1600 KB |
Output is correct |
62 |
Correct |
10 ms |
1996 KB |
Output is correct |
63 |
Correct |
16 ms |
1936 KB |
Output is correct |
64 |
Correct |
54 ms |
1852 KB |
Output is correct |
65 |
Correct |
76 ms |
1740 KB |
Output is correct |
66 |
Correct |
117 ms |
1612 KB |
Output is correct |
67 |
Correct |
117 ms |
1356 KB |
Output is correct |
68 |
Correct |
119 ms |
1612 KB |
Output is correct |
69 |
Correct |
119 ms |
1612 KB |
Output is correct |
70 |
Correct |
115 ms |
1612 KB |
Output is correct |
71 |
Correct |
121 ms |
1740 KB |
Output is correct |
72 |
Correct |
72 ms |
3172 KB |
Output is correct |
73 |
Correct |
8 ms |
384 KB |
Output is correct |