This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 costG(ll l,ll x) {
ll ans=0;
FOR(i,0,l){
if(G[i]>x)ans++;
}
return ans;
}
ll costR(ll l,ll x) {
ll ans=0;
FOR(i,0,l){
if(R[i]>x)ans++;
}
return ans;
}
ll costY(ll l,ll x) {
ll ans=0;
FOR(i,0,l){
if(Y[i]>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;
/* */
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 (stderr)
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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |