#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define dbgv(V) for(auto x:V)cout<<x<<' ';cout<<endl;
#define FOR(a,n) for(int a=0;a<n;a++)
void OJize(){cin.tie(NULL); ios_base::sync_with_stdio(false);}
#define ZZ 401
#define INF 999999
int dp[ZZ][ZZ][ZZ][3];
int acc[3][ZZ];
// (r,g,y) counts, last R/G/B ... +1
vector<int> R, G, Y;
void upd(int r, int g, int y, int prev, int cur){
// Where is the new vegetable?
int pos=0, nr=r, ng=g, ny=y;
if(cur == 0) pos = R[r], nr++;
else if(cur == 1) pos = G[g], ng++;
else pos = Y[y], ny++;
// How much was it pushed to the right?
int rig=0;
if(cur != 0) rig+= max(0, r-acc[0][pos]);
if(cur != 1) rig+= max(0, g-acc[1][pos]);
if(cur != 2) rig+= max(0, y-acc[2][pos]);
// Where is the desired position?
int goal = r+g+y;
int nv = dp[r][g][y][prev] + pos+rig-goal;
dp[nr][ng][ny][cur] = min(dp[nr][ng][ny][cur], nv);
}
int main(){OJize();
int n; cin>>n;
string s; cin>>s;
FOR(i,n){
if(i) FOR(j,3) acc[j][i] = acc[j][i-1];
if(s[i] == 'R') R.push_back(i), acc[0][i]++;
else if(s[i] == 'G') G.push_back(i), acc[1][i]++;
else Y.push_back(i), acc[2][i]++;
}
int RSZ = R.size(), GSZ = G.size(), YSZ = Y.size();
FOR(r,RSZ+1) FOR(g,GSZ+1) FOR(y,YSZ+1) FOR(i,3) dp[r][g][y][i] = INF;
FOR(i,3) dp[0][0][0][i] = 0;
FOR(r,RSZ+1) FOR(g,GSZ+1) FOR(y,YSZ+1){
if(r != RSZ) upd(r,g,y,1,0), upd(r,g,y,2,0);
if(g != GSZ) upd(r,g,y,0,1), upd(r,g,y,2,1);
if(y != YSZ) upd(r,g,y,0,2), upd(r,g,y,1,2);
}
int ans = INF;
FOR(i, 3) ans = min(ans, dp[RSZ][GSZ][YSZ][i]);
cout << ((ans == INF)? -1 : ans);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
380 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
476 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 |
504 KB |
Output is correct |
9 |
Correct |
2 ms |
632 KB |
Output is correct |
10 |
Correct |
2 ms |
760 KB |
Output is correct |
11 |
Correct |
3 ms |
504 KB |
Output is correct |
12 |
Correct |
2 ms |
504 KB |
Output is correct |
13 |
Correct |
2 ms |
376 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 |
548 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
380 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
476 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 |
504 KB |
Output is correct |
9 |
Correct |
2 ms |
632 KB |
Output is correct |
10 |
Correct |
2 ms |
760 KB |
Output is correct |
11 |
Correct |
3 ms |
504 KB |
Output is correct |
12 |
Correct |
2 ms |
504 KB |
Output is correct |
13 |
Correct |
2 ms |
376 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 |
548 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
4 ms |
2424 KB |
Output is correct |
19 |
Correct |
3 ms |
1912 KB |
Output is correct |
20 |
Correct |
4 ms |
2296 KB |
Output is correct |
21 |
Correct |
4 ms |
2424 KB |
Output is correct |
22 |
Correct |
4 ms |
1784 KB |
Output is correct |
23 |
Correct |
4 ms |
2168 KB |
Output is correct |
24 |
Correct |
4 ms |
1656 KB |
Output is correct |
25 |
Correct |
5 ms |
3960 KB |
Output is correct |
26 |
Correct |
5 ms |
4216 KB |
Output is correct |
27 |
Correct |
5 ms |
3116 KB |
Output is correct |
28 |
Correct |
4 ms |
2424 KB |
Output is correct |
29 |
Correct |
4 ms |
2324 KB |
Output is correct |
30 |
Correct |
4 ms |
2296 KB |
Output is correct |
31 |
Correct |
4 ms |
2088 KB |
Output is correct |
32 |
Correct |
4 ms |
2424 KB |
Output is correct |
33 |
Correct |
5 ms |
3836 KB |
Output is correct |
34 |
Correct |
5 ms |
3592 KB |
Output is correct |
35 |
Correct |
4 ms |
2780 KB |
Output is correct |
36 |
Correct |
4 ms |
2040 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 |
2424 KB |
Output is correct |
40 |
Correct |
2 ms |
504 KB |
Output is correct |
41 |
Correct |
4 ms |
2296 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
127 ms |
162936 KB |
Output is correct |
3 |
Correct |
127 ms |
162296 KB |
Output is correct |
4 |
Correct |
128 ms |
162944 KB |
Output is correct |
5 |
Correct |
129 ms |
162936 KB |
Output is correct |
6 |
Correct |
127 ms |
163064 KB |
Output is correct |
7 |
Correct |
126 ms |
162168 KB |
Output is correct |
8 |
Correct |
135 ms |
162252 KB |
Output is correct |
9 |
Correct |
136 ms |
161332 KB |
Output is correct |
10 |
Correct |
128 ms |
163072 KB |
Output is correct |
11 |
Correct |
128 ms |
163064 KB |
Output is correct |
12 |
Correct |
37 ms |
44000 KB |
Output is correct |
13 |
Correct |
62 ms |
77192 KB |
Output is correct |
14 |
Correct |
87 ms |
111352 KB |
Output is correct |
15 |
Correct |
127 ms |
163028 KB |
Output is correct |
16 |
Correct |
129 ms |
162936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
380 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
476 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 |
504 KB |
Output is correct |
9 |
Correct |
2 ms |
632 KB |
Output is correct |
10 |
Correct |
2 ms |
760 KB |
Output is correct |
11 |
Correct |
3 ms |
504 KB |
Output is correct |
12 |
Correct |
2 ms |
504 KB |
Output is correct |
13 |
Correct |
2 ms |
376 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 |
548 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
4 ms |
2424 KB |
Output is correct |
19 |
Correct |
3 ms |
1912 KB |
Output is correct |
20 |
Correct |
4 ms |
2296 KB |
Output is correct |
21 |
Correct |
4 ms |
2424 KB |
Output is correct |
22 |
Correct |
4 ms |
1784 KB |
Output is correct |
23 |
Correct |
4 ms |
2168 KB |
Output is correct |
24 |
Correct |
4 ms |
1656 KB |
Output is correct |
25 |
Correct |
5 ms |
3960 KB |
Output is correct |
26 |
Correct |
5 ms |
4216 KB |
Output is correct |
27 |
Correct |
5 ms |
3116 KB |
Output is correct |
28 |
Correct |
4 ms |
2424 KB |
Output is correct |
29 |
Correct |
4 ms |
2324 KB |
Output is correct |
30 |
Correct |
4 ms |
2296 KB |
Output is correct |
31 |
Correct |
4 ms |
2088 KB |
Output is correct |
32 |
Correct |
4 ms |
2424 KB |
Output is correct |
33 |
Correct |
5 ms |
3836 KB |
Output is correct |
34 |
Correct |
5 ms |
3592 KB |
Output is correct |
35 |
Correct |
4 ms |
2780 KB |
Output is correct |
36 |
Correct |
4 ms |
2040 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 |
2424 KB |
Output is correct |
40 |
Correct |
2 ms |
504 KB |
Output is correct |
41 |
Correct |
4 ms |
2296 KB |
Output is correct |
42 |
Correct |
2 ms |
376 KB |
Output is correct |
43 |
Correct |
127 ms |
162936 KB |
Output is correct |
44 |
Correct |
127 ms |
162296 KB |
Output is correct |
45 |
Correct |
128 ms |
162944 KB |
Output is correct |
46 |
Correct |
129 ms |
162936 KB |
Output is correct |
47 |
Correct |
127 ms |
163064 KB |
Output is correct |
48 |
Correct |
126 ms |
162168 KB |
Output is correct |
49 |
Correct |
135 ms |
162252 KB |
Output is correct |
50 |
Correct |
136 ms |
161332 KB |
Output is correct |
51 |
Correct |
128 ms |
163072 KB |
Output is correct |
52 |
Correct |
128 ms |
163064 KB |
Output is correct |
53 |
Correct |
37 ms |
44000 KB |
Output is correct |
54 |
Correct |
62 ms |
77192 KB |
Output is correct |
55 |
Correct |
87 ms |
111352 KB |
Output is correct |
56 |
Correct |
127 ms |
163028 KB |
Output is correct |
57 |
Correct |
129 ms |
162936 KB |
Output is correct |
58 |
Correct |
179 ms |
75572 KB |
Output is correct |
59 |
Correct |
196 ms |
95740 KB |
Output is correct |
60 |
Correct |
189 ms |
87160 KB |
Output is correct |
61 |
Correct |
186 ms |
83992 KB |
Output is correct |
62 |
Correct |
136 ms |
160692 KB |
Output is correct |
63 |
Correct |
147 ms |
160172 KB |
Output is correct |
64 |
Correct |
194 ms |
150184 KB |
Output is correct |
65 |
Correct |
219 ms |
138744 KB |
Output is correct |
66 |
Correct |
185 ms |
85368 KB |
Output is correct |
67 |
Correct |
183 ms |
82140 KB |
Output is correct |
68 |
Correct |
189 ms |
87928 KB |
Output is correct |
69 |
Correct |
188 ms |
84744 KB |
Output is correct |
70 |
Correct |
202 ms |
91768 KB |
Output is correct |
71 |
Correct |
185 ms |
83224 KB |
Output is correct |
72 |
Correct |
71 ms |
41208 KB |
Output is correct |
73 |
Correct |
11 ms |
2808 KB |
Output is correct |