#include <bits/stdc++.h>
using namespace std;
int inf = 1e8;
int n;
int a[405];
char sir[405];
int dp[401][401][401][4];
int pos[3][405],p0,p1,p2;
int sp[3][405];
int main()
{
cin >> n;
for (int i = 1; i <= n; i++)
cin >> sir[i];
for (int i = 1; i <= n; i++)
{
if (sir[i] == 'R')
{
a[i] = 0;
pos[0][++p0] = i;
}
else if (sir[i] == 'G')
{
a[i] = 1;
pos[1][++p1] = i;
}
else
{
a[i] = 2;
pos[2][++p2] = i;
}
}
for (int i = 1; i <= n; i++)
{
for (int j = 0; j < 3; j++)
sp[j][i] = sp[j][i - 1];
sp[a[i]][i]++;
}
for (int i = 0; i <= p0; i++)
for (int j = 0; j <= p1; j++)
for (int q = 0; q <= p2; q++)
for (int c = 0; c < 3; c++)
dp[i][j][q][c] = inf;
dp[0][0][0][0] = dp[0][0][0][1] = dp[0][0][0][2] = 0;
for (int i = 0; i <= p0; i++)
{
for (int j = 0; j <= p1; j++)
{
for (int q = 0; q <= p2; q++)
{
if (i < p0)
{
int pz = pos[0][i + 1];
dp[i + 1][j][q][0] = min(dp[i + 1][j][q][0],min(dp[i][j][q][1],dp[i][j][q][2]) + abs(j - sp[1][pz]) + abs(q - sp[2][pz]));
}
if (j < p1)
{
int pz = pos[1][j + 1];
dp[i][j + 1][q][1] = min(dp[i][j + 1][q][1],min(dp[i][j][q][0],dp[i][j][q][2]) + abs(i - sp[0][pz]) + abs(q - sp[2][pz]));
}
if (q < p2)
{
int pz = pos[2][q + 1];
dp[i][j][q + 1][2] = min(dp[i][j][q + 1][2],min(dp[i][j][q][0],dp[i][j][q][1]) + abs(i - sp[0][pz]) + abs(j - sp[1][pz]));
}
}
}
}
int ans = min(dp[p0][p1][p2][0],min(dp[p0][p1][p2][1],dp[p0][p1][p2][2]));
if (ans >= inf / 2)
cout << -1;
else
cout << ans / 2;
return 0;
}
///dp[i][j][q][c] bla bla
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
2 ms |
10584 KB |
Output is correct |
6 |
Correct |
2 ms |
12636 KB |
Output is correct |
7 |
Correct |
2 ms |
14836 KB |
Output is correct |
8 |
Correct |
1 ms |
12636 KB |
Output is correct |
9 |
Correct |
2 ms |
12636 KB |
Output is correct |
10 |
Correct |
2 ms |
20824 KB |
Output is correct |
11 |
Correct |
1 ms |
10584 KB |
Output is correct |
12 |
Correct |
1 ms |
10588 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
2 ms |
12636 KB |
Output is correct |
15 |
Correct |
2 ms |
8540 KB |
Output is correct |
16 |
Correct |
3 ms |
22876 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
2 ms |
10584 KB |
Output is correct |
6 |
Correct |
2 ms |
12636 KB |
Output is correct |
7 |
Correct |
2 ms |
14836 KB |
Output is correct |
8 |
Correct |
1 ms |
12636 KB |
Output is correct |
9 |
Correct |
2 ms |
12636 KB |
Output is correct |
10 |
Correct |
2 ms |
20824 KB |
Output is correct |
11 |
Correct |
1 ms |
10584 KB |
Output is correct |
12 |
Correct |
1 ms |
10588 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
2 ms |
12636 KB |
Output is correct |
15 |
Correct |
2 ms |
8540 KB |
Output is correct |
16 |
Correct |
3 ms |
22876 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
11 ms |
51824 KB |
Output is correct |
19 |
Correct |
5 ms |
43608 KB |
Output is correct |
20 |
Correct |
7 ms |
55904 KB |
Output is correct |
21 |
Correct |
5 ms |
45660 KB |
Output is correct |
22 |
Correct |
6 ms |
51804 KB |
Output is correct |
23 |
Correct |
4 ms |
37468 KB |
Output is correct |
24 |
Correct |
3 ms |
23132 KB |
Output is correct |
25 |
Correct |
12 ms |
82524 KB |
Output is correct |
26 |
Correct |
8 ms |
72280 KB |
Output is correct |
27 |
Correct |
6 ms |
59996 KB |
Output is correct |
28 |
Correct |
5 ms |
47708 KB |
Output is correct |
29 |
Correct |
5 ms |
49752 KB |
Output is correct |
30 |
Correct |
4 ms |
37468 KB |
Output is correct |
31 |
Correct |
4 ms |
33624 KB |
Output is correct |
32 |
Correct |
4 ms |
41564 KB |
Output is correct |
33 |
Correct |
6 ms |
59996 KB |
Output is correct |
34 |
Correct |
6 ms |
53860 KB |
Output is correct |
35 |
Correct |
6 ms |
51804 KB |
Output is correct |
36 |
Correct |
5 ms |
45500 KB |
Output is correct |
37 |
Correct |
3 ms |
29272 KB |
Output is correct |
38 |
Correct |
4 ms |
31324 KB |
Output is correct |
39 |
Correct |
5 ms |
43612 KB |
Output is correct |
40 |
Correct |
0 ms |
348 KB |
Output is correct |
41 |
Correct |
3 ms |
23132 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
76 ms |
406400 KB |
Output is correct |
3 |
Correct |
53 ms |
407568 KB |
Output is correct |
4 |
Correct |
50 ms |
407684 KB |
Output is correct |
5 |
Correct |
49 ms |
407636 KB |
Output is correct |
6 |
Correct |
49 ms |
407636 KB |
Output is correct |
7 |
Correct |
52 ms |
406864 KB |
Output is correct |
8 |
Correct |
51 ms |
406868 KB |
Output is correct |
9 |
Correct |
51 ms |
406708 KB |
Output is correct |
10 |
Correct |
49 ms |
407632 KB |
Output is correct |
11 |
Correct |
52 ms |
407636 KB |
Output is correct |
12 |
Correct |
25 ms |
260944 KB |
Output is correct |
13 |
Correct |
34 ms |
347220 KB |
Output is correct |
14 |
Correct |
42 ms |
376408 KB |
Output is correct |
15 |
Correct |
50 ms |
406980 KB |
Output is correct |
16 |
Correct |
48 ms |
407124 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
2 ms |
10584 KB |
Output is correct |
6 |
Correct |
2 ms |
12636 KB |
Output is correct |
7 |
Correct |
2 ms |
14836 KB |
Output is correct |
8 |
Correct |
1 ms |
12636 KB |
Output is correct |
9 |
Correct |
2 ms |
12636 KB |
Output is correct |
10 |
Correct |
2 ms |
20824 KB |
Output is correct |
11 |
Correct |
1 ms |
10584 KB |
Output is correct |
12 |
Correct |
1 ms |
10588 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
2 ms |
12636 KB |
Output is correct |
15 |
Correct |
2 ms |
8540 KB |
Output is correct |
16 |
Correct |
3 ms |
22876 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
11 ms |
51824 KB |
Output is correct |
19 |
Correct |
5 ms |
43608 KB |
Output is correct |
20 |
Correct |
7 ms |
55904 KB |
Output is correct |
21 |
Correct |
5 ms |
45660 KB |
Output is correct |
22 |
Correct |
6 ms |
51804 KB |
Output is correct |
23 |
Correct |
4 ms |
37468 KB |
Output is correct |
24 |
Correct |
3 ms |
23132 KB |
Output is correct |
25 |
Correct |
12 ms |
82524 KB |
Output is correct |
26 |
Correct |
8 ms |
72280 KB |
Output is correct |
27 |
Correct |
6 ms |
59996 KB |
Output is correct |
28 |
Correct |
5 ms |
47708 KB |
Output is correct |
29 |
Correct |
5 ms |
49752 KB |
Output is correct |
30 |
Correct |
4 ms |
37468 KB |
Output is correct |
31 |
Correct |
4 ms |
33624 KB |
Output is correct |
32 |
Correct |
4 ms |
41564 KB |
Output is correct |
33 |
Correct |
6 ms |
59996 KB |
Output is correct |
34 |
Correct |
6 ms |
53860 KB |
Output is correct |
35 |
Correct |
6 ms |
51804 KB |
Output is correct |
36 |
Correct |
5 ms |
45500 KB |
Output is correct |
37 |
Correct |
3 ms |
29272 KB |
Output is correct |
38 |
Correct |
4 ms |
31324 KB |
Output is correct |
39 |
Correct |
5 ms |
43612 KB |
Output is correct |
40 |
Correct |
0 ms |
348 KB |
Output is correct |
41 |
Correct |
3 ms |
23132 KB |
Output is correct |
42 |
Correct |
1 ms |
4444 KB |
Output is correct |
43 |
Correct |
76 ms |
406400 KB |
Output is correct |
44 |
Correct |
53 ms |
407568 KB |
Output is correct |
45 |
Correct |
50 ms |
407684 KB |
Output is correct |
46 |
Correct |
49 ms |
407636 KB |
Output is correct |
47 |
Correct |
49 ms |
407636 KB |
Output is correct |
48 |
Correct |
52 ms |
406864 KB |
Output is correct |
49 |
Correct |
51 ms |
406868 KB |
Output is correct |
50 |
Correct |
51 ms |
406708 KB |
Output is correct |
51 |
Correct |
49 ms |
407632 KB |
Output is correct |
52 |
Correct |
52 ms |
407636 KB |
Output is correct |
53 |
Correct |
25 ms |
260944 KB |
Output is correct |
54 |
Correct |
34 ms |
347220 KB |
Output is correct |
55 |
Correct |
42 ms |
376408 KB |
Output is correct |
56 |
Correct |
50 ms |
406980 KB |
Output is correct |
57 |
Correct |
48 ms |
407124 KB |
Output is correct |
58 |
Correct |
54 ms |
312404 KB |
Output is correct |
59 |
Correct |
60 ms |
360052 KB |
Output is correct |
60 |
Correct |
59 ms |
359504 KB |
Output is correct |
61 |
Correct |
60 ms |
337356 KB |
Output is correct |
62 |
Correct |
57 ms |
412004 KB |
Output is correct |
63 |
Correct |
55 ms |
403096 KB |
Output is correct |
64 |
Correct |
62 ms |
395340 KB |
Output is correct |
65 |
Correct |
60 ms |
373840 KB |
Output is correct |
66 |
Correct |
60 ms |
345168 KB |
Output is correct |
67 |
Correct |
51 ms |
283732 KB |
Output is correct |
68 |
Correct |
57 ms |
345200 KB |
Output is correct |
69 |
Correct |
57 ms |
345180 KB |
Output is correct |
70 |
Correct |
59 ms |
341072 KB |
Output is correct |
71 |
Correct |
62 ms |
361552 KB |
Output is correct |
72 |
Correct |
49 ms |
376496 KB |
Output is correct |
73 |
Correct |
3 ms |
12892 KB |
Output is correct |