Submission #542061

# Submission time Handle Problem Language Result Execution time Memory
542061 2022-03-25T09:33:56 Z Cookie197 Growing Vegetable is Fun 3 (JOI19_ho_t3) C++17
100 / 100
231 ms 522776 KB
#include<iostream>
#include<vector>
#include<algorithm>
#include<set>
#include<map>

using namespace std;
#define ll long long
#define endl "\n"
#define mp make_pair
#define out(x) cout<< #x << " = " << x << endl
#define pii pair<int,int> 
#pragma GCC optimize("Ofast")

// 考慮前i個,放了r個R g個G i-r-g個Y,第i個恰是R/G/Y,所需最小步數
string s;
int n;
vector<int> rpos, gpos, ypos;
int rcnt, gcnt, ycnt;
int dp[405][405][405][3];
int rpre[405],gpre[405],ypre[405];
void chmin(int &a, int b){
    a = min(a,b);
}
signed main(){
    ios::sync_with_stdio(false); cin.tie(0);
    cin>>n>>s;
    s = '$' + s;
    rpos.push_back(0), gpos.push_back(0), ypos.push_back(0);
    for (int i=1;i<=n;i++){
        if (s[i] == 'R') rpos.push_back(i), rcnt++;
        if (s[i] == 'G') gpos.push_back(i), gcnt++;
        if (s[i] == 'Y') ypos.push_back(i), ycnt++;
        rpre[i] = rpre[i-1] + (s[i] == 'R');
        gpre[i] = gpre[i-1] + (s[i] == 'G');
        ypre[i] = ypre[i-1] + (s[i] == 'Y');
    }

    for (int i=1;i<=n;i++) for (int r=0;r<=rcnt;r++) for (int g=0;g<=gcnt;g++) for (int c=0;c<=2;c++) dp[i][r][g][c] = 1e9;
    dp[1][1][0][0] = rcnt ? rpos[1] - 1 : 1e9;
    dp[1][0][1][1] = gcnt ? gpos[1] - 1 : 1e9;
    dp[1][0][0][2] = ycnt ? ypos[1] - 1 : 1e9;

    for (int i=1;i<n;i++) for (int r=0;r<=rcnt;r++) for (int g=0;g<=gcnt;g++) for (int c=0;c<=2;c++){
        int y = i-r-g; if (y<0) continue;
        if (dp[i][r][g][c] == 1e9) continue;
        if (r>i || g>i || y>i) continue;
        if (r+g > i) continue;
        //cout<<i<<" "<<r<<" "<<g<<" "<<y<<" "<<c<<"   "<<dp[i][r][g][c]<<endl;
        // put R in i+1
        if (c!=0 && r<rcnt){
            int cost = abs(gpre[rpos[r+1]] - g) + abs(ypre[rpos[r+1]] - y);
            chmin(dp[i+1][r+1][g][0], dp[i][r][g][c] + cost);
        }

        if (c!=1 && g<gcnt){
            int cost = abs(rpre[gpos[g+1]] - r) + abs(ypre[gpos[g+1]] - y);
            chmin(dp[i+1][r][g+1][1], dp[i][r][g][c] + cost);
        }

        if (c!=2 && y<ycnt){
            int cost = abs(rpre[ypos[y+1]] - r) + abs(gpre[ypos[y+1]] - g);
            chmin(dp[i+1][r][g][2], dp[i][r][g][c] + cost);
        }
    }

    int res = min(dp[n][rcnt][gcnt][0], min(dp[n][rcnt][gcnt][1], dp[n][rcnt][gcnt][2]));
    if (res >= 5e8) cout<<-1<<endl;
    else cout<<res/2<<endl;
}
# 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 328 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 724 KB Output is correct
6 Correct 1 ms 724 KB Output is correct
7 Correct 1 ms 852 KB Output is correct
8 Correct 1 ms 724 KB Output is correct
9 Correct 1 ms 724 KB Output is correct
10 Correct 1 ms 980 KB Output is correct
11 Correct 1 ms 724 KB Output is correct
12 Correct 1 ms 724 KB Output is correct
13 Correct 1 ms 596 KB Output is correct
14 Correct 1 ms 716 KB Output is correct
15 Correct 1 ms 584 KB Output is correct
16 Correct 1 ms 1096 KB Output is correct
17 Correct 1 ms 340 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 328 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 724 KB Output is correct
6 Correct 1 ms 724 KB Output is correct
7 Correct 1 ms 852 KB Output is correct
8 Correct 1 ms 724 KB Output is correct
9 Correct 1 ms 724 KB Output is correct
10 Correct 1 ms 980 KB Output is correct
11 Correct 1 ms 724 KB Output is correct
12 Correct 1 ms 724 KB Output is correct
13 Correct 1 ms 596 KB Output is correct
14 Correct 1 ms 716 KB Output is correct
15 Correct 1 ms 584 KB Output is correct
16 Correct 1 ms 1096 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 3 ms 6612 KB Output is correct
19 Correct 3 ms 5588 KB Output is correct
20 Correct 3 ms 6996 KB Output is correct
21 Correct 3 ms 5832 KB Output is correct
22 Correct 3 ms 6720 KB Output is correct
23 Correct 3 ms 5076 KB Output is correct
24 Correct 2 ms 3284 KB Output is correct
25 Correct 5 ms 10196 KB Output is correct
26 Correct 4 ms 8776 KB Output is correct
27 Correct 3 ms 7636 KB Output is correct
28 Correct 3 ms 6100 KB Output is correct
29 Correct 3 ms 6356 KB Output is correct
30 Correct 2 ms 5192 KB Output is correct
31 Correct 3 ms 4564 KB Output is correct
32 Correct 3 ms 5332 KB Output is correct
33 Correct 3 ms 7252 KB Output is correct
34 Correct 3 ms 6476 KB Output is correct
35 Correct 3 ms 6468 KB Output is correct
36 Correct 3 ms 5716 KB Output is correct
37 Correct 2 ms 3912 KB Output is correct
38 Correct 2 ms 4436 KB Output is correct
39 Correct 3 ms 5780 KB Output is correct
40 Correct 1 ms 724 KB Output is correct
41 Correct 2 ms 3156 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 197 ms 384040 KB Output is correct
3 Correct 187 ms 383044 KB Output is correct
4 Correct 194 ms 384044 KB Output is correct
5 Correct 184 ms 384092 KB Output is correct
6 Correct 188 ms 384044 KB Output is correct
7 Correct 181 ms 381088 KB Output is correct
8 Correct 182 ms 381188 KB Output is correct
9 Correct 183 ms 380376 KB Output is correct
10 Correct 180 ms 384036 KB Output is correct
11 Correct 183 ms 383944 KB Output is correct
12 Correct 51 ms 102760 KB Output is correct
13 Correct 91 ms 180932 KB Output is correct
14 Correct 126 ms 261836 KB Output is correct
15 Correct 177 ms 382148 KB Output is correct
16 Correct 186 ms 382036 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 328 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 724 KB Output is correct
6 Correct 1 ms 724 KB Output is correct
7 Correct 1 ms 852 KB Output is correct
8 Correct 1 ms 724 KB Output is correct
9 Correct 1 ms 724 KB Output is correct
10 Correct 1 ms 980 KB Output is correct
11 Correct 1 ms 724 KB Output is correct
12 Correct 1 ms 724 KB Output is correct
13 Correct 1 ms 596 KB Output is correct
14 Correct 1 ms 716 KB Output is correct
15 Correct 1 ms 584 KB Output is correct
16 Correct 1 ms 1096 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 3 ms 6612 KB Output is correct
19 Correct 3 ms 5588 KB Output is correct
20 Correct 3 ms 6996 KB Output is correct
21 Correct 3 ms 5832 KB Output is correct
22 Correct 3 ms 6720 KB Output is correct
23 Correct 3 ms 5076 KB Output is correct
24 Correct 2 ms 3284 KB Output is correct
25 Correct 5 ms 10196 KB Output is correct
26 Correct 4 ms 8776 KB Output is correct
27 Correct 3 ms 7636 KB Output is correct
28 Correct 3 ms 6100 KB Output is correct
29 Correct 3 ms 6356 KB Output is correct
30 Correct 2 ms 5192 KB Output is correct
31 Correct 3 ms 4564 KB Output is correct
32 Correct 3 ms 5332 KB Output is correct
33 Correct 3 ms 7252 KB Output is correct
34 Correct 3 ms 6476 KB Output is correct
35 Correct 3 ms 6468 KB Output is correct
36 Correct 3 ms 5716 KB Output is correct
37 Correct 2 ms 3912 KB Output is correct
38 Correct 2 ms 4436 KB Output is correct
39 Correct 3 ms 5780 KB Output is correct
40 Correct 1 ms 724 KB Output is correct
41 Correct 2 ms 3156 KB Output is correct
42 Correct 1 ms 340 KB Output is correct
43 Correct 197 ms 384040 KB Output is correct
44 Correct 187 ms 383044 KB Output is correct
45 Correct 194 ms 384044 KB Output is correct
46 Correct 184 ms 384092 KB Output is correct
47 Correct 188 ms 384044 KB Output is correct
48 Correct 181 ms 381088 KB Output is correct
49 Correct 182 ms 381188 KB Output is correct
50 Correct 183 ms 380376 KB Output is correct
51 Correct 180 ms 384036 KB Output is correct
52 Correct 183 ms 383944 KB Output is correct
53 Correct 51 ms 102760 KB Output is correct
54 Correct 91 ms 180932 KB Output is correct
55 Correct 126 ms 261836 KB Output is correct
56 Correct 177 ms 382148 KB Output is correct
57 Correct 186 ms 382036 KB Output is correct
58 Correct 137 ms 237548 KB Output is correct
59 Correct 155 ms 277444 KB Output is correct
60 Correct 150 ms 272332 KB Output is correct
61 Correct 143 ms 255804 KB Output is correct
62 Correct 193 ms 410468 KB Output is correct
63 Correct 178 ms 374536 KB Output is correct
64 Correct 172 ms 359276 KB Output is correct
65 Correct 156 ms 305324 KB Output is correct
66 Correct 141 ms 260248 KB Output is correct
67 Correct 126 ms 216764 KB Output is correct
68 Correct 148 ms 261452 KB Output is correct
69 Correct 141 ms 262264 KB Output is correct
70 Correct 148 ms 260296 KB Output is correct
71 Correct 151 ms 289780 KB Output is correct
72 Correct 231 ms 522776 KB Output is correct
73 Correct 6 ms 11476 KB Output is correct