Submission #538610

# Submission time Handle Problem Language Result Execution time Memory
538610 2022-03-17T08:26:44 Z 79brue Growing Vegetable is Fun 3 (JOI19_ho_t3) C++14
100 / 100
165 ms 6092 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

int n;
int arr[402];
int DP[2][402][402][3];
int cnt[3];
vector<int> occ[3];

int weight[402][402][3]; /// index, 배열에서의 순서

int main(){
    scanf("%d", &n);
    for(int i=1; i<=n; i++){
        char c;
        scanf(" %c", &c);
        arr[i] = c=='R' ? 0 : c=='G' ? 1 : 2;
        cnt[arr[i]]++;
        occ[arr[i]].push_back(i);
    }

    for(int i=1; i<=n; i++){
        for(int d=0; d<3; d++){
            for(int j=1; j<=cnt[d]; j++){
                weight[i][j][d] = weight[i-1][j][d] + (arr[i] == d && i <= occ[d][j-1]);
            }
        }
    }

    for(int i=0; i<2; i++) for(int j=0; j<=n; j++) for(int k=0; k<=n; k++) for(int l=0; l<3; l++) DP[i][j][k][l]=1e9;
    DP[0][0][0][0] = DP[0][0][0][1] = DP[0][0][0][2] = 0;

    for(int i=1; i<=n; i++){
        int ri = i%2;
        for(int j=0; j<=n; j++) for(int k=0; k<=n; k++) for(int l=0; l<3; l++) DP[ri][j][k][l]=1e9;

        for(int j=0; j<i && j<=cnt[0]; j++){
            for(int k=0; j+k<i && k<=cnt[1]; k++){
                int l = i-j-k-1;
                if(l>cnt[2]) continue;
                for(int d=0; d<3; d++){
//                    printf("%d %d %d %d %d: %d\n", i, j, k, l, d, DP[!ri][j][k][d]);
                    if(j<cnt[0] && d!=0){
                        int w = occ[0][j];
                        DP[ri][j+1][k][0] = min(DP[ri][j+1][k][0],
                                                DP[!ri][j][k][d] + w-1 -
                                                (weight[w][j][0] + weight[w][k][1] + weight[w][l][2]));
                    }
                    if(k<cnt[1] && d!=1){
                        int w = occ[1][k];
                        DP[ri][j][k+1][1] = min(DP[ri][j][k+1][1],
                                                DP[!ri][j][k][d] + w-1 -
                                                (weight[w][j][0] + weight[w][k][1] + weight[w][l][2]));
                    }
                    if(l<cnt[2] && d!=2){
                        int w = occ[2][l];
                        DP[ri][j][k][2] = min(DP[ri][j][k][2],
                                                DP[!ri][j][k][d] + w-1 -
                                                (weight[w][j][0] + weight[w][k][1] + weight[w][l][2]));
                    }
                }
            }
        }
    }

    int ans = 1e9;
    for(int i=0; i<3; i++) ans = min(ans, DP[n%2][cnt[0]][cnt[1]][i]);
    printf("%d", ans==1000000000 ? -1 : ans);
}

Compilation message

joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:16:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   16 |     scanf("%d", &n);
      |     ~~~~~^~~~~~~~~~
joi2019_ho_t3.cpp:19:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   19 |         scanf(" %c", &c);
      |         ~~~~~^~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 280 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 444 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 440 KB Output is correct
13 Correct 1 ms 440 KB Output is correct
14 Correct 1 ms 444 KB Output is correct
15 Correct 1 ms 468 KB Output is correct
16 Correct 1 ms 468 KB Output is correct
17 Correct 1 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 280 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 444 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 440 KB Output is correct
13 Correct 1 ms 440 KB Output is correct
14 Correct 1 ms 444 KB Output is correct
15 Correct 1 ms 468 KB Output is correct
16 Correct 1 ms 468 KB Output is correct
17 Correct 1 ms 468 KB Output is correct
18 Correct 1 ms 1080 KB Output is correct
19 Correct 2 ms 1084 KB Output is correct
20 Correct 2 ms 1108 KB Output is correct
21 Correct 2 ms 1108 KB Output is correct
22 Correct 1 ms 1108 KB Output is correct
23 Correct 2 ms 1080 KB Output is correct
24 Correct 1 ms 1108 KB Output is correct
25 Correct 1 ms 1108 KB Output is correct
26 Correct 1 ms 1108 KB Output is correct
27 Correct 2 ms 1108 KB Output is correct
28 Correct 1 ms 1108 KB Output is correct
29 Correct 1 ms 1108 KB Output is correct
30 Correct 2 ms 1108 KB Output is correct
31 Correct 1 ms 1088 KB Output is correct
32 Correct 2 ms 1108 KB Output is correct
33 Correct 2 ms 1084 KB Output is correct
34 Correct 2 ms 1080 KB Output is correct
35 Correct 1 ms 1080 KB Output is correct
36 Correct 2 ms 1108 KB Output is correct
37 Correct 1 ms 1084 KB Output is correct
38 Correct 2 ms 1084 KB Output is correct
39 Correct 1 ms 1108 KB Output is correct
40 Correct 1 ms 1080 KB Output is correct
41 Correct 1 ms 1108 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 87 ms 5976 KB Output is correct
3 Correct 108 ms 5960 KB Output is correct
4 Correct 91 ms 5972 KB Output is correct
5 Correct 85 ms 5972 KB Output is correct
6 Correct 84 ms 5984 KB Output is correct
7 Correct 90 ms 5972 KB Output is correct
8 Correct 87 ms 5972 KB Output is correct
9 Correct 82 ms 5948 KB Output is correct
10 Correct 88 ms 5952 KB Output is correct
11 Correct 85 ms 5972 KB Output is correct
12 Correct 15 ms 3156 KB Output is correct
13 Correct 36 ms 4180 KB Output is correct
14 Correct 55 ms 4948 KB Output is correct
15 Correct 93 ms 5992 KB Output is correct
16 Correct 96 ms 5980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 280 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 444 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 440 KB Output is correct
13 Correct 1 ms 440 KB Output is correct
14 Correct 1 ms 444 KB Output is correct
15 Correct 1 ms 468 KB Output is correct
16 Correct 1 ms 468 KB Output is correct
17 Correct 1 ms 468 KB Output is correct
18 Correct 1 ms 1080 KB Output is correct
19 Correct 2 ms 1084 KB Output is correct
20 Correct 2 ms 1108 KB Output is correct
21 Correct 2 ms 1108 KB Output is correct
22 Correct 1 ms 1108 KB Output is correct
23 Correct 2 ms 1080 KB Output is correct
24 Correct 1 ms 1108 KB Output is correct
25 Correct 1 ms 1108 KB Output is correct
26 Correct 1 ms 1108 KB Output is correct
27 Correct 2 ms 1108 KB Output is correct
28 Correct 1 ms 1108 KB Output is correct
29 Correct 1 ms 1108 KB Output is correct
30 Correct 2 ms 1108 KB Output is correct
31 Correct 1 ms 1088 KB Output is correct
32 Correct 2 ms 1108 KB Output is correct
33 Correct 2 ms 1084 KB Output is correct
34 Correct 2 ms 1080 KB Output is correct
35 Correct 1 ms 1080 KB Output is correct
36 Correct 2 ms 1108 KB Output is correct
37 Correct 1 ms 1084 KB Output is correct
38 Correct 2 ms 1084 KB Output is correct
39 Correct 1 ms 1108 KB Output is correct
40 Correct 1 ms 1080 KB Output is correct
41 Correct 1 ms 1108 KB Output is correct
42 Correct 1 ms 340 KB Output is correct
43 Correct 87 ms 5976 KB Output is correct
44 Correct 108 ms 5960 KB Output is correct
45 Correct 91 ms 5972 KB Output is correct
46 Correct 85 ms 5972 KB Output is correct
47 Correct 84 ms 5984 KB Output is correct
48 Correct 90 ms 5972 KB Output is correct
49 Correct 87 ms 5972 KB Output is correct
50 Correct 82 ms 5948 KB Output is correct
51 Correct 88 ms 5952 KB Output is correct
52 Correct 85 ms 5972 KB Output is correct
53 Correct 15 ms 3156 KB Output is correct
54 Correct 36 ms 4180 KB Output is correct
55 Correct 55 ms 4948 KB Output is correct
56 Correct 93 ms 5992 KB Output is correct
57 Correct 96 ms 5980 KB Output is correct
58 Correct 152 ms 5976 KB Output is correct
59 Correct 148 ms 5976 KB Output is correct
60 Correct 128 ms 5944 KB Output is correct
61 Correct 131 ms 5972 KB Output is correct
62 Correct 102 ms 6092 KB Output is correct
63 Correct 123 ms 5972 KB Output is correct
64 Correct 111 ms 5984 KB Output is correct
65 Correct 112 ms 5972 KB Output is correct
66 Correct 120 ms 5844 KB Output is correct
67 Correct 152 ms 5972 KB Output is correct
68 Correct 119 ms 5972 KB Output is correct
69 Correct 121 ms 5972 KB Output is correct
70 Correct 146 ms 6052 KB Output is correct
71 Correct 165 ms 5972 KB Output is correct
72 Correct 105 ms 5972 KB Output is correct
73 Correct 72 ms 5972 KB Output is correct