#define LOCAL
#include "bits/stdc++.h"
using namespace std;
#define FAST ios_base::sync_with_stdio(false); cin.tie(0);
#define LLINF ((long long) 1e18)//1234567890987654321
#define INF 1234567890ll
#define pb push_back
#define eb emplace_back
#define ins insert
#define f first
#define s second
#define db 0
#define EPS (1e-7) //0.0000001 the value
#define PI (acos((ld)-1.0))
#define MAXN (300006)
#define ll long long int
#define ld long double
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); //can be used by calling rng() or shuffle(A, A+n, rng)
#define FOR(ii, ss, ee) for(ll ii = ss; ii < (ll)ee; ++ii)
#define space " "
#define cbr cerr << "hi\n"
#define mmst(x, v) memset((x), v, sizeof ((x)))
#define siz(x) ((ll)x.size())
#define ph push
#define btinpct(x) __builtin_popcountll((x))
#define all(x) (x).begin(), (x).end()
#define lbd(x, y) lower_bound(all(x), y)
#define ubd(x, y) upper_bound(all(x), y)
typedef pair <ll, ll> pi;
typedef pair <ll, pi> spi;
typedef pair <pi, pi> dpi;
inline ll rand(ll x, ll y) { ++y; return (rng() % (y-x)) + x; } //inclusivesss
string to_string(char c) {string s(1,c);return s;}string to_string(bool b){return (b ? "true" : "false");}template <typename A, typename B>string to_string(pair<A, B> p) {return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";}template <typename A>string to_string(A v) {bool first = true;string res = "{";for (const auto &x : v) {if (!first) {res += ", ";}first = false;res += to_string(x);}res += "}";return res;}void degug_out() { cerr << endl; }template <typename Head, typename... Tail>void degug_out(Head H, Tail... T) {cerr << " " << to_string(H);degug_out(T...);}inline ll gcd(ll a,ll b){if(a>b)swap(a,b);if(a==0)return b;return gcd(b%a,a);}
#ifdef LOCAL
#define degug(...) cerr << "[" << #__VA_ARGS__ << "]:", degug_out(__VA_ARGS__)
#else
#define degug(...) 42
#define cerr if(0)cout
#endif
ll n,dp[2][406][406][4]; // size of green,red& yellow,
// to do bottom up u have to set unreachable states to inf.
string A; vector<ll>G,R,Y; ll memoG[406][406],memoR[406][406],memoY[406][406];
ll costG(ll l,ll x) { if(~memoG[l][x])return memoG[l][x];
ll ans=0;
FOR(i,0,l){
if(G[i]>x)ans++;
} memoG[l][x]=ans;
return ans;
}
ll costR(ll l,ll x) { if(~memoR[l][x])return memoR[l][x];
ll ans=0;
FOR(i,0,l){
if(R[i]>x)ans++;
} memoR[l][x]=ans;
return ans;
}
ll costY(ll l,ll x) { if(~memoY[l][x])return memoY[l][x];
ll ans=0;
FOR(i,0,l){
if(Y[i]>x)ans++;
} memoY[l][x]=ans;
return ans;
}
void degugg(ll g) { return;
cerr<<g<<": ";
FOR(j,0,siz(R)){
FOR(k,0,siz(Y)){
FOR(p,0,3){
cerr<<g<<' '<<j<<' '<<k<<' '<<p<<' '<<dp[g&1][j][k][p]<<'\n';
}
}
}
}
void check(){
if(siz(G)>(ll)ceill(n/(ld)2)||siz(R)>(ll)ceill(n/(ld)2)||siz(Y)>(ll)ceill(n/(ld)2)){cout<<"-1\n";exit(0);}
}
int main()
{
FAST
cin>>n>>A;
// vector<ll>G,R,Y;
FOR(i,0,siz(A))if(A[i]=='G')G.pb(i);else if(A[i]=='R')R.pb(i);else Y.pb(i); check();
FOR(i,0,2)FOR(j,0,406)FOR(k,0,406)FOR(l,0,4)dp[i][j][k][l]=LLINF;
// where p is a flag
// p=0 means put green
// p=1 means put red
// p=2 means put yellow
/* set base case */
dp[0][0][0][0]=dp[0][0][0][1]=dp[0][0][0][2]=0;
/* */ mmst(memoR,-1);mmst(memoY,-1);mmst(memoG,-1);
FOR(i,0,siz(G)+1) { degugg(i); FOR(j,0,siz(R)+1) FOR(k,0,siz(Y)+1) FOR(p,0,3){
if(i==0&&j==0&&k==0){
continue;
}
dp[i&1][j][k][p]=LLINF;
if(p==0&&i>0){
dp[i&1][j][k][p]=min(dp[i&1][j][k][p],min(dp[(i&1)^1][j][k][1],dp[(i&1)^1][j][k][2])+costR(j,G[i-1])+costY(k,G[i-1]));
}
if(p==1&&j>0){
dp[i&1][j][k][p]=min(dp[i&1][j][k][p],min(dp[i&1][j-1][k][0],dp[i&1][j-1][k][2])+costG(i,R[j-1])+costY(k,R[j-1]));
}
if(p==2&&k>0){
dp[i&1][j][k][p]=min(dp[i&1][j][k][p],min(dp[i&1][j][k-1][0],dp[i&1][j][k-1][1])+costG(i,Y[k-1])+costR(j,Y[k-1]));
}
} }
cout<<min({dp[siz(G)&1][siz(R)][siz(Y)][0],dp[siz(G)&1][siz(R)][siz(Y)][1],dp[siz(G)&1][siz(R)][siz(Y)][2]});
}
Compilation message
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:19:25: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
#define FOR(ii, ss, ee) for(ll ii = ss; ii < (ll)ee; ++ii)
^
joi2019_ho_t3.cpp:82:2: note: in expansion of macro 'FOR'
FOR(i,0,siz(A))if(A[i]=='G')G.pb(i);else if(A[i]=='R')R.pb(i);else Y.pb(i); check();
^~~
joi2019_ho_t3.cpp:82:78: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
FOR(i,0,siz(A))if(A[i]=='G')G.pb(i);else if(A[i]=='R')R.pb(i);else Y.pb(i); check();
^~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
14564 KB |
Output is correct |
2 |
Correct |
13 ms |
14516 KB |
Output is correct |
3 |
Correct |
13 ms |
14464 KB |
Output is correct |
4 |
Correct |
12 ms |
14548 KB |
Output is correct |
5 |
Correct |
13 ms |
14464 KB |
Output is correct |
6 |
Correct |
13 ms |
14464 KB |
Output is correct |
7 |
Correct |
13 ms |
14464 KB |
Output is correct |
8 |
Correct |
13 ms |
14592 KB |
Output is correct |
9 |
Correct |
12 ms |
14576 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
13 ms |
14464 KB |
Output is correct |
12 |
Correct |
12 ms |
14464 KB |
Output is correct |
13 |
Correct |
2 ms |
384 KB |
Output is correct |
14 |
Correct |
13 ms |
14488 KB |
Output is correct |
15 |
Correct |
12 ms |
14464 KB |
Output is correct |
16 |
Correct |
2 ms |
384 KB |
Output is correct |
17 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
14564 KB |
Output is correct |
2 |
Correct |
13 ms |
14516 KB |
Output is correct |
3 |
Correct |
13 ms |
14464 KB |
Output is correct |
4 |
Correct |
12 ms |
14548 KB |
Output is correct |
5 |
Correct |
13 ms |
14464 KB |
Output is correct |
6 |
Correct |
13 ms |
14464 KB |
Output is correct |
7 |
Correct |
13 ms |
14464 KB |
Output is correct |
8 |
Correct |
13 ms |
14592 KB |
Output is correct |
9 |
Correct |
12 ms |
14576 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
13 ms |
14464 KB |
Output is correct |
12 |
Correct |
12 ms |
14464 KB |
Output is correct |
13 |
Correct |
2 ms |
384 KB |
Output is correct |
14 |
Correct |
13 ms |
14488 KB |
Output is correct |
15 |
Correct |
12 ms |
14464 KB |
Output is correct |
16 |
Correct |
2 ms |
384 KB |
Output is correct |
17 |
Correct |
2 ms |
384 KB |
Output is correct |
18 |
Correct |
13 ms |
14592 KB |
Output is correct |
19 |
Correct |
12 ms |
14532 KB |
Output is correct |
20 |
Correct |
13 ms |
14576 KB |
Output is correct |
21 |
Correct |
12 ms |
14464 KB |
Output is correct |
22 |
Correct |
15 ms |
14592 KB |
Output is correct |
23 |
Correct |
13 ms |
14456 KB |
Output is correct |
24 |
Correct |
12 ms |
14484 KB |
Output is correct |
25 |
Correct |
2 ms |
384 KB |
Output is correct |
26 |
Correct |
2 ms |
304 KB |
Output is correct |
27 |
Correct |
13 ms |
14592 KB |
Output is correct |
28 |
Correct |
12 ms |
14464 KB |
Output is correct |
29 |
Correct |
12 ms |
14464 KB |
Output is correct |
30 |
Correct |
12 ms |
14464 KB |
Output is correct |
31 |
Correct |
12 ms |
14464 KB |
Output is correct |
32 |
Correct |
12 ms |
14464 KB |
Output is correct |
33 |
Correct |
2 ms |
384 KB |
Output is correct |
34 |
Correct |
2 ms |
384 KB |
Output is correct |
35 |
Correct |
12 ms |
14592 KB |
Output is correct |
36 |
Correct |
13 ms |
14464 KB |
Output is correct |
37 |
Correct |
12 ms |
14464 KB |
Output is correct |
38 |
Correct |
14 ms |
14592 KB |
Output is correct |
39 |
Correct |
14 ms |
14552 KB |
Output is correct |
40 |
Correct |
2 ms |
384 KB |
Output is correct |
41 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
14464 KB |
Output is correct |
2 |
Correct |
19 ms |
14464 KB |
Output is correct |
3 |
Correct |
20 ms |
14464 KB |
Output is correct |
4 |
Correct |
21 ms |
14620 KB |
Output is correct |
5 |
Correct |
24 ms |
14592 KB |
Output is correct |
6 |
Correct |
21 ms |
14592 KB |
Output is correct |
7 |
Correct |
20 ms |
14464 KB |
Output is correct |
8 |
Correct |
20 ms |
14592 KB |
Output is correct |
9 |
Correct |
19 ms |
14592 KB |
Output is correct |
10 |
Correct |
22 ms |
14464 KB |
Output is correct |
11 |
Correct |
21 ms |
14592 KB |
Output is correct |
12 |
Correct |
14 ms |
14464 KB |
Output is correct |
13 |
Correct |
15 ms |
14592 KB |
Output is correct |
14 |
Correct |
17 ms |
14464 KB |
Output is correct |
15 |
Correct |
2 ms |
284 KB |
Output is correct |
16 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
14564 KB |
Output is correct |
2 |
Correct |
13 ms |
14516 KB |
Output is correct |
3 |
Correct |
13 ms |
14464 KB |
Output is correct |
4 |
Correct |
12 ms |
14548 KB |
Output is correct |
5 |
Correct |
13 ms |
14464 KB |
Output is correct |
6 |
Correct |
13 ms |
14464 KB |
Output is correct |
7 |
Correct |
13 ms |
14464 KB |
Output is correct |
8 |
Correct |
13 ms |
14592 KB |
Output is correct |
9 |
Correct |
12 ms |
14576 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
13 ms |
14464 KB |
Output is correct |
12 |
Correct |
12 ms |
14464 KB |
Output is correct |
13 |
Correct |
2 ms |
384 KB |
Output is correct |
14 |
Correct |
13 ms |
14488 KB |
Output is correct |
15 |
Correct |
12 ms |
14464 KB |
Output is correct |
16 |
Correct |
2 ms |
384 KB |
Output is correct |
17 |
Correct |
2 ms |
384 KB |
Output is correct |
18 |
Correct |
13 ms |
14592 KB |
Output is correct |
19 |
Correct |
12 ms |
14532 KB |
Output is correct |
20 |
Correct |
13 ms |
14576 KB |
Output is correct |
21 |
Correct |
12 ms |
14464 KB |
Output is correct |
22 |
Correct |
15 ms |
14592 KB |
Output is correct |
23 |
Correct |
13 ms |
14456 KB |
Output is correct |
24 |
Correct |
12 ms |
14484 KB |
Output is correct |
25 |
Correct |
2 ms |
384 KB |
Output is correct |
26 |
Correct |
2 ms |
304 KB |
Output is correct |
27 |
Correct |
13 ms |
14592 KB |
Output is correct |
28 |
Correct |
12 ms |
14464 KB |
Output is correct |
29 |
Correct |
12 ms |
14464 KB |
Output is correct |
30 |
Correct |
12 ms |
14464 KB |
Output is correct |
31 |
Correct |
12 ms |
14464 KB |
Output is correct |
32 |
Correct |
12 ms |
14464 KB |
Output is correct |
33 |
Correct |
2 ms |
384 KB |
Output is correct |
34 |
Correct |
2 ms |
384 KB |
Output is correct |
35 |
Correct |
12 ms |
14592 KB |
Output is correct |
36 |
Correct |
13 ms |
14464 KB |
Output is correct |
37 |
Correct |
12 ms |
14464 KB |
Output is correct |
38 |
Correct |
14 ms |
14592 KB |
Output is correct |
39 |
Correct |
14 ms |
14552 KB |
Output is correct |
40 |
Correct |
2 ms |
384 KB |
Output is correct |
41 |
Correct |
2 ms |
384 KB |
Output is correct |
42 |
Correct |
12 ms |
14464 KB |
Output is correct |
43 |
Correct |
19 ms |
14464 KB |
Output is correct |
44 |
Correct |
20 ms |
14464 KB |
Output is correct |
45 |
Correct |
21 ms |
14620 KB |
Output is correct |
46 |
Correct |
24 ms |
14592 KB |
Output is correct |
47 |
Correct |
21 ms |
14592 KB |
Output is correct |
48 |
Correct |
20 ms |
14464 KB |
Output is correct |
49 |
Correct |
20 ms |
14592 KB |
Output is correct |
50 |
Correct |
19 ms |
14592 KB |
Output is correct |
51 |
Correct |
22 ms |
14464 KB |
Output is correct |
52 |
Correct |
21 ms |
14592 KB |
Output is correct |
53 |
Correct |
14 ms |
14464 KB |
Output is correct |
54 |
Correct |
15 ms |
14592 KB |
Output is correct |
55 |
Correct |
17 ms |
14464 KB |
Output is correct |
56 |
Correct |
2 ms |
284 KB |
Output is correct |
57 |
Correct |
2 ms |
384 KB |
Output is correct |
58 |
Correct |
62 ms |
14464 KB |
Output is correct |
59 |
Correct |
59 ms |
14580 KB |
Output is correct |
60 |
Correct |
58 ms |
14580 KB |
Output is correct |
61 |
Correct |
61 ms |
14584 KB |
Output is correct |
62 |
Correct |
2 ms |
384 KB |
Output is correct |
63 |
Correct |
26 ms |
14584 KB |
Output is correct |
64 |
Correct |
56 ms |
14464 KB |
Output is correct |
65 |
Correct |
46 ms |
14592 KB |
Output is correct |
66 |
Correct |
54 ms |
14592 KB |
Output is correct |
67 |
Correct |
50 ms |
14592 KB |
Output is correct |
68 |
Correct |
53 ms |
14564 KB |
Output is correct |
69 |
Correct |
53 ms |
14584 KB |
Output is correct |
70 |
Correct |
57 ms |
14592 KB |
Output is correct |
71 |
Correct |
59 ms |
14464 KB |
Output is correct |
72 |
Correct |
2 ms |
384 KB |
Output is correct |
73 |
Correct |
2 ms |
384 KB |
Output is correct |