# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
866030 | 2023-10-25T10:00:49 Z | guagua0407 | Growing Vegetable is Fun 3 (JOI19_ho_t3) | C++17 | 196 ms | 8276 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][3]; ll tmp[mxn][mxn][3]; int cnt[mxn][3]; ll inf=1e18; 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 j=0;j<=n;j++){ for(int k=0;k<=n;k++){ for(int t=0;t<3;t++){ dp[j][k][t]=1e18; } } } dp[0][0][0]=dp[0][0][1]=dp[0][0][2]=0; for(int i=1;i<=n;i++){ for(int j=0;j<=n;j++){ for(int k=0;k<=n;k++){ for(int t=0;t<3;t++){ tmp[j][k][t]=1e18; } } } 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; if(j>0){ int ps=pos[0][j]; tmp[j][k][0]=min(dp[j-1][k][1] , dp[j-1][k][2])+bs(cnt[ps][1]-k)+bs(cnt[ps][2]-r); } if(k>0){ int ps=pos[1][k]; tmp[j][k][1]=min(dp[j][k-1][0] , dp[j][k-1][2])+bs(cnt[ps][0]-j)+bs(cnt[ps][2]-r); } if(r>0){ int ps=pos[2][r]; tmp[j][k][2]=min(dp[j][k][0] , dp[j][k][1])+bs(cnt[ps][0]-j)+bs(cnt[ps][1]-k); } /*for(int tp=0;tp<3;tp++){ cout<<i<<' '<<j<<' '<<k<<' '<<tp<<' '<<dp[i][j][k][tp]<<'\n'; }*/ } } for(int j=0;j<=n;j++){ for(int k=0;k<=n;k++){ for(int t=0;t<3;t++){ dp[j][k][t]=min(tmp[j][k][t],inf); } } } } ll ans=*min_element(dp[cnt[n][0]][cnt[n][1]],dp[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
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 1 ms | 2396 KB | Output is correct |
2 | Correct | 0 ms | 2396 KB | Output is correct |
3 | Correct | 1 ms | 2396 KB | Output is correct |
4 | Correct | 0 ms | 2396 KB | Output is correct |
5 | Correct | 0 ms | 2396 KB | Output is correct |
6 | Correct | 1 ms | 2396 KB | Output is correct |
7 | Correct | 0 ms | 2396 KB | Output is correct |
8 | Correct | 0 ms | 2396 KB | Output is correct |
9 | Correct | 0 ms | 2396 KB | Output is correct |
10 | Correct | 0 ms | 2396 KB | Output is correct |
11 | Correct | 0 ms | 2396 KB | Output is correct |
12 | Correct | 0 ms | 2396 KB | Output is correct |
13 | Correct | 1 ms | 2644 KB | Output is correct |
14 | Correct | 0 ms | 2396 KB | Output is correct |
15 | Correct | 1 ms | 2524 KB | Output is correct |
16 | Correct | 0 ms | 2396 KB | Output is correct |
17 | Correct | 0 ms | 2396 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 1 ms | 2396 KB | Output is correct |
2 | Correct | 0 ms | 2396 KB | Output is correct |
3 | Correct | 1 ms | 2396 KB | Output is correct |
4 | Correct | 0 ms | 2396 KB | Output is correct |
5 | Correct | 0 ms | 2396 KB | Output is correct |
6 | Correct | 1 ms | 2396 KB | Output is correct |
7 | Correct | 0 ms | 2396 KB | Output is correct |
8 | Correct | 0 ms | 2396 KB | Output is correct |
9 | Correct | 0 ms | 2396 KB | Output is correct |
10 | Correct | 0 ms | 2396 KB | Output is correct |
11 | Correct | 0 ms | 2396 KB | Output is correct |
12 | Correct | 0 ms | 2396 KB | Output is correct |
13 | Correct | 1 ms | 2644 KB | Output is correct |
14 | Correct | 0 ms | 2396 KB | Output is correct |
15 | Correct | 1 ms | 2524 KB | Output is correct |
16 | Correct | 0 ms | 2396 KB | Output is correct |
17 | Correct | 0 ms | 2396 KB | Output is correct |
18 | Correct | 2 ms | 6488 KB | Output is correct |
19 | Correct | 2 ms | 6488 KB | Output is correct |
20 | Correct | 2 ms | 6492 KB | Output is correct |
21 | Correct | 1 ms | 6492 KB | Output is correct |
22 | Correct | 1 ms | 6492 KB | Output is correct |
23 | Correct | 1 ms | 6492 KB | Output is correct |
24 | Correct | 2 ms | 6492 KB | Output is correct |
25 | Correct | 2 ms | 6492 KB | Output is correct |
26 | Correct | 2 ms | 6492 KB | Output is correct |
27 | Correct | 1 ms | 6492 KB | Output is correct |
28 | Correct | 2 ms | 6492 KB | Output is correct |
29 | Correct | 2 ms | 6492 KB | Output is correct |
30 | Correct | 2 ms | 6612 KB | Output is correct |
31 | Correct | 2 ms | 6488 KB | Output is correct |
32 | Correct | 2 ms | 6492 KB | Output is correct |
33 | Correct | 2 ms | 6492 KB | Output is correct |
34 | Correct | 1 ms | 6492 KB | Output is correct |
35 | Correct | 1 ms | 6496 KB | Output is correct |
36 | Correct | 2 ms | 6492 KB | Output is correct |
37 | Correct | 1 ms | 6496 KB | Output is correct |
38 | Correct | 1 ms | 6496 KB | Output is correct |
39 | Correct | 2 ms | 6720 KB | Output is correct |
40 | Correct | 1 ms | 6688 KB | Output is correct |
41 | Correct | 1 ms | 6492 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 1 ms | 2396 KB | Output is correct |
2 | Correct | 171 ms | 8024 KB | Output is correct |
3 | Correct | 182 ms | 8112 KB | Output is correct |
4 | Correct | 169 ms | 8116 KB | Output is correct |
5 | Correct | 169 ms | 8120 KB | Output is correct |
6 | Correct | 167 ms | 8120 KB | Output is correct |
7 | Correct | 166 ms | 8024 KB | Output is correct |
8 | Correct | 170 ms | 8104 KB | Output is correct |
9 | Correct | 164 ms | 8096 KB | Output is correct |
10 | Correct | 173 ms | 8276 KB | Output is correct |
11 | Correct | 170 ms | 8124 KB | Output is correct |
12 | Correct | 25 ms | 6748 KB | Output is correct |
13 | Correct | 58 ms | 6948 KB | Output is correct |
14 | Correct | 103 ms | 7452 KB | Output is correct |
15 | Correct | 173 ms | 8276 KB | Output is correct |
16 | Correct | 171 ms | 8028 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 1 ms | 2396 KB | Output is correct |
2 | Correct | 0 ms | 2396 KB | Output is correct |
3 | Correct | 1 ms | 2396 KB | Output is correct |
4 | Correct | 0 ms | 2396 KB | Output is correct |
5 | Correct | 0 ms | 2396 KB | Output is correct |
6 | Correct | 1 ms | 2396 KB | Output is correct |
7 | Correct | 0 ms | 2396 KB | Output is correct |
8 | Correct | 0 ms | 2396 KB | Output is correct |
9 | Correct | 0 ms | 2396 KB | Output is correct |
10 | Correct | 0 ms | 2396 KB | Output is correct |
11 | Correct | 0 ms | 2396 KB | Output is correct |
12 | Correct | 0 ms | 2396 KB | Output is correct |
13 | Correct | 1 ms | 2644 KB | Output is correct |
14 | Correct | 0 ms | 2396 KB | Output is correct |
15 | Correct | 1 ms | 2524 KB | Output is correct |
16 | Correct | 0 ms | 2396 KB | Output is correct |
17 | Correct | 0 ms | 2396 KB | Output is correct |
18 | Correct | 2 ms | 6488 KB | Output is correct |
19 | Correct | 2 ms | 6488 KB | Output is correct |
20 | Correct | 2 ms | 6492 KB | Output is correct |
21 | Correct | 1 ms | 6492 KB | Output is correct |
22 | Correct | 1 ms | 6492 KB | Output is correct |
23 | Correct | 1 ms | 6492 KB | Output is correct |
24 | Correct | 2 ms | 6492 KB | Output is correct |
25 | Correct | 2 ms | 6492 KB | Output is correct |
26 | Correct | 2 ms | 6492 KB | Output is correct |
27 | Correct | 1 ms | 6492 KB | Output is correct |
28 | Correct | 2 ms | 6492 KB | Output is correct |
29 | Correct | 2 ms | 6492 KB | Output is correct |
30 | Correct | 2 ms | 6612 KB | Output is correct |
31 | Correct | 2 ms | 6488 KB | Output is correct |
32 | Correct | 2 ms | 6492 KB | Output is correct |
33 | Correct | 2 ms | 6492 KB | Output is correct |
34 | Correct | 1 ms | 6492 KB | Output is correct |
35 | Correct | 1 ms | 6496 KB | Output is correct |
36 | Correct | 2 ms | 6492 KB | Output is correct |
37 | Correct | 1 ms | 6496 KB | Output is correct |
38 | Correct | 1 ms | 6496 KB | Output is correct |
39 | Correct | 2 ms | 6720 KB | Output is correct |
40 | Correct | 1 ms | 6688 KB | Output is correct |
41 | Correct | 1 ms | 6492 KB | Output is correct |
42 | Correct | 1 ms | 2396 KB | Output is correct |
43 | Correct | 171 ms | 8024 KB | Output is correct |
44 | Correct | 182 ms | 8112 KB | Output is correct |
45 | Correct | 169 ms | 8116 KB | Output is correct |
46 | Correct | 169 ms | 8120 KB | Output is correct |
47 | Correct | 167 ms | 8120 KB | Output is correct |
48 | Correct | 166 ms | 8024 KB | Output is correct |
49 | Correct | 170 ms | 8104 KB | Output is correct |
50 | Correct | 164 ms | 8096 KB | Output is correct |
51 | Correct | 173 ms | 8276 KB | Output is correct |
52 | Correct | 170 ms | 8124 KB | Output is correct |
53 | Correct | 25 ms | 6748 KB | Output is correct |
54 | Correct | 58 ms | 6948 KB | Output is correct |
55 | Correct | 103 ms | 7452 KB | Output is correct |
56 | Correct | 173 ms | 8276 KB | Output is correct |
57 | Correct | 171 ms | 8028 KB | Output is correct |
58 | Correct | 183 ms | 8028 KB | Output is correct |
59 | Correct | 187 ms | 8028 KB | Output is correct |
60 | Correct | 178 ms | 8104 KB | Output is correct |
61 | Correct | 180 ms | 8104 KB | Output is correct |
62 | Correct | 171 ms | 8028 KB | Output is correct |
63 | Correct | 186 ms | 8024 KB | Output is correct |
64 | Correct | 174 ms | 8116 KB | Output is correct |
65 | Correct | 196 ms | 8136 KB | Output is correct |
66 | Correct | 177 ms | 8092 KB | Output is correct |
67 | Correct | 179 ms | 8116 KB | Output is correct |
68 | Correct | 185 ms | 8276 KB | Output is correct |
69 | Correct | 183 ms | 8116 KB | Output is correct |
70 | Correct | 180 ms | 8028 KB | Output is correct |
71 | Correct | 177 ms | 8024 KB | Output is correct |
72 | Correct | 165 ms | 8104 KB | Output is correct |
73 | Correct | 164 ms | 8028 KB | Output is correct |