#include <bits/stdc++.h>
typedef long long ll;
#define pb push_back
#define MS(x,y) memset((x),(y),sizeof((x)))
#define MN 1000000007
using namespace std;
const int MAXN = 401;
vector<int> pos[3];
string org;
int pref[3][MAXN];
int n;
int dp[3][MAXN][MAXN][MAXN];
int convI(char c) {
if (c == 'R') return 0;
else if (c == 'G') return 1;
else if (c == 'Y') return 2;
}
char convC(int b) {
if (b == 0) return 'R';
else if (b == 1) return 'G';
else if (b == 2) return 'Y';
}
int kolkuPrefLast(int cnt, int koj) {
if (cnt == -1) return 0;
else return pref[koj][pos[koj][cnt]];
}
int rec(int last, int cntR, int cntG, int cntY) {
if (cntR+cntG+cntY == n) return 0;
if (dp[last][cntR][cntG][cntY] != -1) return dp[last][cntR][cntG][cntY];
int ret = INT_MAX/2;
int tmp[3] = {cntR,cntG,cntY};
for (int i = 0; i<3; i++) {
if (last == i && cntR+cntG+cntY != 0) continue;
if (tmp[i] >= pos[i].size()) continue;
int ctr = 0;
for (int j = 0; j<3; j++) {
if (i == j) continue;
ctr += abs(pref[j][pos[i][tmp[i]]] - kolkuPrefLast(tmp[j]-1,j));
}
if (i == 0) ctr += rec(0,cntR+1,cntG,cntY);
else if (i == 1) ctr += rec(1,cntR,cntG+1,cntY);
else if (i == 2) ctr += rec(2,cntR,cntG,cntY+1);
ret = min(ret,ctr);
}
return dp[last][cntR][cntG][cntY] = ret;
}
int main()
{
#if LOCAL_DEBUG
fstream cin("in.txt");
#endif
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
MS(dp,-1);
MS(pref,0);
cin>>n;
cin>>org;
for (int i = 0; i<n; i++) {
for (int j = 0; j<3; j++) {
if (i != 0) pref[j][i] = pref[j][i-1];
if (org[i] == convC(j)) {
pref[j][i]++;
pos[j].pb(i);
}
}
}
int res = rec(0,0,0,0);
if (res == INT_MAX/2) cout<<-1<<endl;
else cout<<res/2<<endl;
return 0;
}
Compilation message
joi2019_ho_t3.cpp: In function 'int rec(int, int, int, int)':
joi2019_ho_t3.cpp:44:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
44 | if (tmp[i] >= pos[i].size()) continue;
| ~~~~~~~^~~~~~~~~~~~~~~~
joi2019_ho_t3.cpp: In function 'int convI(char)':
joi2019_ho_t3.cpp:22:1: warning: control reaches end of non-void function [-Wreturn-type]
22 | }
| ^
joi2019_ho_t3.cpp: In function 'char convC(int)':
joi2019_ho_t3.cpp:28:1: warning: control reaches end of non-void function [-Wreturn-type]
28 | }
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
327 ms |
757384 KB |
Output is correct |
2 |
Correct |
286 ms |
757400 KB |
Output is correct |
3 |
Correct |
289 ms |
757452 KB |
Output is correct |
4 |
Correct |
284 ms |
757428 KB |
Output is correct |
5 |
Correct |
283 ms |
757540 KB |
Output is correct |
6 |
Correct |
283 ms |
757380 KB |
Output is correct |
7 |
Correct |
282 ms |
757304 KB |
Output is correct |
8 |
Correct |
282 ms |
757360 KB |
Output is correct |
9 |
Correct |
281 ms |
757368 KB |
Output is correct |
10 |
Correct |
281 ms |
757424 KB |
Output is correct |
11 |
Correct |
282 ms |
757324 KB |
Output is correct |
12 |
Correct |
280 ms |
757352 KB |
Output is correct |
13 |
Correct |
282 ms |
757356 KB |
Output is correct |
14 |
Correct |
281 ms |
757320 KB |
Output is correct |
15 |
Correct |
284 ms |
757360 KB |
Output is correct |
16 |
Correct |
281 ms |
757432 KB |
Output is correct |
17 |
Correct |
284 ms |
757332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
327 ms |
757384 KB |
Output is correct |
2 |
Correct |
286 ms |
757400 KB |
Output is correct |
3 |
Correct |
289 ms |
757452 KB |
Output is correct |
4 |
Correct |
284 ms |
757428 KB |
Output is correct |
5 |
Correct |
283 ms |
757540 KB |
Output is correct |
6 |
Correct |
283 ms |
757380 KB |
Output is correct |
7 |
Correct |
282 ms |
757304 KB |
Output is correct |
8 |
Correct |
282 ms |
757360 KB |
Output is correct |
9 |
Correct |
281 ms |
757368 KB |
Output is correct |
10 |
Correct |
281 ms |
757424 KB |
Output is correct |
11 |
Correct |
282 ms |
757324 KB |
Output is correct |
12 |
Correct |
280 ms |
757352 KB |
Output is correct |
13 |
Correct |
282 ms |
757356 KB |
Output is correct |
14 |
Correct |
281 ms |
757320 KB |
Output is correct |
15 |
Correct |
284 ms |
757360 KB |
Output is correct |
16 |
Correct |
281 ms |
757432 KB |
Output is correct |
17 |
Correct |
284 ms |
757332 KB |
Output is correct |
18 |
Correct |
284 ms |
757308 KB |
Output is correct |
19 |
Correct |
278 ms |
757448 KB |
Output is correct |
20 |
Correct |
290 ms |
757420 KB |
Output is correct |
21 |
Correct |
277 ms |
757324 KB |
Output is correct |
22 |
Correct |
313 ms |
757428 KB |
Output is correct |
23 |
Correct |
283 ms |
757420 KB |
Output is correct |
24 |
Correct |
278 ms |
757328 KB |
Output is correct |
25 |
Correct |
278 ms |
757336 KB |
Output is correct |
26 |
Correct |
276 ms |
757404 KB |
Output is correct |
27 |
Correct |
316 ms |
757472 KB |
Output is correct |
28 |
Correct |
306 ms |
757424 KB |
Output is correct |
29 |
Correct |
283 ms |
757356 KB |
Output is correct |
30 |
Correct |
290 ms |
757400 KB |
Output is correct |
31 |
Correct |
280 ms |
757468 KB |
Output is correct |
32 |
Correct |
320 ms |
757344 KB |
Output is correct |
33 |
Correct |
278 ms |
757324 KB |
Output is correct |
34 |
Correct |
274 ms |
757324 KB |
Output is correct |
35 |
Correct |
286 ms |
757368 KB |
Output is correct |
36 |
Correct |
287 ms |
757408 KB |
Output is correct |
37 |
Correct |
282 ms |
757480 KB |
Output is correct |
38 |
Correct |
278 ms |
757400 KB |
Output is correct |
39 |
Correct |
276 ms |
757372 KB |
Output is correct |
40 |
Correct |
278 ms |
757328 KB |
Output is correct |
41 |
Correct |
275 ms |
757404 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
275 ms |
757352 KB |
Output is correct |
2 |
Correct |
272 ms |
757468 KB |
Output is correct |
3 |
Correct |
276 ms |
757380 KB |
Output is correct |
4 |
Correct |
283 ms |
757400 KB |
Output is correct |
5 |
Correct |
276 ms |
757404 KB |
Output is correct |
6 |
Correct |
277 ms |
757452 KB |
Output is correct |
7 |
Correct |
277 ms |
757392 KB |
Output is correct |
8 |
Correct |
273 ms |
757356 KB |
Output is correct |
9 |
Correct |
280 ms |
757600 KB |
Output is correct |
10 |
Correct |
272 ms |
757420 KB |
Output is correct |
11 |
Correct |
272 ms |
757420 KB |
Output is correct |
12 |
Correct |
274 ms |
757440 KB |
Output is correct |
13 |
Correct |
299 ms |
757340 KB |
Output is correct |
14 |
Correct |
276 ms |
757416 KB |
Output is correct |
15 |
Correct |
275 ms |
757380 KB |
Output is correct |
16 |
Correct |
276 ms |
757412 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
327 ms |
757384 KB |
Output is correct |
2 |
Correct |
286 ms |
757400 KB |
Output is correct |
3 |
Correct |
289 ms |
757452 KB |
Output is correct |
4 |
Correct |
284 ms |
757428 KB |
Output is correct |
5 |
Correct |
283 ms |
757540 KB |
Output is correct |
6 |
Correct |
283 ms |
757380 KB |
Output is correct |
7 |
Correct |
282 ms |
757304 KB |
Output is correct |
8 |
Correct |
282 ms |
757360 KB |
Output is correct |
9 |
Correct |
281 ms |
757368 KB |
Output is correct |
10 |
Correct |
281 ms |
757424 KB |
Output is correct |
11 |
Correct |
282 ms |
757324 KB |
Output is correct |
12 |
Correct |
280 ms |
757352 KB |
Output is correct |
13 |
Correct |
282 ms |
757356 KB |
Output is correct |
14 |
Correct |
281 ms |
757320 KB |
Output is correct |
15 |
Correct |
284 ms |
757360 KB |
Output is correct |
16 |
Correct |
281 ms |
757432 KB |
Output is correct |
17 |
Correct |
284 ms |
757332 KB |
Output is correct |
18 |
Correct |
284 ms |
757308 KB |
Output is correct |
19 |
Correct |
278 ms |
757448 KB |
Output is correct |
20 |
Correct |
290 ms |
757420 KB |
Output is correct |
21 |
Correct |
277 ms |
757324 KB |
Output is correct |
22 |
Correct |
313 ms |
757428 KB |
Output is correct |
23 |
Correct |
283 ms |
757420 KB |
Output is correct |
24 |
Correct |
278 ms |
757328 KB |
Output is correct |
25 |
Correct |
278 ms |
757336 KB |
Output is correct |
26 |
Correct |
276 ms |
757404 KB |
Output is correct |
27 |
Correct |
316 ms |
757472 KB |
Output is correct |
28 |
Correct |
306 ms |
757424 KB |
Output is correct |
29 |
Correct |
283 ms |
757356 KB |
Output is correct |
30 |
Correct |
290 ms |
757400 KB |
Output is correct |
31 |
Correct |
280 ms |
757468 KB |
Output is correct |
32 |
Correct |
320 ms |
757344 KB |
Output is correct |
33 |
Correct |
278 ms |
757324 KB |
Output is correct |
34 |
Correct |
274 ms |
757324 KB |
Output is correct |
35 |
Correct |
286 ms |
757368 KB |
Output is correct |
36 |
Correct |
287 ms |
757408 KB |
Output is correct |
37 |
Correct |
282 ms |
757480 KB |
Output is correct |
38 |
Correct |
278 ms |
757400 KB |
Output is correct |
39 |
Correct |
276 ms |
757372 KB |
Output is correct |
40 |
Correct |
278 ms |
757328 KB |
Output is correct |
41 |
Correct |
275 ms |
757404 KB |
Output is correct |
42 |
Correct |
275 ms |
757352 KB |
Output is correct |
43 |
Correct |
272 ms |
757468 KB |
Output is correct |
44 |
Correct |
276 ms |
757380 KB |
Output is correct |
45 |
Correct |
283 ms |
757400 KB |
Output is correct |
46 |
Correct |
276 ms |
757404 KB |
Output is correct |
47 |
Correct |
277 ms |
757452 KB |
Output is correct |
48 |
Correct |
277 ms |
757392 KB |
Output is correct |
49 |
Correct |
273 ms |
757356 KB |
Output is correct |
50 |
Correct |
280 ms |
757600 KB |
Output is correct |
51 |
Correct |
272 ms |
757420 KB |
Output is correct |
52 |
Correct |
272 ms |
757420 KB |
Output is correct |
53 |
Correct |
274 ms |
757440 KB |
Output is correct |
54 |
Correct |
299 ms |
757340 KB |
Output is correct |
55 |
Correct |
276 ms |
757416 KB |
Output is correct |
56 |
Correct |
275 ms |
757380 KB |
Output is correct |
57 |
Correct |
276 ms |
757412 KB |
Output is correct |
58 |
Correct |
418 ms |
757500 KB |
Output is correct |
59 |
Correct |
417 ms |
757468 KB |
Output is correct |
60 |
Correct |
419 ms |
757472 KB |
Output is correct |
61 |
Correct |
432 ms |
757480 KB |
Output is correct |
62 |
Correct |
276 ms |
757364 KB |
Output is correct |
63 |
Correct |
278 ms |
757452 KB |
Output is correct |
64 |
Correct |
295 ms |
757496 KB |
Output is correct |
65 |
Correct |
332 ms |
757452 KB |
Output is correct |
66 |
Correct |
422 ms |
757608 KB |
Output is correct |
67 |
Correct |
416 ms |
757592 KB |
Output is correct |
68 |
Correct |
459 ms |
757476 KB |
Output is correct |
69 |
Correct |
435 ms |
757444 KB |
Output is correct |
70 |
Correct |
431 ms |
757440 KB |
Output is correct |
71 |
Correct |
412 ms |
757476 KB |
Output is correct |
72 |
Correct |
281 ms |
757444 KB |
Output is correct |
73 |
Correct |
271 ms |
757452 KB |
Output is correct |