#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define fi first
#define si second
typedef pair<int,int> pi;
typedef tuple<int,int,int> ti;
template<typename T> bool chmin(T &a, T b){return (b < a) ? a = b, 1 : 0;}
template<typename T> bool chmax(T &a, T b){return (b > a) ? a = b, 1 : 0;}
void debug_out() {cerr<<endl;}
template <typename Head, typename... Tail>
void debug_out(Head _H, Tail... _T) {cerr<<" "<<to_string(_H);debug_out(_T...);}
#define debug(...) cerr<<"["<<#__VA_ARGS__<<"]:",debug_out(__VA_ARGS__)
const int N = 410, INF=2e9;
int n, s[N];
int dp[N][N][N][3];
int pref[3][N], pos[3][N];
int rc,gc,yc;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
cin.exceptions(ios::badbit|ios::failbit);
cin >> n;
for (int i = 1; i <= n; ++i) {
char x; cin >> x;
if (x=='R') s[i]=0,++rc,pos[0][rc]=i;
else if (x=='G') s[i]=1,++gc,pos[1][gc]=i;
else s[i]=2,++yc,pos[2][yc]=i;
pref[0][i]=rc;
pref[1][i]=gc;
pref[2][i]=yc;
}
for (int i = 0; i <= n; ++i) {
for (int j = 0; j <= n; ++j) {
for (int k = 0; k <= n; ++k) {
fill(dp[i][j][k],dp[i][j][k]+3,INF);
}
}
}
for (int i = 0; i < 3; ++i) dp[0][0][0][i]=0;
for (int i = 0; i <= rc; ++i) {
for (int j = 0; j <= gc; ++j) {
for (int k = 0; k <= yc; ++k) {
if (i==0&&j==0&k==0) continue;
if (i) { // place red
int og = pos[0][i];
dp[i][j][k][0]=min(dp[i-1][j][k][1],dp[i-1][j][k][2]);
dp[i][j][k][0]+=max(0, pref[1][og]-j)+max(0, pref[2][og]-k);
}
if (j) {
int og = pos[1][j];
dp[i][j][k][1]=min(dp[i][j-1][k][0],dp[i][j-1][k][2]);
dp[i][j][k][1]+=max(0, pref[0][og]-i)+max(0, pref[2][og]-k);
}
if (k) {
int og = pos[2][k];
dp[i][j][k][2]=min(dp[i][j][k-1][0],dp[i][j][k-1][1]);
dp[i][j][k][2]+=max(0, pref[0][og]-i)+max(0, pref[1][og]-j);
}
}
}
}
int ans = INF;
for (int i = 0; i < 3; ++i) chmin(ans, dp[rc][gc][yc][i]);
cout << (ans==INF?-1:ans);
return 0;
}
Compilation message
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:45:28: warning: suggest parentheses around comparison in operand of '&' [-Wparentheses]
45 | if (i==0&&j==0&k==0) continue;
| ~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
852 KB |
Output is correct |
5 |
Correct |
1 ms |
1364 KB |
Output is correct |
6 |
Correct |
1 ms |
1352 KB |
Output is correct |
7 |
Correct |
1 ms |
1364 KB |
Output is correct |
8 |
Correct |
1 ms |
1348 KB |
Output is correct |
9 |
Correct |
1 ms |
1364 KB |
Output is correct |
10 |
Correct |
1 ms |
1348 KB |
Output is correct |
11 |
Correct |
1 ms |
1364 KB |
Output is correct |
12 |
Correct |
1 ms |
1364 KB |
Output is correct |
13 |
Correct |
1 ms |
1344 KB |
Output is correct |
14 |
Correct |
1 ms |
1236 KB |
Output is correct |
15 |
Correct |
1 ms |
1364 KB |
Output is correct |
16 |
Correct |
1 ms |
1356 KB |
Output is correct |
17 |
Correct |
1 ms |
1236 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
852 KB |
Output is correct |
5 |
Correct |
1 ms |
1364 KB |
Output is correct |
6 |
Correct |
1 ms |
1352 KB |
Output is correct |
7 |
Correct |
1 ms |
1364 KB |
Output is correct |
8 |
Correct |
1 ms |
1348 KB |
Output is correct |
9 |
Correct |
1 ms |
1364 KB |
Output is correct |
10 |
Correct |
1 ms |
1348 KB |
Output is correct |
11 |
Correct |
1 ms |
1364 KB |
Output is correct |
12 |
Correct |
1 ms |
1364 KB |
Output is correct |
13 |
Correct |
1 ms |
1344 KB |
Output is correct |
14 |
Correct |
1 ms |
1236 KB |
Output is correct |
15 |
Correct |
1 ms |
1364 KB |
Output is correct |
16 |
Correct |
1 ms |
1356 KB |
Output is correct |
17 |
Correct |
1 ms |
1236 KB |
Output is correct |
18 |
Correct |
7 ms |
18004 KB |
Output is correct |
19 |
Correct |
7 ms |
18004 KB |
Output is correct |
20 |
Correct |
8 ms |
17936 KB |
Output is correct |
21 |
Correct |
7 ms |
17992 KB |
Output is correct |
22 |
Correct |
8 ms |
18084 KB |
Output is correct |
23 |
Correct |
8 ms |
18040 KB |
Output is correct |
24 |
Correct |
8 ms |
17984 KB |
Output is correct |
25 |
Correct |
8 ms |
17996 KB |
Output is correct |
26 |
Correct |
8 ms |
17992 KB |
Output is correct |
27 |
Correct |
8 ms |
17960 KB |
Output is correct |
28 |
Correct |
10 ms |
17988 KB |
Output is correct |
29 |
Correct |
7 ms |
17988 KB |
Output is correct |
30 |
Correct |
9 ms |
18004 KB |
Output is correct |
31 |
Correct |
8 ms |
17972 KB |
Output is correct |
32 |
Correct |
7 ms |
17988 KB |
Output is correct |
33 |
Correct |
7 ms |
17464 KB |
Output is correct |
34 |
Correct |
7 ms |
17380 KB |
Output is correct |
35 |
Correct |
7 ms |
16812 KB |
Output is correct |
36 |
Correct |
8 ms |
17352 KB |
Output is correct |
37 |
Correct |
7 ms |
16212 KB |
Output is correct |
38 |
Correct |
7 ms |
18004 KB |
Output is correct |
39 |
Correct |
7 ms |
18004 KB |
Output is correct |
40 |
Correct |
7 ms |
17364 KB |
Output is correct |
41 |
Correct |
7 ms |
18004 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
364 ms |
775996 KB |
Output is correct |
3 |
Correct |
341 ms |
772044 KB |
Output is correct |
4 |
Correct |
354 ms |
775984 KB |
Output is correct |
5 |
Correct |
340 ms |
776012 KB |
Output is correct |
6 |
Correct |
322 ms |
775968 KB |
Output is correct |
7 |
Correct |
340 ms |
772036 KB |
Output is correct |
8 |
Correct |
326 ms |
772164 KB |
Output is correct |
9 |
Correct |
319 ms |
768324 KB |
Output is correct |
10 |
Correct |
381 ms |
775968 KB |
Output is correct |
11 |
Correct |
329 ms |
775932 KB |
Output is correct |
12 |
Correct |
96 ms |
207232 KB |
Output is correct |
13 |
Correct |
149 ms |
365380 KB |
Output is correct |
14 |
Correct |
216 ms |
529068 KB |
Output is correct |
15 |
Correct |
331 ms |
775924 KB |
Output is correct |
16 |
Correct |
338 ms |
776096 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
852 KB |
Output is correct |
5 |
Correct |
1 ms |
1364 KB |
Output is correct |
6 |
Correct |
1 ms |
1352 KB |
Output is correct |
7 |
Correct |
1 ms |
1364 KB |
Output is correct |
8 |
Correct |
1 ms |
1348 KB |
Output is correct |
9 |
Correct |
1 ms |
1364 KB |
Output is correct |
10 |
Correct |
1 ms |
1348 KB |
Output is correct |
11 |
Correct |
1 ms |
1364 KB |
Output is correct |
12 |
Correct |
1 ms |
1364 KB |
Output is correct |
13 |
Correct |
1 ms |
1344 KB |
Output is correct |
14 |
Correct |
1 ms |
1236 KB |
Output is correct |
15 |
Correct |
1 ms |
1364 KB |
Output is correct |
16 |
Correct |
1 ms |
1356 KB |
Output is correct |
17 |
Correct |
1 ms |
1236 KB |
Output is correct |
18 |
Correct |
7 ms |
18004 KB |
Output is correct |
19 |
Correct |
7 ms |
18004 KB |
Output is correct |
20 |
Correct |
8 ms |
17936 KB |
Output is correct |
21 |
Correct |
7 ms |
17992 KB |
Output is correct |
22 |
Correct |
8 ms |
18084 KB |
Output is correct |
23 |
Correct |
8 ms |
18040 KB |
Output is correct |
24 |
Correct |
8 ms |
17984 KB |
Output is correct |
25 |
Correct |
8 ms |
17996 KB |
Output is correct |
26 |
Correct |
8 ms |
17992 KB |
Output is correct |
27 |
Correct |
8 ms |
17960 KB |
Output is correct |
28 |
Correct |
10 ms |
17988 KB |
Output is correct |
29 |
Correct |
7 ms |
17988 KB |
Output is correct |
30 |
Correct |
9 ms |
18004 KB |
Output is correct |
31 |
Correct |
8 ms |
17972 KB |
Output is correct |
32 |
Correct |
7 ms |
17988 KB |
Output is correct |
33 |
Correct |
7 ms |
17464 KB |
Output is correct |
34 |
Correct |
7 ms |
17380 KB |
Output is correct |
35 |
Correct |
7 ms |
16812 KB |
Output is correct |
36 |
Correct |
8 ms |
17352 KB |
Output is correct |
37 |
Correct |
7 ms |
16212 KB |
Output is correct |
38 |
Correct |
7 ms |
18004 KB |
Output is correct |
39 |
Correct |
7 ms |
18004 KB |
Output is correct |
40 |
Correct |
7 ms |
17364 KB |
Output is correct |
41 |
Correct |
7 ms |
18004 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
364 ms |
775996 KB |
Output is correct |
44 |
Correct |
341 ms |
772044 KB |
Output is correct |
45 |
Correct |
354 ms |
775984 KB |
Output is correct |
46 |
Correct |
340 ms |
776012 KB |
Output is correct |
47 |
Correct |
322 ms |
775968 KB |
Output is correct |
48 |
Correct |
340 ms |
772036 KB |
Output is correct |
49 |
Correct |
326 ms |
772164 KB |
Output is correct |
50 |
Correct |
319 ms |
768324 KB |
Output is correct |
51 |
Correct |
381 ms |
775968 KB |
Output is correct |
52 |
Correct |
329 ms |
775932 KB |
Output is correct |
53 |
Correct |
96 ms |
207232 KB |
Output is correct |
54 |
Correct |
149 ms |
365380 KB |
Output is correct |
55 |
Correct |
216 ms |
529068 KB |
Output is correct |
56 |
Correct |
331 ms |
775924 KB |
Output is correct |
57 |
Correct |
338 ms |
776096 KB |
Output is correct |
58 |
Correct |
387 ms |
775960 KB |
Output is correct |
59 |
Correct |
397 ms |
775892 KB |
Output is correct |
60 |
Correct |
348 ms |
768204 KB |
Output is correct |
61 |
Correct |
339 ms |
772144 KB |
Output is correct |
62 |
Correct |
353 ms |
775900 KB |
Output is correct |
63 |
Correct |
344 ms |
775916 KB |
Output is correct |
64 |
Correct |
333 ms |
775988 KB |
Output is correct |
65 |
Correct |
346 ms |
772184 KB |
Output is correct |
66 |
Correct |
333 ms |
764424 KB |
Output is correct |
67 |
Correct |
348 ms |
775992 KB |
Output is correct |
68 |
Correct |
358 ms |
772164 KB |
Output is correct |
69 |
Correct |
352 ms |
776012 KB |
Output is correct |
70 |
Correct |
342 ms |
775956 KB |
Output is correct |
71 |
Correct |
334 ms |
772200 KB |
Output is correct |
72 |
Correct |
333 ms |
772084 KB |
Output is correct |
73 |
Correct |
334 ms |
775948 KB |
Output is correct |