Submission #716380

#TimeUsernameProblemLanguageResultExecution timeMemory
716380KahouGrowing Vegetable is Fun 3 (JOI19_ho_t3)C++14
60 / 100
606 ms780484 KiB
/* In the name of God, aka Allah */
#include<bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define endl '\n'
#define mk make_pair
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int N = 405;
int n, dp[N][N][N][3];
string s;
vector<int> vc[3];

void solve() {
        cin >> n;
        cin >> s;
        s = '.'+s;
        for (int i = 1; i <= n; i++) {
                int c = (s[i] != 'R') + (s[i] == 'Y');
                vc[c].push_back(i);
        }
        memset(dp, 64, sizeof dp);
        dp[0][0][0][0] = dp[0][0][0][1] = dp[0][0][0][2] = 0;
        for (int sum = 0; sum <= n; sum++) {
                for (int A = 0; A <= sum; A++) {
                        for (int B = 0; A+B <= sum; B++) {
                                int C = sum-A-B;
                                if (A < (int)vc[0].size()) {
                                        int b = lower_bound(vc[1].begin(), vc[1].end(), vc[0][A])-vc[1].begin();
                                        int c = lower_bound(vc[2].begin(), vc[2].end(), vc[0][A])-vc[2].begin();
                                        dp[A+1][B][C][0] = min(dp[A+1][B][C][0], min(dp[A][B][C][1], dp[A][B][C][2]) + vc[0][A]+max(B-b, 0)+max(C-c, 0)-(A+B+C+1));
                                }
                                if (B < (int)vc[1].size()) {
                                        int a = lower_bound(vc[0].begin(), vc[0].end(), vc[1][B])-vc[0].begin();
                                        int c = lower_bound(vc[2].begin(), vc[2].end(), vc[1][B])-vc[2].begin();
                                        dp[A][B+1][C][1] = min(dp[A][B+1][C][1], min(dp[A][B][C][0], dp[A][B][C][2]) + vc[1][B]+max(A-a, 0)+max(C-c, 0)-(A+B+C+1));
                                }
                                if (C < (int)vc[2].size()) {
                                        int a = lower_bound(vc[0].begin(), vc[0].end(), vc[2][C])-vc[0].begin();
                                        int b = lower_bound(vc[1].begin(), vc[1].end(), vc[2][C])-vc[1].begin();
                                        dp[A][B][C+1][2] = min(dp[A][B][C+1][2], min(dp[A][B][C][0], dp[A][B][C][1]) + vc[2][C]+max(A-a,0)+max(B-b, 0)-(A+B+C+1));
                                }
                        }
                }
        }
        int ans = 1e9;
        ans = min(dp[vc[0].size()][vc[1].size()][vc[2].size()][0], ans);
        ans = min(dp[vc[0].size()][vc[1].size()][vc[2].size()][1], ans);
        ans = min(dp[vc[0].size()][vc[1].size()][vc[2].size()][2], ans);
        cout << (ans >= 1e9? -1 : ans) << endl;
}

int main() {
        ios::sync_with_stdio(0), cin.tie(0);
        solve();
        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...