#include <bits/stdc++.h>
#define N 70
using namespace std;
int n, v[N], pos[3][N], sz[3], inf = 2000000000;
int dp[N][N][N][4], dij[N][N], diz[N][N], dji[N][N], dzi[N][N], djz[N][N], dzj[N][N];
int solve(int i, int j, int z, int lst)
{
if(!i and !j and !z) return 0;
if(dp[i][j][z][lst] != -1) return dp[i][j][z][lst];
int A = inf, B = inf, C = inf, di = 0, dj = 0, dz = 0;
if(i > 0 and lst != 0) A = solve(i - 1, j, z, 0) + abs(i + j + z - pos[0][i] + di + dij[i][j + 1] + diz[i][z + 1]);
if(j > 0 and lst != 1) B = solve(i, j - 1, z, 1) + abs(i + j + z - pos[1][j] + dj + dji[i + 1][j] + djz[z + 1][j]);
if(z > 0 and lst != 2) C = solve(i, j, z - 1, 2) + abs(i + j + z - pos[2][z] + dz + dzi[i + 1][z] + dzj[z][j + 1]);
return dp[i][j][z][lst] = min({A, B, C});
}
int main()
{
ios::sync_with_stdio(false); cin.tie(0);
cin>>n;
for(int i = 1; i <= n; i++)
{
char c;
cin>>c;
if(c == 'R') v[i] = 0;
else if(c == 'Y') v[i] = 1;
else v[i] = 2;
++sz[v[i]];
pos[v[i]][sz[v[i]]] = i;
}
for(int i = sz[0]; i >= 1; i--)
{
for(int a = sz[1]; a >= 1; a--)
{
if(pos[1][a] < pos[0][i]) dij[i][a] = dij[i][a + 1] + 1;
else dij[i][a] = dij[i][a + 1];
if(pos[0][i] < pos[1][a]) dji[i][a] = dji[i + 1][a] + 1;
else dji[i][a] = dji[i + 1][a];
}
for(int a = sz[2]; a >= 1; a--)
{
if(pos[2][a] < pos[0][i]) diz[i][a] = diz[i][a + 1] + 1;
else diz[i][a] = diz[i][a + 1];
if(pos[0][i] < pos[2][a]) dzi[i][a] = dzi[i + 1][a] + 1;
else dzi[i][a] = dzi[i + 1][a];
}
}
for(int z = sz[2]; z >= 1; z--)
{
for(int j = sz[1]; j >= 1; j--)
{
if(pos[1][j] < pos[2][z]) dzj[z][j] = dzj[z][j + 1] + 1;
else dzj[z][j] = dzj[z][j + 1];
if(pos[2][z] < pos[1][j]) djz[z][j] = djz[z + 1][j] + 1;
else djz[z][j] = djz[z + 1][j];
}
}
memset(dp, -1, sizeof dp);
int ans = solve(sz[0], sz[1], sz[2], 3);
cout<<(ans >= inf ? -1 : ans)<<"\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
5632 KB |
Output is correct |
2 |
Correct |
6 ms |
5760 KB |
Output is correct |
3 |
Correct |
6 ms |
5760 KB |
Output is correct |
4 |
Correct |
6 ms |
5760 KB |
Output is correct |
5 |
Correct |
6 ms |
5760 KB |
Output is correct |
6 |
Correct |
7 ms |
5760 KB |
Output is correct |
7 |
Correct |
7 ms |
5760 KB |
Output is correct |
8 |
Correct |
7 ms |
5760 KB |
Output is correct |
9 |
Correct |
6 ms |
5760 KB |
Output is correct |
10 |
Correct |
6 ms |
5760 KB |
Output is correct |
11 |
Correct |
8 ms |
5888 KB |
Output is correct |
12 |
Correct |
7 ms |
5760 KB |
Output is correct |
13 |
Correct |
7 ms |
5760 KB |
Output is correct |
14 |
Correct |
6 ms |
5760 KB |
Output is correct |
15 |
Correct |
6 ms |
5760 KB |
Output is correct |
16 |
Correct |
7 ms |
5760 KB |
Output is correct |
17 |
Correct |
6 ms |
5760 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
5632 KB |
Output is correct |
2 |
Correct |
6 ms |
5760 KB |
Output is correct |
3 |
Correct |
6 ms |
5760 KB |
Output is correct |
4 |
Correct |
6 ms |
5760 KB |
Output is correct |
5 |
Correct |
6 ms |
5760 KB |
Output is correct |
6 |
Correct |
7 ms |
5760 KB |
Output is correct |
7 |
Correct |
7 ms |
5760 KB |
Output is correct |
8 |
Correct |
7 ms |
5760 KB |
Output is correct |
9 |
Correct |
6 ms |
5760 KB |
Output is correct |
10 |
Correct |
6 ms |
5760 KB |
Output is correct |
11 |
Correct |
8 ms |
5888 KB |
Output is correct |
12 |
Correct |
7 ms |
5760 KB |
Output is correct |
13 |
Correct |
7 ms |
5760 KB |
Output is correct |
14 |
Correct |
6 ms |
5760 KB |
Output is correct |
15 |
Correct |
6 ms |
5760 KB |
Output is correct |
16 |
Correct |
7 ms |
5760 KB |
Output is correct |
17 |
Correct |
6 ms |
5760 KB |
Output is correct |
18 |
Correct |
7 ms |
5816 KB |
Output is correct |
19 |
Correct |
6 ms |
5760 KB |
Output is correct |
20 |
Correct |
7 ms |
5760 KB |
Output is correct |
21 |
Correct |
6 ms |
5760 KB |
Output is correct |
22 |
Correct |
7 ms |
5760 KB |
Output is correct |
23 |
Correct |
7 ms |
5760 KB |
Output is correct |
24 |
Correct |
7 ms |
5760 KB |
Output is correct |
25 |
Correct |
6 ms |
5760 KB |
Output is correct |
26 |
Correct |
6 ms |
5760 KB |
Output is correct |
27 |
Correct |
7 ms |
5760 KB |
Output is correct |
28 |
Correct |
8 ms |
5760 KB |
Output is correct |
29 |
Correct |
7 ms |
5808 KB |
Output is correct |
30 |
Correct |
8 ms |
5760 KB |
Output is correct |
31 |
Correct |
8 ms |
5760 KB |
Output is correct |
32 |
Correct |
6 ms |
5760 KB |
Output is correct |
33 |
Correct |
7 ms |
5760 KB |
Output is correct |
34 |
Correct |
6 ms |
5760 KB |
Output is correct |
35 |
Correct |
7 ms |
5760 KB |
Output is correct |
36 |
Correct |
6 ms |
5760 KB |
Output is correct |
37 |
Correct |
7 ms |
5760 KB |
Output is correct |
38 |
Correct |
7 ms |
5760 KB |
Output is correct |
39 |
Correct |
8 ms |
5760 KB |
Output is correct |
40 |
Correct |
6 ms |
5760 KB |
Output is correct |
41 |
Correct |
9 ms |
5760 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
5760 KB |
Output is correct |
2 |
Incorrect |
7 ms |
5760 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
5632 KB |
Output is correct |
2 |
Correct |
6 ms |
5760 KB |
Output is correct |
3 |
Correct |
6 ms |
5760 KB |
Output is correct |
4 |
Correct |
6 ms |
5760 KB |
Output is correct |
5 |
Correct |
6 ms |
5760 KB |
Output is correct |
6 |
Correct |
7 ms |
5760 KB |
Output is correct |
7 |
Correct |
7 ms |
5760 KB |
Output is correct |
8 |
Correct |
7 ms |
5760 KB |
Output is correct |
9 |
Correct |
6 ms |
5760 KB |
Output is correct |
10 |
Correct |
6 ms |
5760 KB |
Output is correct |
11 |
Correct |
8 ms |
5888 KB |
Output is correct |
12 |
Correct |
7 ms |
5760 KB |
Output is correct |
13 |
Correct |
7 ms |
5760 KB |
Output is correct |
14 |
Correct |
6 ms |
5760 KB |
Output is correct |
15 |
Correct |
6 ms |
5760 KB |
Output is correct |
16 |
Correct |
7 ms |
5760 KB |
Output is correct |
17 |
Correct |
6 ms |
5760 KB |
Output is correct |
18 |
Correct |
7 ms |
5816 KB |
Output is correct |
19 |
Correct |
6 ms |
5760 KB |
Output is correct |
20 |
Correct |
7 ms |
5760 KB |
Output is correct |
21 |
Correct |
6 ms |
5760 KB |
Output is correct |
22 |
Correct |
7 ms |
5760 KB |
Output is correct |
23 |
Correct |
7 ms |
5760 KB |
Output is correct |
24 |
Correct |
7 ms |
5760 KB |
Output is correct |
25 |
Correct |
6 ms |
5760 KB |
Output is correct |
26 |
Correct |
6 ms |
5760 KB |
Output is correct |
27 |
Correct |
7 ms |
5760 KB |
Output is correct |
28 |
Correct |
8 ms |
5760 KB |
Output is correct |
29 |
Correct |
7 ms |
5808 KB |
Output is correct |
30 |
Correct |
8 ms |
5760 KB |
Output is correct |
31 |
Correct |
8 ms |
5760 KB |
Output is correct |
32 |
Correct |
6 ms |
5760 KB |
Output is correct |
33 |
Correct |
7 ms |
5760 KB |
Output is correct |
34 |
Correct |
6 ms |
5760 KB |
Output is correct |
35 |
Correct |
7 ms |
5760 KB |
Output is correct |
36 |
Correct |
6 ms |
5760 KB |
Output is correct |
37 |
Correct |
7 ms |
5760 KB |
Output is correct |
38 |
Correct |
7 ms |
5760 KB |
Output is correct |
39 |
Correct |
8 ms |
5760 KB |
Output is correct |
40 |
Correct |
6 ms |
5760 KB |
Output is correct |
41 |
Correct |
9 ms |
5760 KB |
Output is correct |
42 |
Correct |
8 ms |
5760 KB |
Output is correct |
43 |
Incorrect |
7 ms |
5760 KB |
Output isn't correct |
44 |
Halted |
0 ms |
0 KB |
- |