#include <bits/stdc++.h>
//#define int long long
#define P(x) do {if(debug) cout << x << endl;} while(false)
#define H(x) P(#x << ": " << x)
#define FR(i, a, b) for(int i = (a); i < (b); ++i)
#define F(i, n) FR(i, 0, n)
#define DR(i, a, b) for(int i = (b); i --> (a);)
#define D(i, n) DR(i, 0, n)
#define S(s) (int)(s).size()
#define ALL(x) (x).begin(), (x).end()
#define MI(x, a) (x) = min(x, a)
#define MA(x, a) (x) = max(x, a)
#define V vector
#define pb push_back
#define mp make_pair
using namespace std;
const bool debug = 0;
const int inf = 1e9;
int n;
vector<int> color;
vector<int> pos[3];
int dp[400][400][400][3];
bool vis[400][400][400][3];
vector<vector<int> > pref;
int offset(int a[3], int i){
int res = 0;
F(j,3){
res+=max(0,a[j] - pref[j][i]);
}
return res;
}
int solve(int a[3], int last){
int g = a[0], y = a[1], r = a[2];
H(g); H(y); H(r); H(last);
int j = g + y + r;
if(vis[g][y][r][last]){
P("visited, return: "<<dp[g][y][r][last]);
return dp[g][y][r][last];
}
if(g+y+r==n){
P("reached end");
return 0;
}
vis[g][y][r][last] = 1;
dp[g][y][r][last] = inf;
for(int i = 0; i<3; i++) if(i!=last && a[i] < pos[i].size()){
H(i);
H(offset(a,pos[i][a[i]]));
int extra = pos[i][a[i]] + offset(a,pos[i][a[i]]) - j;
a[i] ++;
dp[g][y][r][last] = min(dp[g][y][r][last], solve(a,i)+extra);
P("a");
a[i] --;
P("c");
}
P("d");
P(g<<" "<<y<<" "<<r<<" "<<last<<" returns "<< dp[g][y][r][last]);
return dp[g][y][r][last];
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin>>n;
map<char,int> mp;
color.resize(n);
mp['G'] = 0;
mp['Y'] = 1;
mp['R'] = 2;
pref.resize(3, vector<int> (n+1,0));
for(int i = 0; i<n; i++){
char c;
cin>>c;
color[i] = mp[c];
pos[mp[c]].push_back(i);
F(j,3){
pref[j][i+1] = pref[j][i];
}
pref[mp[c]][i+1]++;
}
int a[] = {0,0,0};
int ans = min(solve(a,0),solve(a,1));
if(ans < 1e6){
cout<<ans<<endl;
}
else{
cout<<-1<<endl;
}
return 0;
}
Compilation message
joi2019_ho_t3.cpp: In function 'int solve(int*, int)':
joi2019_ho_t3.cpp:49:46: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i<3; i++) if(i!=last && a[i] < pos[i].size()){
~~~~~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
512 KB |
Output is correct |
5 |
Correct |
5 ms |
512 KB |
Output is correct |
6 |
Correct |
5 ms |
512 KB |
Output is correct |
7 |
Correct |
4 ms |
512 KB |
Output is correct |
8 |
Correct |
5 ms |
512 KB |
Output is correct |
9 |
Correct |
5 ms |
512 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
512 KB |
Output is correct |
12 |
Correct |
5 ms |
512 KB |
Output is correct |
13 |
Correct |
5 ms |
512 KB |
Output is correct |
14 |
Correct |
4 ms |
512 KB |
Output is correct |
15 |
Correct |
5 ms |
512 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
512 KB |
Output is correct |
5 |
Correct |
5 ms |
512 KB |
Output is correct |
6 |
Correct |
5 ms |
512 KB |
Output is correct |
7 |
Correct |
4 ms |
512 KB |
Output is correct |
8 |
Correct |
5 ms |
512 KB |
Output is correct |
9 |
Correct |
5 ms |
512 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
512 KB |
Output is correct |
12 |
Correct |
5 ms |
512 KB |
Output is correct |
13 |
Correct |
5 ms |
512 KB |
Output is correct |
14 |
Correct |
4 ms |
512 KB |
Output is correct |
15 |
Correct |
5 ms |
512 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
7 ms |
2560 KB |
Output is correct |
19 |
Correct |
6 ms |
2944 KB |
Output is correct |
20 |
Correct |
6 ms |
2304 KB |
Output is correct |
21 |
Correct |
6 ms |
2816 KB |
Output is correct |
22 |
Correct |
6 ms |
2224 KB |
Output is correct |
23 |
Correct |
7 ms |
3072 KB |
Output is correct |
24 |
Correct |
6 ms |
2944 KB |
Output is correct |
25 |
Correct |
6 ms |
768 KB |
Output is correct |
26 |
Correct |
6 ms |
896 KB |
Output is correct |
27 |
Correct |
6 ms |
1920 KB |
Output is correct |
28 |
Correct |
6 ms |
2688 KB |
Output is correct |
29 |
Correct |
6 ms |
2560 KB |
Output is correct |
30 |
Correct |
7 ms |
3072 KB |
Output is correct |
31 |
Correct |
7 ms |
3200 KB |
Output is correct |
32 |
Correct |
6 ms |
2944 KB |
Output is correct |
33 |
Correct |
5 ms |
1024 KB |
Output is correct |
34 |
Correct |
5 ms |
1280 KB |
Output is correct |
35 |
Correct |
6 ms |
2304 KB |
Output is correct |
36 |
Correct |
6 ms |
2688 KB |
Output is correct |
37 |
Correct |
8 ms |
2944 KB |
Output is correct |
38 |
Correct |
6 ms |
3072 KB |
Output is correct |
39 |
Correct |
6 ms |
2944 KB |
Output is correct |
40 |
Correct |
5 ms |
768 KB |
Output is correct |
41 |
Correct |
5 ms |
1024 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
6 ms |
2944 KB |
Output is correct |
3 |
Correct |
6 ms |
2944 KB |
Output is correct |
4 |
Correct |
6 ms |
2944 KB |
Output is correct |
5 |
Correct |
6 ms |
2944 KB |
Output is correct |
6 |
Correct |
6 ms |
2944 KB |
Output is correct |
7 |
Correct |
6 ms |
2944 KB |
Output is correct |
8 |
Correct |
6 ms |
2944 KB |
Output is correct |
9 |
Correct |
6 ms |
2944 KB |
Output is correct |
10 |
Correct |
6 ms |
2988 KB |
Output is correct |
11 |
Correct |
6 ms |
2944 KB |
Output is correct |
12 |
Correct |
5 ms |
1664 KB |
Output is correct |
13 |
Correct |
6 ms |
2048 KB |
Output is correct |
14 |
Correct |
6 ms |
2432 KB |
Output is correct |
15 |
Correct |
6 ms |
2944 KB |
Output is correct |
16 |
Correct |
6 ms |
2944 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
512 KB |
Output is correct |
5 |
Correct |
5 ms |
512 KB |
Output is correct |
6 |
Correct |
5 ms |
512 KB |
Output is correct |
7 |
Correct |
4 ms |
512 KB |
Output is correct |
8 |
Correct |
5 ms |
512 KB |
Output is correct |
9 |
Correct |
5 ms |
512 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
512 KB |
Output is correct |
12 |
Correct |
5 ms |
512 KB |
Output is correct |
13 |
Correct |
5 ms |
512 KB |
Output is correct |
14 |
Correct |
4 ms |
512 KB |
Output is correct |
15 |
Correct |
5 ms |
512 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
7 ms |
2560 KB |
Output is correct |
19 |
Correct |
6 ms |
2944 KB |
Output is correct |
20 |
Correct |
6 ms |
2304 KB |
Output is correct |
21 |
Correct |
6 ms |
2816 KB |
Output is correct |
22 |
Correct |
6 ms |
2224 KB |
Output is correct |
23 |
Correct |
7 ms |
3072 KB |
Output is correct |
24 |
Correct |
6 ms |
2944 KB |
Output is correct |
25 |
Correct |
6 ms |
768 KB |
Output is correct |
26 |
Correct |
6 ms |
896 KB |
Output is correct |
27 |
Correct |
6 ms |
1920 KB |
Output is correct |
28 |
Correct |
6 ms |
2688 KB |
Output is correct |
29 |
Correct |
6 ms |
2560 KB |
Output is correct |
30 |
Correct |
7 ms |
3072 KB |
Output is correct |
31 |
Correct |
7 ms |
3200 KB |
Output is correct |
32 |
Correct |
6 ms |
2944 KB |
Output is correct |
33 |
Correct |
5 ms |
1024 KB |
Output is correct |
34 |
Correct |
5 ms |
1280 KB |
Output is correct |
35 |
Correct |
6 ms |
2304 KB |
Output is correct |
36 |
Correct |
6 ms |
2688 KB |
Output is correct |
37 |
Correct |
8 ms |
2944 KB |
Output is correct |
38 |
Correct |
6 ms |
3072 KB |
Output is correct |
39 |
Correct |
6 ms |
2944 KB |
Output is correct |
40 |
Correct |
5 ms |
768 KB |
Output is correct |
41 |
Correct |
5 ms |
1024 KB |
Output is correct |
42 |
Correct |
5 ms |
384 KB |
Output is correct |
43 |
Correct |
6 ms |
2944 KB |
Output is correct |
44 |
Correct |
6 ms |
2944 KB |
Output is correct |
45 |
Correct |
6 ms |
2944 KB |
Output is correct |
46 |
Correct |
6 ms |
2944 KB |
Output is correct |
47 |
Correct |
6 ms |
2944 KB |
Output is correct |
48 |
Correct |
6 ms |
2944 KB |
Output is correct |
49 |
Correct |
6 ms |
2944 KB |
Output is correct |
50 |
Correct |
6 ms |
2944 KB |
Output is correct |
51 |
Correct |
6 ms |
2988 KB |
Output is correct |
52 |
Correct |
6 ms |
2944 KB |
Output is correct |
53 |
Correct |
5 ms |
1664 KB |
Output is correct |
54 |
Correct |
6 ms |
2048 KB |
Output is correct |
55 |
Correct |
6 ms |
2432 KB |
Output is correct |
56 |
Correct |
6 ms |
2944 KB |
Output is correct |
57 |
Correct |
6 ms |
2944 KB |
Output is correct |
58 |
Correct |
212 ms |
109560 KB |
Output is correct |
59 |
Correct |
249 ms |
96224 KB |
Output is correct |
60 |
Correct |
218 ms |
96632 KB |
Output is correct |
61 |
Correct |
203 ms |
104056 KB |
Output is correct |
62 |
Correct |
8 ms |
6528 KB |
Output is correct |
63 |
Correct |
12 ms |
11264 KB |
Output is correct |
64 |
Correct |
48 ms |
36728 KB |
Output is correct |
65 |
Correct |
100 ms |
60284 KB |
Output is correct |
66 |
Correct |
203 ms |
100216 KB |
Output is correct |
67 |
Correct |
203 ms |
114936 KB |
Output is correct |
68 |
Correct |
214 ms |
101752 KB |
Output is correct |
69 |
Correct |
208 ms |
102504 KB |
Output is correct |
70 |
Correct |
205 ms |
102904 KB |
Output is correct |
71 |
Correct |
200 ms |
90204 KB |
Output is correct |
72 |
Correct |
16 ms |
11648 KB |
Output is correct |
73 |
Correct |
8 ms |
7168 KB |
Output is correct |