제출 #240989

#제출 시각아이디문제언어결과실행 시간메모리
240989aggu_01000101Growing Vegetable is Fun 3 (JOI19_ho_t3)C++14
100 / 100
500 ms757652 KiB
#include <bits/stdc++.h>
#define INF 100000000
using namespace std;

signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n;
    cin>>n;
    const int N = n+1;
    int dp[N][N][N][3];
    int g[3][N];
    int cc[] = {0, 0, 0};
    int cmp[N][3];
    if(n==1){
        cout<<0<<"\n";
        return 0;
    }
    char c;
    for(int i = 1;i<=n;i++){
        cmp[i][0] = cc[0];
        cmp[i][1] = cc[1];
        cmp[i][2] = cc[2];
        cin>>c;
        if(c=='R') g[0][cc[0]++] = i;
        else if(c=='G') g[1][cc[1]++] = i;
        else g[2][cc[2]++] = i;
    }
    memset(dp , INF , sizeof(dp)) ;
    for(int i = n-1;i>=1;i--){
        for(int aused = n;aused>=0;aused--){
            for(int bused = n;bused >= 0 ;bused--){
                int cused = i - (aused + bused);
                int ause = INF, buse = INF, cuse = INF;
                if(aused>cc[0] || bused>cc[1] || cused>cc[2]) continue;
                if(cused<0) continue;
                int currind;
                //calculate use[0]
                if(cc[0] > aused) {
                    currind = g[0][aused] + max((int) 0, bused - cmp[g[0][aused]][1]) +
                              max((int) 0, cused - cmp[g[0][aused]][2]);
                    ause = (currind - (i + 1));
                    if(i<n-1) ause += dp[i + 1][aused + 1][bused][0];
                }
                //calculate buse
                if(cc[1] > bused) {
                    currind = g[1][bused] + max((int) 0, aused - cmp[g[1][bused]][0]) +
                              max((int) 0, cused - cmp[g[1][bused]][2]);
                    buse = (currind - (i + 1));
                    if(i<n-1) buse += dp[i + 1][aused][bused + 1][1];
                }
                //calculate cuse
                if(cc[2] > cused) {
                    currind = g[2][cused] + max((int) 0, aused - cmp[g[2][cused]][0]) +
                              max((int) 0, bused - cmp[g[2][cused]][1]);
                    cuse = (currind - (i + 1));
                    if(i<n-1) cuse += dp[i + 1][aused][bused][2];
                }

                dp[i][aused][bused][0] = min(buse, cuse);
                dp[i][aused][bused][1] = min(ause, cuse);
                dp[i][aused][bused][2] = min(buse, ause);
            }
        }
    }
    int ans = INF;
    if(cc[0]) {
        ans = min(ans, (g[0][0] - 1) + dp[1][1][0][0]);
    }
    if(cc[1]) {
        ans = min(ans, (g[1][0] - 1) + dp[1][0][1][1]);
    }
    if(cc[2]) {
        ans = min(ans, (g[2][0] - 1) + dp[1][0][0][2]);
    }
    if(ans>=INF) ans = -1;
    cout<<ans<<"\n";
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...