#include <cstdio>
#include <vector>
#include <cmath>
#include <algorithm>
#include <cstring>
using namespace std;
int n;
char arr[405];
int pre[405][3];
vector<int> r,g,y;
int memo[205][205][205][3];
const int INF=1000000000;
int dp(int sr,int sg,int sy,int last){ ///1000000000 for invalid case
if (!sr && !sg && !sy) return 0;
else if (memo[sr][sg][sy][last]!=-1) return memo[sr][sg][sy][last];
int ans=INF;
if (last!=0 && sr) ans=min(ans,dp(sr-1,sg,sy,0)
+max(0,pre[r[sr-1]][1]-sg)+max(0,pre[r[sr-1]][2]-sy));
if (last!=1 && sg) ans=min(ans,dp(sr,sg-1,sy,1)
+max(0,pre[g[sg-1]][0]-sr)+max(0,pre[g[sg-1]][2]-sy));
if (last!=2 && sy) ans=min(ans,dp(sr,sg,sy-1,2)
+max(0,pre[y[sy-1]][0]-sr)+max(0,pre[y[sy-1]][1]-sg));
//printf("%d %d %d %d %d\n",sr,sg,sy,last,ans);
return memo[sr][sg][sy][last]=ans;
}
int main(){
//freopen("input.txt","r",stdin);
scanf("%d",&n);
getchar();
for (int x=0;x<n;x++){
arr[x]=getchar();
if (arr[x]=='R') r.push_back(x);
else if (arr[x]=='G') g.push_back(x);
else y.push_back(x);
}
if (r.size()>200 || g.size()>200 || y.size()>200) {
printf("-1\n");
return 0;
}
for (int x=0;x<n;x++){
if (arr[x]=='R') pre[x][0]++;
else if (arr[x]=='G') pre[x][1]++;
else pre[x][2]++;
for (int y=0;y<3;y++) pre[x+1][y]=pre[x][y];
}
memset(memo,-1,sizeof(memo));
int ans=INF;
if (!r.empty()) ans=min(ans,dp(r.size()-1,g.size(),y.size(),0));
if (!g.empty()) ans=min(ans,dp(r.size(),g.size()-1,y.size(),1));
if (!y.empty()) ans=min(ans,dp(r.size(),g.size(),y.size()-1,2));
printf("%d\n",(ans==INF)?-1:ans);
}
Compilation message
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:28:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&n);
~~~~~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
86 ms |
101496 KB |
Output is correct |
2 |
Correct |
86 ms |
101496 KB |
Output is correct |
3 |
Correct |
86 ms |
101496 KB |
Output is correct |
4 |
Correct |
86 ms |
101496 KB |
Output is correct |
5 |
Correct |
88 ms |
101624 KB |
Output is correct |
6 |
Correct |
86 ms |
101404 KB |
Output is correct |
7 |
Correct |
87 ms |
101496 KB |
Output is correct |
8 |
Correct |
87 ms |
101496 KB |
Output is correct |
9 |
Correct |
87 ms |
101496 KB |
Output is correct |
10 |
Correct |
87 ms |
101496 KB |
Output is correct |
11 |
Correct |
87 ms |
101496 KB |
Output is correct |
12 |
Correct |
86 ms |
101496 KB |
Output is correct |
13 |
Correct |
87 ms |
101496 KB |
Output is correct |
14 |
Correct |
86 ms |
101500 KB |
Output is correct |
15 |
Correct |
86 ms |
101496 KB |
Output is correct |
16 |
Correct |
87 ms |
101496 KB |
Output is correct |
17 |
Correct |
87 ms |
101624 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
86 ms |
101496 KB |
Output is correct |
2 |
Correct |
86 ms |
101496 KB |
Output is correct |
3 |
Correct |
86 ms |
101496 KB |
Output is correct |
4 |
Correct |
86 ms |
101496 KB |
Output is correct |
5 |
Correct |
88 ms |
101624 KB |
Output is correct |
6 |
Correct |
86 ms |
101404 KB |
Output is correct |
7 |
Correct |
87 ms |
101496 KB |
Output is correct |
8 |
Correct |
87 ms |
101496 KB |
Output is correct |
9 |
Correct |
87 ms |
101496 KB |
Output is correct |
10 |
Correct |
87 ms |
101496 KB |
Output is correct |
11 |
Correct |
87 ms |
101496 KB |
Output is correct |
12 |
Correct |
86 ms |
101496 KB |
Output is correct |
13 |
Correct |
87 ms |
101496 KB |
Output is correct |
14 |
Correct |
86 ms |
101500 KB |
Output is correct |
15 |
Correct |
86 ms |
101496 KB |
Output is correct |
16 |
Correct |
87 ms |
101496 KB |
Output is correct |
17 |
Correct |
87 ms |
101624 KB |
Output is correct |
18 |
Correct |
87 ms |
101496 KB |
Output is correct |
19 |
Correct |
87 ms |
101496 KB |
Output is correct |
20 |
Correct |
87 ms |
101496 KB |
Output is correct |
21 |
Correct |
87 ms |
101496 KB |
Output is correct |
22 |
Correct |
87 ms |
101496 KB |
Output is correct |
23 |
Correct |
88 ms |
101496 KB |
Output is correct |
24 |
Correct |
87 ms |
101496 KB |
Output is correct |
25 |
Correct |
87 ms |
101496 KB |
Output is correct |
26 |
Correct |
87 ms |
101496 KB |
Output is correct |
27 |
Correct |
87 ms |
101572 KB |
Output is correct |
28 |
Correct |
88 ms |
101496 KB |
Output is correct |
29 |
Correct |
94 ms |
101496 KB |
Output is correct |
30 |
Correct |
87 ms |
101496 KB |
Output is correct |
31 |
Correct |
89 ms |
101496 KB |
Output is correct |
32 |
Correct |
87 ms |
101500 KB |
Output is correct |
33 |
Correct |
87 ms |
101496 KB |
Output is correct |
34 |
Correct |
86 ms |
101496 KB |
Output is correct |
35 |
Correct |
87 ms |
101496 KB |
Output is correct |
36 |
Correct |
87 ms |
101496 KB |
Output is correct |
37 |
Correct |
87 ms |
101496 KB |
Output is correct |
38 |
Correct |
87 ms |
101496 KB |
Output is correct |
39 |
Correct |
88 ms |
101496 KB |
Output is correct |
40 |
Correct |
87 ms |
101496 KB |
Output is correct |
41 |
Correct |
87 ms |
101488 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
86 ms |
101624 KB |
Output is correct |
2 |
Correct |
86 ms |
101496 KB |
Output is correct |
3 |
Correct |
87 ms |
101448 KB |
Output is correct |
4 |
Correct |
87 ms |
101500 KB |
Output is correct |
5 |
Correct |
87 ms |
101496 KB |
Output is correct |
6 |
Correct |
87 ms |
101496 KB |
Output is correct |
7 |
Correct |
87 ms |
101480 KB |
Output is correct |
8 |
Correct |
87 ms |
101496 KB |
Output is correct |
9 |
Correct |
86 ms |
101496 KB |
Output is correct |
10 |
Correct |
88 ms |
101496 KB |
Output is correct |
11 |
Correct |
87 ms |
101496 KB |
Output is correct |
12 |
Correct |
87 ms |
101496 KB |
Output is correct |
13 |
Correct |
86 ms |
101508 KB |
Output is correct |
14 |
Correct |
86 ms |
101496 KB |
Output is correct |
15 |
Correct |
2 ms |
380 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
86 ms |
101496 KB |
Output is correct |
2 |
Correct |
86 ms |
101496 KB |
Output is correct |
3 |
Correct |
86 ms |
101496 KB |
Output is correct |
4 |
Correct |
86 ms |
101496 KB |
Output is correct |
5 |
Correct |
88 ms |
101624 KB |
Output is correct |
6 |
Correct |
86 ms |
101404 KB |
Output is correct |
7 |
Correct |
87 ms |
101496 KB |
Output is correct |
8 |
Correct |
87 ms |
101496 KB |
Output is correct |
9 |
Correct |
87 ms |
101496 KB |
Output is correct |
10 |
Correct |
87 ms |
101496 KB |
Output is correct |
11 |
Correct |
87 ms |
101496 KB |
Output is correct |
12 |
Correct |
86 ms |
101496 KB |
Output is correct |
13 |
Correct |
87 ms |
101496 KB |
Output is correct |
14 |
Correct |
86 ms |
101500 KB |
Output is correct |
15 |
Correct |
86 ms |
101496 KB |
Output is correct |
16 |
Correct |
87 ms |
101496 KB |
Output is correct |
17 |
Correct |
87 ms |
101624 KB |
Output is correct |
18 |
Correct |
87 ms |
101496 KB |
Output is correct |
19 |
Correct |
87 ms |
101496 KB |
Output is correct |
20 |
Correct |
87 ms |
101496 KB |
Output is correct |
21 |
Correct |
87 ms |
101496 KB |
Output is correct |
22 |
Correct |
87 ms |
101496 KB |
Output is correct |
23 |
Correct |
88 ms |
101496 KB |
Output is correct |
24 |
Correct |
87 ms |
101496 KB |
Output is correct |
25 |
Correct |
87 ms |
101496 KB |
Output is correct |
26 |
Correct |
87 ms |
101496 KB |
Output is correct |
27 |
Correct |
87 ms |
101572 KB |
Output is correct |
28 |
Correct |
88 ms |
101496 KB |
Output is correct |
29 |
Correct |
94 ms |
101496 KB |
Output is correct |
30 |
Correct |
87 ms |
101496 KB |
Output is correct |
31 |
Correct |
89 ms |
101496 KB |
Output is correct |
32 |
Correct |
87 ms |
101500 KB |
Output is correct |
33 |
Correct |
87 ms |
101496 KB |
Output is correct |
34 |
Correct |
86 ms |
101496 KB |
Output is correct |
35 |
Correct |
87 ms |
101496 KB |
Output is correct |
36 |
Correct |
87 ms |
101496 KB |
Output is correct |
37 |
Correct |
87 ms |
101496 KB |
Output is correct |
38 |
Correct |
87 ms |
101496 KB |
Output is correct |
39 |
Correct |
88 ms |
101496 KB |
Output is correct |
40 |
Correct |
87 ms |
101496 KB |
Output is correct |
41 |
Correct |
87 ms |
101488 KB |
Output is correct |
42 |
Correct |
86 ms |
101624 KB |
Output is correct |
43 |
Correct |
86 ms |
101496 KB |
Output is correct |
44 |
Correct |
87 ms |
101448 KB |
Output is correct |
45 |
Correct |
87 ms |
101500 KB |
Output is correct |
46 |
Correct |
87 ms |
101496 KB |
Output is correct |
47 |
Correct |
87 ms |
101496 KB |
Output is correct |
48 |
Correct |
87 ms |
101480 KB |
Output is correct |
49 |
Correct |
87 ms |
101496 KB |
Output is correct |
50 |
Correct |
86 ms |
101496 KB |
Output is correct |
51 |
Correct |
88 ms |
101496 KB |
Output is correct |
52 |
Correct |
87 ms |
101496 KB |
Output is correct |
53 |
Correct |
87 ms |
101496 KB |
Output is correct |
54 |
Correct |
86 ms |
101508 KB |
Output is correct |
55 |
Correct |
86 ms |
101496 KB |
Output is correct |
56 |
Correct |
2 ms |
380 KB |
Output is correct |
57 |
Correct |
2 ms |
376 KB |
Output is correct |
58 |
Correct |
186 ms |
101496 KB |
Output is correct |
59 |
Correct |
187 ms |
101508 KB |
Output is correct |
60 |
Correct |
206 ms |
101568 KB |
Output is correct |
61 |
Correct |
194 ms |
101532 KB |
Output is correct |
62 |
Correct |
2 ms |
504 KB |
Output is correct |
63 |
Correct |
88 ms |
101496 KB |
Output is correct |
64 |
Correct |
102 ms |
101624 KB |
Output is correct |
65 |
Correct |
150 ms |
101496 KB |
Output is correct |
66 |
Correct |
200 ms |
101468 KB |
Output is correct |
67 |
Correct |
185 ms |
101556 KB |
Output is correct |
68 |
Correct |
193 ms |
101760 KB |
Output is correct |
69 |
Correct |
193 ms |
101560 KB |
Output is correct |
70 |
Correct |
192 ms |
101496 KB |
Output is correct |
71 |
Correct |
184 ms |
101532 KB |
Output is correct |
72 |
Correct |
2 ms |
256 KB |
Output is correct |
73 |
Correct |
2 ms |
376 KB |
Output is correct |