Submission #866028

# Submission time Handle Problem Language Result Execution time Memory
866028 2023-10-25T09:57:41 Z guagua0407 Growing Vegetable is Fun 3 (JOI19_ho_t3) C++17
60 / 100
248 ms 1048576 KB
//#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int,int>
#define f first
#define s second
#define all(x) x.begin(),x.end()
#define _ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

void setIO(string s) {
    freopen((s + ".in").c_str(), "r", stdin);
    freopen((s + ".out").c_str(), "w", stdout);
}

const int mxn=405;
ll dp[mxn][mxn][mxn][3];
int cnt[mxn][3];
map<char,int> mp;

ll bs(ll a){
    return max(0ll,a);
}

int main() {_
    int n;
    cin>>n;
    string str;
    cin>>str;
    str="."+str;
    vector<int> vec(n+1);
    mp['R']=0;
    mp['G']=1;
    mp['Y']=2;
    for(int i=1;i<=n;i++){
        vec[i]=mp[str[i]];
    }
    for(int i=1;i<=n;i++){
        cnt[i][0]=cnt[i-1][0];
        cnt[i][1]=cnt[i-1][1];
        cnt[i][2]=cnt[i-1][2];
        cnt[i][vec[i]]++;
    }
    vector<int> pos[3];
    pos[0].push_back(0);
    pos[1].push_back(0);
    pos[2].push_back(0);
    for(int i=1;i<=n;i++){
        pos[vec[i]].push_back(i);
    }
    //return 0;
    for(int i=0;i<=n;i++){
        for(int j=0;j<=n;j++){
            for(int k=0;k<=n;k++){
                for(int t=0;t<3;t++){
                    dp[i][j][k][t]=1e18;
                }
            }
        }
    }
    dp[0][0][0][0]=dp[0][0][0][1]=dp[0][0][0][2]=0;
    for(int i=1;i<=n;i++){
        for(int j=0;j<=min(cnt[n][0],i);j++){
            for(int k=0;k<=min(cnt[n][1],i-j);k++){
                int r=i-j-k;
                if(r<0 or cnt[n][2]<r) continue;
                ll inf=dp[i][j][k][0];
                if(j>0){
                    int ps=pos[0][j];
                    dp[i][j][k][0]=min(dp[i-1][j-1][k][1] , dp[i-1][j-1][k][2])+bs(cnt[ps][1]-k)+bs(cnt[ps][2]-r);
                    dp[i][j][k][0]=min(inf,dp[i][j][k][0]);
                }
                if(k>0){
                    int ps=pos[1][k];
                    dp[i][j][k][1]=min(dp[i-1][j][k-1][0] , dp[i-1][j][k-1][2])+bs(cnt[ps][0]-j)+bs(cnt[ps][2]-r);
                    dp[i][j][k][1]=min(inf,dp[i][j][k][1]);
                }
                if(r>0){
                    int ps=pos[2][r];
                    dp[i][j][k][2]=min(dp[i-1][j][k][0] , dp[i-1][j][k][1])+bs(cnt[ps][0]-j)+bs(cnt[ps][1]-k);
                    dp[i][j][k][2]=min(inf,dp[i][j][k][2]);
                }
                /*for(int tp=0;tp<3;tp++){
                    cout<<i<<' '<<j<<' '<<k<<' '<<tp<<' '<<dp[i][j][k][tp]<<'\n';
                }*/
            }
        }
    }
    ll ans=*min_element(dp[n][cnt[n][0]][cnt[n][1]],dp[n][cnt[n][0]][cnt[n][1]]+3);
    cout<<(ans>=1e18?-1:ans)<<'\n';
    return 0;
}
//maybe its multiset not set
//dp[i][j][k][type] 前 i 個有 j 個 R, k 個 G, 最後一個東西是 type
/*
dp[i][j][k][type]=dp[i-1][

YYGYYYGGGGRGYYGRGRYG
*/

Compilation message

joi2019_ho_t3.cpp: In function 'void setIO(std::string)':
joi2019_ho_t3.cpp:12:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   12 |     freopen((s + ".in").c_str(), "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:13:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   13 |     freopen((s + ".out").c_str(), "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 2396 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 6636 KB Output is correct
4 Correct 2 ms 20828 KB Output is correct
5 Correct 4 ms 33372 KB Output is correct
6 Correct 4 ms 33116 KB Output is correct
7 Correct 3 ms 33236 KB Output is correct
8 Correct 3 ms 33332 KB Output is correct
9 Correct 4 ms 33372 KB Output is correct
10 Correct 4 ms 33116 KB Output is correct
11 Correct 4 ms 33372 KB Output is correct
12 Correct 4 ms 33368 KB Output is correct
13 Correct 5 ms 33372 KB Output is correct
14 Correct 4 ms 31268 KB Output is correct
15 Correct 4 ms 33372 KB Output is correct
16 Correct 4 ms 33116 KB Output is correct
17 Correct 4 ms 31060 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 2396 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 6636 KB Output is correct
4 Correct 2 ms 20828 KB Output is correct
5 Correct 4 ms 33372 KB Output is correct
6 Correct 4 ms 33116 KB Output is correct
7 Correct 3 ms 33236 KB Output is correct
8 Correct 3 ms 33332 KB Output is correct
9 Correct 4 ms 33372 KB Output is correct
10 Correct 4 ms 33116 KB Output is correct
11 Correct 4 ms 33372 KB Output is correct
12 Correct 4 ms 33368 KB Output is correct
13 Correct 5 ms 33372 KB Output is correct
14 Correct 4 ms 31268 KB Output is correct
15 Correct 4 ms 33372 KB Output is correct
16 Correct 4 ms 33116 KB Output is correct
17 Correct 4 ms 31060 KB Output is correct
18 Correct 17 ms 160596 KB Output is correct
19 Correct 16 ms 160600 KB Output is correct
20 Correct 15 ms 160604 KB Output is correct
21 Correct 16 ms 160604 KB Output is correct
22 Correct 17 ms 160600 KB Output is correct
23 Correct 16 ms 160576 KB Output is correct
24 Correct 16 ms 160600 KB Output is correct
25 Correct 16 ms 160608 KB Output is correct
26 Correct 16 ms 160604 KB Output is correct
27 Correct 16 ms 160392 KB Output is correct
28 Correct 15 ms 160604 KB Output is correct
29 Correct 16 ms 160612 KB Output is correct
30 Correct 15 ms 160600 KB Output is correct
31 Correct 16 ms 160464 KB Output is correct
32 Correct 16 ms 160464 KB Output is correct
33 Correct 15 ms 158556 KB Output is correct
34 Correct 16 ms 158544 KB Output is correct
35 Correct 15 ms 154396 KB Output is correct
36 Correct 16 ms 158552 KB Output is correct
37 Correct 15 ms 150108 KB Output is correct
38 Correct 16 ms 160472 KB Output is correct
39 Correct 16 ms 160380 KB Output is correct
40 Correct 15 ms 158552 KB Output is correct
41 Correct 16 ms 160604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 6488 KB Output is correct
2 Runtime error 248 ms 1048576 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 2396 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 6636 KB Output is correct
4 Correct 2 ms 20828 KB Output is correct
5 Correct 4 ms 33372 KB Output is correct
6 Correct 4 ms 33116 KB Output is correct
7 Correct 3 ms 33236 KB Output is correct
8 Correct 3 ms 33332 KB Output is correct
9 Correct 4 ms 33372 KB Output is correct
10 Correct 4 ms 33116 KB Output is correct
11 Correct 4 ms 33372 KB Output is correct
12 Correct 4 ms 33368 KB Output is correct
13 Correct 5 ms 33372 KB Output is correct
14 Correct 4 ms 31268 KB Output is correct
15 Correct 4 ms 33372 KB Output is correct
16 Correct 4 ms 33116 KB Output is correct
17 Correct 4 ms 31060 KB Output is correct
18 Correct 17 ms 160596 KB Output is correct
19 Correct 16 ms 160600 KB Output is correct
20 Correct 15 ms 160604 KB Output is correct
21 Correct 16 ms 160604 KB Output is correct
22 Correct 17 ms 160600 KB Output is correct
23 Correct 16 ms 160576 KB Output is correct
24 Correct 16 ms 160600 KB Output is correct
25 Correct 16 ms 160608 KB Output is correct
26 Correct 16 ms 160604 KB Output is correct
27 Correct 16 ms 160392 KB Output is correct
28 Correct 15 ms 160604 KB Output is correct
29 Correct 16 ms 160612 KB Output is correct
30 Correct 15 ms 160600 KB Output is correct
31 Correct 16 ms 160464 KB Output is correct
32 Correct 16 ms 160464 KB Output is correct
33 Correct 15 ms 158556 KB Output is correct
34 Correct 16 ms 158544 KB Output is correct
35 Correct 15 ms 154396 KB Output is correct
36 Correct 16 ms 158552 KB Output is correct
37 Correct 15 ms 150108 KB Output is correct
38 Correct 16 ms 160472 KB Output is correct
39 Correct 16 ms 160380 KB Output is correct
40 Correct 15 ms 158552 KB Output is correct
41 Correct 16 ms 160604 KB Output is correct
42 Correct 1 ms 6488 KB Output is correct
43 Runtime error 248 ms 1048576 KB Execution killed with signal 9
44 Halted 0 ms 0 KB -