Submission #888902

#TimeUsernameProblemLanguageResultExecution timeMemory
888902hafoGrowing Vegetable is Fun 3 (JOI19_ho_t3)C++14
75 / 100
1056 ms780372 KiB
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define pb push_back
#define pa pair<int, int>
#define pall pair<ll, int>
#define fi first
#define se second
#define TASK "test"
#define Size(x) (int) x.size()
#define all(x) x.begin(), x.end()
using namespace std;

template<typename T1, typename T2> bool mini (T1 &a, T2 b) {if(a > b) a = b; else return 0; return 1;}
template<typename T1, typename T2> bool maxi (T1 &a, T2 b) {if(a < b) a = b; else return 0; return 1;}

const int MOD = 1e9 + 7;
const int LOG = 20;
const int maxn = 400 + 7;
const ll oo = 1e9 + 69;

int n, dp[maxn][maxn][maxn][3];
char a[maxn];
vector<int> pos[3];

int convert(char ch) {
    if(ch == 'R') return 0;
    if(ch == 'G') return 1;
    return 2;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    //freopen(TASK".inp", "r", stdin);
    //freopen(TASK".out", "w", stdout);

    cin>>n;
    for(int i = 1; i <= n; i++) {
        cin>>a[i];
        pos[convert(a[i])].pb(i);
    }

    for(int i = 0; i <= n; i++) {
        for(int red = 0; red <= Size(pos[0]); red++) {
            for(int green = 0; green <= Size(pos[1]); green++) {
                for(int cur = 0; cur < 3; cur++) dp[i][red][green][cur] = oo;
            }
        }
    }

    dp[0][0][0][0] = 0;
    dp[0][0][0][1] = 0;
    dp[0][0][0][2] = 0;

    for(int i = 0; i < n; i++) {
        for(int red = 0; red <= Size(pos[0]); red++) {
            for(int green = 0; green <= Size(pos[1]); green++) {
                int yellow = i - red - green;
                if(yellow < 0 || yellow > Size(pos[2])) continue;
                for(int cur = 0; cur < 3; cur++) {
                    int cost;
                    if(cur != 0 && red < Size(pos[0])) {
                        cost = 0;
                        int p = pos[0][red];
                        for(int j = 0; j < red; j++) cost += (p < pos[0][j]);
                        for(int j = 0; j < green; j++) cost += (p < pos[1][j]);
                        for(int j = 0; j < yellow; j++) cost += (p < pos[2][j]);
                        mini(dp[i + 1][red + 1][green][0], dp[i][red][green][cur] + cost);
                    }

                    if(cur != 1 && green < Size(pos[1])) {
                        cost = 0;
                        int p = pos[1][green];
                        for(int j = 0; j < red; j++) cost += (p < pos[0][j]);
                        for(int j = 0; j < green; j++) cost += (p < pos[1][j]);
                        for(int j = 0; j < yellow; j++) cost += (p < pos[2][j]);
                        mini(dp[i + 1][red][green + 1][1], dp[i][red][green][cur] + cost);
                    }

                    if(cur != 2 && yellow < Size(pos[2])) {
                        cost = 0;
                        int p = pos[2][yellow];
                        for(int j = 0; j < red; j++) cost += (p < pos[0][j]);
                        for(int j = 0; j < green; j++) cost += (p < pos[1][j]);
                        for(int j = 0; j < yellow; j++) cost += (p < pos[2][j]);
                        mini(dp[i + 1][red][green][2], dp[i][red][green][cur] + cost);
                    } 
                }
            }
        }
    }

    int ans = oo;
    for(int i = 0; i < 3; i++) mini(ans, dp[n][Size(pos[0])][Size(pos[1])][i]);
    cout<<(ans == oo ? -1:ans);
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...