#include <bits/stdc++.h>
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define llinf 8987654321987654321
#define inf 1987654321
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;
int n, dp[410][410][410][3], cnt[3];
char str[410];
vector<pair<int, pii> > vc[3];
int dist(pair<int, pii> a, pair<int, pii> b)
{
return abs(a.F-b.F)+abs(a.S.F-b.S.F)+abs(a.S.S-b.S.S);
}
int main()
{
scanf("%d", &n);
scanf("%s", str);
for(int i=0; i<n; i++){
int c;
if(str[i]=='R')c=0;
else if(str[i]=='G')c=1;
else c=2;
cnt[c]++;
vc[c].pb(mp(cnt[0], mp(cnt[1], cnt[2])));
}
int red=vc[0].size(), green=vc[1].size(), yellow=vc[2].size();
if(max(red, max(green, yellow))>(n+1)/2){
printf("-1");
return 0;
}
for(int r=0; r<=red; r++){
for(int g=0; g<=green; g++){
for(int y=0; y<=yellow; y++){
if(r+g+y==0)continue;
dp[r][g][y][0]=dp[r][g][y][1]=dp[r][g][y][2]=inf;
if(r)dp[r][g][y][0]=min(dp[r-1][g][y][1], dp[r-1][g][y][2])+dist(vc[0][r-1], {r, {g, y}});
if(g)dp[r][g][y][1]=min(dp[r][g-1][y][2], dp[r][g-1][y][0])+dist(vc[1][g-1], {r, {g, y}});
if(y)dp[r][g][y][2]=min(dp[r][g][y-1][0], dp[r][g][y-1][1])+dist(vc[2][y-1], {r, {g, y}});
}
}
}
printf("%d", min(dp[red][green][yellow][0], min(dp[red][green][yellow][1], dp[red][green][yellow][2]))/2);
}
Compilation message
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:21:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &n);
~~~~~^~~~~~~~~~
joi2019_ho_t3.cpp:22:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%s", str);
~~~~~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
256 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
3 ms |
504 KB |
Output is correct |
6 |
Correct |
2 ms |
504 KB |
Output is correct |
7 |
Correct |
2 ms |
504 KB |
Output is correct |
8 |
Correct |
2 ms |
508 KB |
Output is correct |
9 |
Correct |
2 ms |
632 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
504 KB |
Output is correct |
12 |
Correct |
2 ms |
504 KB |
Output is correct |
13 |
Correct |
2 ms |
256 KB |
Output is correct |
14 |
Correct |
2 ms |
504 KB |
Output is correct |
15 |
Correct |
2 ms |
504 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
380 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
256 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
3 ms |
504 KB |
Output is correct |
6 |
Correct |
2 ms |
504 KB |
Output is correct |
7 |
Correct |
2 ms |
504 KB |
Output is correct |
8 |
Correct |
2 ms |
508 KB |
Output is correct |
9 |
Correct |
2 ms |
632 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
504 KB |
Output is correct |
12 |
Correct |
2 ms |
504 KB |
Output is correct |
13 |
Correct |
2 ms |
256 KB |
Output is correct |
14 |
Correct |
2 ms |
504 KB |
Output is correct |
15 |
Correct |
2 ms |
504 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
380 KB |
Output is correct |
18 |
Correct |
4 ms |
2300 KB |
Output is correct |
19 |
Correct |
3 ms |
1916 KB |
Output is correct |
20 |
Correct |
4 ms |
2296 KB |
Output is correct |
21 |
Correct |
4 ms |
2424 KB |
Output is correct |
22 |
Correct |
3 ms |
1656 KB |
Output is correct |
23 |
Correct |
4 ms |
2168 KB |
Output is correct |
24 |
Correct |
3 ms |
1656 KB |
Output is correct |
25 |
Correct |
2 ms |
252 KB |
Output is correct |
26 |
Correct |
2 ms |
256 KB |
Output is correct |
27 |
Correct |
4 ms |
3064 KB |
Output is correct |
28 |
Correct |
4 ms |
2296 KB |
Output is correct |
29 |
Correct |
4 ms |
2296 KB |
Output is correct |
30 |
Correct |
4 ms |
2296 KB |
Output is correct |
31 |
Correct |
3 ms |
2040 KB |
Output is correct |
32 |
Correct |
4 ms |
2424 KB |
Output is correct |
33 |
Correct |
2 ms |
368 KB |
Output is correct |
34 |
Correct |
2 ms |
256 KB |
Output is correct |
35 |
Correct |
4 ms |
2684 KB |
Output is correct |
36 |
Correct |
3 ms |
1912 KB |
Output is correct |
37 |
Correct |
4 ms |
1784 KB |
Output is correct |
38 |
Correct |
4 ms |
2168 KB |
Output is correct |
39 |
Correct |
4 ms |
2296 KB |
Output is correct |
40 |
Correct |
2 ms |
376 KB |
Output is correct |
41 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
125 ms |
162964 KB |
Output is correct |
3 |
Correct |
127 ms |
162268 KB |
Output is correct |
4 |
Correct |
126 ms |
162988 KB |
Output is correct |
5 |
Correct |
137 ms |
162984 KB |
Output is correct |
6 |
Correct |
127 ms |
163080 KB |
Output is correct |
7 |
Correct |
125 ms |
162140 KB |
Output is correct |
8 |
Correct |
124 ms |
162192 KB |
Output is correct |
9 |
Correct |
125 ms |
161400 KB |
Output is correct |
10 |
Correct |
126 ms |
163028 KB |
Output is correct |
11 |
Correct |
125 ms |
163064 KB |
Output is correct |
12 |
Correct |
35 ms |
44152 KB |
Output is correct |
13 |
Correct |
60 ms |
77176 KB |
Output is correct |
14 |
Correct |
87 ms |
111352 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
256 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
3 ms |
504 KB |
Output is correct |
6 |
Correct |
2 ms |
504 KB |
Output is correct |
7 |
Correct |
2 ms |
504 KB |
Output is correct |
8 |
Correct |
2 ms |
508 KB |
Output is correct |
9 |
Correct |
2 ms |
632 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
504 KB |
Output is correct |
12 |
Correct |
2 ms |
504 KB |
Output is correct |
13 |
Correct |
2 ms |
256 KB |
Output is correct |
14 |
Correct |
2 ms |
504 KB |
Output is correct |
15 |
Correct |
2 ms |
504 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
380 KB |
Output is correct |
18 |
Correct |
4 ms |
2300 KB |
Output is correct |
19 |
Correct |
3 ms |
1916 KB |
Output is correct |
20 |
Correct |
4 ms |
2296 KB |
Output is correct |
21 |
Correct |
4 ms |
2424 KB |
Output is correct |
22 |
Correct |
3 ms |
1656 KB |
Output is correct |
23 |
Correct |
4 ms |
2168 KB |
Output is correct |
24 |
Correct |
3 ms |
1656 KB |
Output is correct |
25 |
Correct |
2 ms |
252 KB |
Output is correct |
26 |
Correct |
2 ms |
256 KB |
Output is correct |
27 |
Correct |
4 ms |
3064 KB |
Output is correct |
28 |
Correct |
4 ms |
2296 KB |
Output is correct |
29 |
Correct |
4 ms |
2296 KB |
Output is correct |
30 |
Correct |
4 ms |
2296 KB |
Output is correct |
31 |
Correct |
3 ms |
2040 KB |
Output is correct |
32 |
Correct |
4 ms |
2424 KB |
Output is correct |
33 |
Correct |
2 ms |
368 KB |
Output is correct |
34 |
Correct |
2 ms |
256 KB |
Output is correct |
35 |
Correct |
4 ms |
2684 KB |
Output is correct |
36 |
Correct |
3 ms |
1912 KB |
Output is correct |
37 |
Correct |
4 ms |
1784 KB |
Output is correct |
38 |
Correct |
4 ms |
2168 KB |
Output is correct |
39 |
Correct |
4 ms |
2296 KB |
Output is correct |
40 |
Correct |
2 ms |
376 KB |
Output is correct |
41 |
Correct |
2 ms |
376 KB |
Output is correct |
42 |
Correct |
2 ms |
376 KB |
Output is correct |
43 |
Correct |
125 ms |
162964 KB |
Output is correct |
44 |
Correct |
127 ms |
162268 KB |
Output is correct |
45 |
Correct |
126 ms |
162988 KB |
Output is correct |
46 |
Correct |
137 ms |
162984 KB |
Output is correct |
47 |
Correct |
127 ms |
163080 KB |
Output is correct |
48 |
Correct |
125 ms |
162140 KB |
Output is correct |
49 |
Correct |
124 ms |
162192 KB |
Output is correct |
50 |
Correct |
125 ms |
161400 KB |
Output is correct |
51 |
Correct |
126 ms |
163028 KB |
Output is correct |
52 |
Correct |
125 ms |
163064 KB |
Output is correct |
53 |
Correct |
35 ms |
44152 KB |
Output is correct |
54 |
Correct |
60 ms |
77176 KB |
Output is correct |
55 |
Correct |
87 ms |
111352 KB |
Output is correct |
56 |
Correct |
2 ms |
376 KB |
Output is correct |
57 |
Correct |
2 ms |
376 KB |
Output is correct |
58 |
Correct |
90 ms |
77148 KB |
Output is correct |
59 |
Correct |
103 ms |
97792 KB |
Output is correct |
60 |
Correct |
97 ms |
88908 KB |
Output is correct |
61 |
Correct |
95 ms |
85904 KB |
Output is correct |
62 |
Correct |
2 ms |
376 KB |
Output is correct |
63 |
Correct |
126 ms |
160220 KB |
Output is correct |
64 |
Correct |
145 ms |
150172 KB |
Output is correct |
65 |
Correct |
127 ms |
138532 KB |
Output is correct |
66 |
Correct |
96 ms |
87216 KB |
Output is correct |
67 |
Correct |
93 ms |
83884 KB |
Output is correct |
68 |
Correct |
97 ms |
89848 KB |
Output is correct |
69 |
Correct |
95 ms |
86488 KB |
Output is correct |
70 |
Correct |
102 ms |
93688 KB |
Output is correct |
71 |
Correct |
94 ms |
85012 KB |
Output is correct |
72 |
Correct |
2 ms |
376 KB |
Output is correct |
73 |
Correct |
3 ms |
348 KB |
Output is correct |