Submission #396078

# Submission time Handle Problem Language Result Execution time Memory
396078 2021-04-29T12:26:45 Z qwerasdfzxcl Growing Vegetable is Fun 3 (JOI19_ho_t3) C++14
100 / 100
116 ms 154224 KB
#include <bits/stdc++.h>

typedef long long ll;
using namespace std;
int dp[404][202][202][3], val[404][202][202][3];
vector<int> pos[3];
string s;

int f(char x){
    if (x=='R') return 0;
    else if (x=='G') return 1;
    else if (x=='Y') return 2;
    return -1;
}

bool CHK(int i, int j, int k, int z){
    int l = i-j-k;
    i--;
    if (z==0) j--;
    else if (z==1) k--;
    else if (z==2) l--;
    if (j<0 || k<0 || l<0) return 0;
    if (j*2>i+1 || k*2>i+1 || l*2>i+1) return 0;
    return 1;
}

int main(){
    cin.tie(NULL);
    ios_base::sync_with_stdio(false);
    int n;
    cin >> n >> s;
    for (int i=0;i<n;i++){
        pos[f(s[i])].push_back(i);
    }
    int r = pos[0].size(), g = pos[1].size(), y = pos[2].size();
    if (r*2>n+1 || g*2>n+1 || y*2>n+1){
        printf("-1\n");
        return 0;
    }
    bool chk1=1;
    for (int i=1;i<n;i++) if (s[i]==s[i-1]) chk1=0;
    if (chk1){
        printf("0\n");
        return 0;
    }
    if (!pos[0].empty()) dp[1][1][0][0] = val[1][1][0][0] = pos[0][0];
    if (!pos[1].empty()) dp[1][0][1][1] = val[1][0][1][1] = pos[1][0];
    if (!pos[2].empty()) dp[1][0][0][2] = val[1][0][0][2] = pos[2][0];
    for (int i=2;i<=n;i++){
        for (int j=0;j<=min(i, r);j++){
            for (int k=0;k<=min(i, g);k++){
                int l = i-j-k;
                if (l>y) continue;
                if (l<0) break;
                if (j){
                    if (!k && !l) val[i][j][k][0] = pos[0][i-1]-i+1;
                    else if (k) val[i][j][k][0] = val[i-1][j][k-1][0] - (pos[1][k-1]<pos[0][j-1]);
                    else val[i][j][k][0] = val[i-1][j][k][0] - (pos[2][l-1]<pos[0][j-1]);
                }
                if (k){
                    if (!j && !l) val[i][j][k][1] = pos[1][i-1]-i+1;
                    else if (j) val[i][j][k][1] = val[i-1][j-1][k][1] - (pos[0][j-1]<pos[1][k-1]);
                    else val[i][j][k][1] = val[i-1][j][k][1] - (pos[2][l-1]<pos[1][k-1]);
                }
                if (l){
                    if (!j && !k) val[i][j][k][2] = pos[2][i-1]-i+1;
                    else if (j) val[i][j][k][2] = val[i-1][j-1][k][2] - (pos[0][j-1]<pos[2][l-1]);
                    else val[i][j][k][2] = val[i-1][j][k-1][2] - (pos[1][k-1]<pos[2][l-1]);
                }
            }
        }
        /*printf(" %d\n", i);
        for (int z=0;z<3;z++){
            for (int j=0;j<=r;j++){
                for (int k=0;k<=g;k++) printf("%d ", val[i][j][k][z]);
                printf("\n");
            }
            printf("\n");
        }
        printf("\n");*/
    }
    ///dp 계산
    for (int i=2;i<=n;i++){
        for (int j=0;j<=min(i, r);j++){
            if (j*2>i+1) break;
            for (int k=0;k<=min(i, g);k++){
                if (k*2>i+1 || j+k>i) break;
                int l = i-j-k;
                if (l>y || l*2>i+1) continue;
                if (l<0) break;
                if (j){
                    int tmp1 = 1e9, tmp2 = 1e9;
                    if (CHK(i-1, j-1, k, 1)) tmp1 = dp[i-1][j-1][k][1];
                    if (CHK(i-1, j-1, k, 2)) tmp2 = dp[i-1][j-1][k][2];
                    if (min(tmp1, tmp2)!=1e9) dp[i][j][k][0] = min(tmp1, tmp2)+val[i][j][k][0];
                }
                if (k){
                    int tmp1 = 1e9, tmp2 = 1e9;
                    if (CHK(i-1, j, k-1, 0)) tmp1 = dp[i-1][j][k-1][0];
                    if (CHK(i-1, j, k-1, 2)) tmp2 = dp[i-1][j][k-1][2];
                    if (min(tmp1, tmp2)!=1e9) dp[i][j][k][1] = min(tmp1, tmp2)+val[i][j][k][1];
                }
                if (l){
                    int tmp1 = 1e9, tmp2 = 1e9;
                    if (CHK(i-1, j, k, 0)) tmp1 = dp[i-1][j][k][0];
                    if (CHK(i-1, j, k, 1)) tmp2 = dp[i-1][j][k][1];
                    if (min(tmp1, tmp2)!=1e9) dp[i][j][k][2] = min(tmp1, tmp2)+val[i][j][k][2];
                }
            }
        }
        /*printf(" %d\n", i);
        for (int z=0;z<3;z++){
            for (int j=0;j<=r;j++){
                for (int k=0;k<=g;k++) printf("%d ", dp[i][j][k][z]);
                printf("\n");
            }
            printf("\n");
        }
        printf("\n");*/
    }
    int ans=1e9;
    for (int j=0;j<=r;j++){
        for (int k=0;k<=g;k++){
            for (int z=0;z<3;z++) if (dp[n][j][k][z]) ans = min(ans, dp[n][j][k][z]);
        }
    }
    cout << ans;
    /*while(true){
        int q, w, e, r;
        cin >> q >> w >>e >> r;
        if (q==-1) break;
        printf("%d %d %d\n", CHK(q, w, e, r), val[q][w][e][r], dp[q][w][e][r]);
    }*/
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 208 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 460 KB Output is correct
5 Correct 1 ms 576 KB Output is correct
6 Correct 1 ms 588 KB Output is correct
7 Correct 1 ms 588 KB Output is correct
8 Correct 1 ms 588 KB Output is correct
9 Correct 1 ms 576 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 588 KB Output is correct
12 Correct 1 ms 588 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 588 KB Output is correct
15 Correct 1 ms 588 KB Output is correct
16 Correct 1 ms 328 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 208 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 460 KB Output is correct
5 Correct 1 ms 576 KB Output is correct
6 Correct 1 ms 588 KB Output is correct
7 Correct 1 ms 588 KB Output is correct
8 Correct 1 ms 588 KB Output is correct
9 Correct 1 ms 576 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 588 KB Output is correct
12 Correct 1 ms 588 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 588 KB Output is correct
15 Correct 1 ms 588 KB Output is correct
16 Correct 1 ms 328 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 3 ms 4300 KB Output is correct
19 Correct 3 ms 4044 KB Output is correct
20 Correct 3 ms 4300 KB Output is correct
21 Correct 3 ms 4172 KB Output is correct
22 Correct 3 ms 4044 KB Output is correct
23 Correct 3 ms 4044 KB Output is correct
24 Correct 3 ms 3140 KB Output is correct
25 Correct 1 ms 204 KB Output is correct
26 Correct 1 ms 332 KB Output is correct
27 Correct 3 ms 4044 KB Output is correct
28 Correct 3 ms 4172 KB Output is correct
29 Correct 3 ms 4300 KB Output is correct
30 Correct 2 ms 3916 KB Output is correct
31 Correct 2 ms 3788 KB Output is correct
32 Correct 3 ms 4044 KB Output is correct
33 Correct 1 ms 204 KB Output is correct
34 Correct 1 ms 328 KB Output is correct
35 Correct 2 ms 3904 KB Output is correct
36 Correct 3 ms 4088 KB Output is correct
37 Correct 2 ms 3404 KB Output is correct
38 Correct 2 ms 3660 KB Output is correct
39 Correct 3 ms 4172 KB Output is correct
40 Correct 1 ms 204 KB Output is correct
41 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 66 ms 98844 KB Output is correct
3 Correct 67 ms 98464 KB Output is correct
4 Correct 66 ms 98884 KB Output is correct
5 Correct 66 ms 98916 KB Output is correct
6 Correct 67 ms 98884 KB Output is correct
7 Correct 66 ms 98376 KB Output is correct
8 Correct 66 ms 98380 KB Output is correct
9 Correct 67 ms 97952 KB Output is correct
10 Correct 78 ms 98824 KB Output is correct
11 Correct 67 ms 98880 KB Output is correct
12 Correct 16 ms 27468 KB Output is correct
13 Correct 29 ms 47564 KB Output is correct
14 Correct 44 ms 68036 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 208 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 460 KB Output is correct
5 Correct 1 ms 576 KB Output is correct
6 Correct 1 ms 588 KB Output is correct
7 Correct 1 ms 588 KB Output is correct
8 Correct 1 ms 588 KB Output is correct
9 Correct 1 ms 576 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 588 KB Output is correct
12 Correct 1 ms 588 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 588 KB Output is correct
15 Correct 1 ms 588 KB Output is correct
16 Correct 1 ms 328 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 3 ms 4300 KB Output is correct
19 Correct 3 ms 4044 KB Output is correct
20 Correct 3 ms 4300 KB Output is correct
21 Correct 3 ms 4172 KB Output is correct
22 Correct 3 ms 4044 KB Output is correct
23 Correct 3 ms 4044 KB Output is correct
24 Correct 3 ms 3140 KB Output is correct
25 Correct 1 ms 204 KB Output is correct
26 Correct 1 ms 332 KB Output is correct
27 Correct 3 ms 4044 KB Output is correct
28 Correct 3 ms 4172 KB Output is correct
29 Correct 3 ms 4300 KB Output is correct
30 Correct 2 ms 3916 KB Output is correct
31 Correct 2 ms 3788 KB Output is correct
32 Correct 3 ms 4044 KB Output is correct
33 Correct 1 ms 204 KB Output is correct
34 Correct 1 ms 328 KB Output is correct
35 Correct 2 ms 3904 KB Output is correct
36 Correct 3 ms 4088 KB Output is correct
37 Correct 2 ms 3404 KB Output is correct
38 Correct 2 ms 3660 KB Output is correct
39 Correct 3 ms 4172 KB Output is correct
40 Correct 1 ms 204 KB Output is correct
41 Correct 1 ms 332 KB Output is correct
42 Correct 1 ms 332 KB Output is correct
43 Correct 66 ms 98844 KB Output is correct
44 Correct 67 ms 98464 KB Output is correct
45 Correct 66 ms 98884 KB Output is correct
46 Correct 66 ms 98916 KB Output is correct
47 Correct 67 ms 98884 KB Output is correct
48 Correct 66 ms 98376 KB Output is correct
49 Correct 66 ms 98380 KB Output is correct
50 Correct 67 ms 97952 KB Output is correct
51 Correct 78 ms 98824 KB Output is correct
52 Correct 67 ms 98880 KB Output is correct
53 Correct 16 ms 27468 KB Output is correct
54 Correct 29 ms 47564 KB Output is correct
55 Correct 44 ms 68036 KB Output is correct
56 Correct 1 ms 332 KB Output is correct
57 Correct 1 ms 332 KB Output is correct
58 Correct 108 ms 147172 KB Output is correct
59 Correct 116 ms 153880 KB Output is correct
60 Correct 113 ms 152512 KB Output is correct
61 Correct 114 ms 150880 KB Output is correct
62 Correct 1 ms 332 KB Output is correct
63 Correct 72 ms 106180 KB Output is correct
64 Correct 90 ms 126532 KB Output is correct
65 Correct 95 ms 136260 KB Output is correct
66 Correct 112 ms 150524 KB Output is correct
67 Correct 107 ms 141968 KB Output is correct
68 Correct 116 ms 151748 KB Output is correct
69 Correct 113 ms 152344 KB Output is correct
70 Correct 114 ms 151872 KB Output is correct
71 Correct 112 ms 154224 KB Output is correct
72 Correct 1 ms 332 KB Output is correct
73 Correct 1 ms 332 KB Output is correct