Submission #1180454

#TimeUsernameProblemLanguageResultExecution timeMemory
1180454Zbyszek99Growing Vegetable is Fun 3 (JOI19_ho_t3)C++20
100 / 100
284 ms691400 KiB
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define ff first
#define ss second
#define vl vector<long long>
#define vi vector<int>
#define pii pair<int,int>
#define pll pair<long long, long long>
#define pb push_back
#define rep(i, b) for(int i = 0; i < (b); ++i)
#define rep2(i,a,b) for(int i = a; i <= (b); ++i)
#define rep3(i,a,b,c) for(int i = a; i <= (b); i+=c)
#define count_bits(x) __builtin_popcountll((x))
#define all(x) (x).begin(),(x).end()
#define siz(x) (int)(x).size()
#define forall(it,x) for(auto& it:(x))
using namespace std;
//mt19937 mt;void random(){mt.seed(chrono::time_point_cast<chrono::milliseconds>(chrono::high_resolution_clock::now()).time_since_epoch().count());}
//ll rand(ll a, ll b) {return a + (mt() % (b-a+1));}
const int INF = 1e9+50;
const ll INF_L = 1e18+40;
const ll MOD = 1e9+7;

ll dp[401][401][401][3];

int arr[401];
vi pozy[3];
int pref[401][3];

int main()
{
    //ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    //random();
    int n;
    string s;
    cin >> n >> s;
    rep2(i,1,n)
    {
        if(s[i-1] == 'R') arr[i] = 0;
        if(s[i-1] == 'Y') arr[i] = 1;
        if(s[i-1] == 'G') arr[i] = 2;
    }
    int c0 = 0;
    int c1 = 0;
    int c2 = 0;
    rep2(i,1,n)
    {
        if(arr[i] == 0) c0++;
        else if(arr[i] == 1) c1++;
        else c2++;
        pref[i][0] = c0;
        pref[i][1] = c1;
        pref[i][2] = c2;
        pozy[arr[i]].pb(i);
    }
    rep2(a,0,400) rep2(b,0,400) rep(k,3) dp[0][a][b][k] = 1e9;
    dp[0][0][0][0] = 0;
    dp[0][0][0][1] = 0;
    dp[0][0][0][2] = 0;
  //  cout << "xd\n";
    rep2(p,1,n)
    {
        rep(c0,siz(pozy[0])+1)
        {
            rep(c1,siz(pozy[1])+1)
            {
                int c2 = p-c0-c1;
                rep(k,3) dp[p][c0][c1][k] = 1e9;
               // cout << p << " " << c0 << " " << c1 << " " << c2 << " xd\n";
                if(c2 < 0 || c2 > siz(pozy[2]))
                {
                    continue;
                }
                // prev = 0;
                if(c0 > 0)
                {
                    ll cost = pozy[0][c0-1]-1 - min(c1,pref[pozy[0][c0-1]][1]) - min(c2,pref[pozy[0][c0-1]][2]) - min(c0-1,pref[pozy[0][c0-1]][0]);
                    dp[p][c0][c1][0] = min(dp[p][c0][c1][0],cost + min(dp[p-1][c0-1][c1][1],dp[p-1][c0-1][c1][2]));     
                }
                else
                {
                    dp[p][c0][c1][0] = 1e9;
                }
                //prev = 1
                if(c1 > 0)
                {
                    ll cost = pozy[1][c1-1]-1 - min(c1-1,pref[pozy[1][c1-1]][1]) - min(c2,pref[pozy[1][c1-1]][2]) - min(c0,pref[pozy[1][c1-1]][0]);
                    dp[p][c0][c1][1] = min(dp[p][c0][c1][1],cost + min(dp[p-1][c0][c1-1][0],dp[p-1][c0][c1-1][2]));     
                }
                else
                {
                    dp[p][c0][c1][1] = 1e9;
                }
                // prev = 2
                if(c2 > 0)
                {
                    ll cost = pozy[2][c2-1]-1 - min(c1,pref[pozy[2][c2-1]][1]) - min(c2-1,pref[pozy[2][c2-1]][2]) - min(c0,pref[pozy[2][c2-1]][0]);
                    dp[p][c0][c1][2] = min(dp[p][c0][c1][2],cost + min(dp[p-1][c0][c1][1],dp[p-1][c0][c1][0]));     
                }
                else
                {
                    dp[p][c0][c1][2] = 1e9;
                }
               // cout << p << " " << c0 << " " << c1 << " " << c2 << " " << dp[p][c0][c1][0] << " " << dp[p][c0][c1][1] << " " << dp[p][c0][c1][2] << " dp\n";
            }
        }
    }
    ll ans = 1e9;
    rep(k,3) ans = min(ans,dp[n][siz(pozy[0])][siz(pozy[1])][k]);
    if(ans != 1e9) cout << ans;
    else cout << -1;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...