This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
template<typename T>using ordered_set = tree<T,null_type,less_equal<T>,rb_tree_tag,tree_order_statistics_node_update>;//find_by_order(ind);
//order_of_key()
#define int long long
#define fast ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
#define pb push_back
#define yes "YES"
#define no "NO"
#define bigg INT_MAX
#define debug(x) cout<<(#x)<<" = " <<x<<endl;
#define all(x) x.begin(),x.end()
#define sz size()
#define nn '\n'
#define mms(x,y) memset(x,y,sizeof(x))
#define forr(i,j,n) for (int i=j; i<n; i++)
#define forn(i,j,n) for (int i=j; i>n; i--)
#define fi first
#define se second
#define la "LA"
#define cinn(x,y) for(int i=0; i<y; i++) cin>>x[i];
#define pii pair<int,int>
#define vi vector<int>
int dp[401][401][401][3];
vi a[3];
int R=0, G=0, Y=0;
int br[401], bg[401], by[401];
int n;
int fn(int r, int g, int y, int prev)
{
// cout<<r<<' '<<g<<' '<<y<<endl;
if (r+g+y==n) return 0;
//int x=r+g+y;
if (dp[r][g][y][prev]!=0) return dp[r][g][y][prev];
int c=bigg;
if (r<R && prev!=0) c=min(c,(fn(r+1,g,y,0)+max(0ll,g-bg[a[0][r]])+max(0ll, y-by[a[0][r]])));
if (g<G && prev!=1) c=min(c,(fn(r,g+1,y,1)+max(0ll,r-br[a[1][g]])+max(0ll, y-by[a[1][g]])));
if (y<Y && prev!=2) c=min(c,(fn(r,g,y+1,2)+max(0ll,r-br[a[2][y]])+max(0ll, g-bg[a[2][y]])));
// cout<<no<<r<<' '<<g<<' '<<y<<' '<<c<<endl;
return dp[r][g][y][prev]=c;
}
signed main()
{
// #ifndef LOCAL
// freopen("lifeguards.in","r",stdin);
// freopen("lifeguards.out","w", stdout);
// #endif
fast
// int n;
cin>>n;
string s;
cin>>s;
//R G y
forr(i,0,n)
{
if (s[i]=='R') {R++; a[0].pb(i); br[i]++;}
else if (s[i]=='G') {G++; a[1].pb(i); bg[i]++;}
else {Y++; a[2].pb(i); by[i]++;}
if (i!=0) { br[i]+=br[i-1]; bg[i]+=bg[i-1]; by[i]+=by[i-1];}
//cout<<br[i]<<' '<<bg[i]<<' '<<by[i]<<endl;
}
// cout<<"R"<<endl;
// for(auto y:a[0]) cout<<y<<' ';
// cout<<endl;
// cout<<"G"<<endl;
// for(auto y:a[1]) cout<<y<<' ';
// cout<<endl;
// cout<<"Y"<<endl;
// for(auto y:a[2]) cout<<y<<' ';
// cout<<endl;
int o=min(fn(0,0,0,0),min(fn(0,0,0,1),fn(0,0,0,2)));
if (o==bigg) o=-1;
cout<<o;
// int dp[n][n][n];
return 0;
}
# | 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... |