Submission #240969

#TimeUsernameProblemLanguageResultExecution timeMemory
240969aggu_01000101Growing Vegetable is Fun 3 (JOI19_ho_t3)C++14
60 / 100
601 ms1021512 KiB
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define INF 100000000 //#define int long long #define lchild(i) (i*2 + 1) #define rchild(i) (i*2 + 2) #define mid(l, u) ((l+u)/2) #define x(p) p.first #define y(p) p.second #define MOD 998244353 #define ordered_set tree<pair<int, int>, null_type,less<pair<int, int>>, rb_tree_tag,tree_order_statistics_node_update> using namespace std; int n; const int N = 405; int dp[N][N][N][4]; int arr[N]; vector<int> g[3]; int cmp[N][3]; int getans(int i, int aused, int bused, int prev){ int cnt[] = {aused, bused, i - (aused + bused)}; int tr = INF; for(int j = 0;j<3;j++){ if(j==prev || g[j].size() <= cnt[j]) continue; int currind = g[j][cnt[j]]; for(int k = 0;k<3;k++){ if(k==j) continue; currind += max((int)0, cnt[k] - cmp[g[j][cnt[j]]][k]); //cout<<"equivalent at index "<<g[j][cnt[j]]<<" for type "<<k<<" is "<<cmp[g[j][cnt[j]]][k]<<endl; } cnt[j]++; //cout<<"putting "<<j<<", cost is "<<(currind - (i+1))<<endl; tr = min(tr, (currind - (i+1)) + ((i==(n-1))?0:dp[i+1][cnt[0]][cnt[1]][j])); cnt[j]--; } return tr; } signed main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin>>n; string s; cin>>s; for(int i = 1;i<=n;i++){ if(s[i-1]=='R') arr[i] = 0; else if(s[i-1]=='G') arr[i] = 1; else arr[i] = 2; cmp[i][0] = g[0].size(); cmp[i][1] = g[1].size(); cmp[i][2] = g[2].size(); g[arr[i]].push_back(i); } for(int i = 0;i<=n;i++) for(int j = 0;j<=n;j++) for(int k = 0;k<=n;k++) for(int x = 0;x<3;x++) dp[i][j][k][x] = INF; for(int i = n-1;i>=1;i--){ for(int aused = n;aused>=0;aused--){ for(int bused = n;bused >= 0 ;bused--){ int cnt[] = {aused, bused, i - (aused + bused)}; int use[] = {INF, INF, INF}; if(cnt[0]>g[0].size() || cnt[1]>g[1].size() || cnt[2]>g[2].size()) continue; if(cnt[2]<0) continue; int currind; //calculate use[0] if(g[0].size() <= cnt[0]) goto use1; currind = g[0][cnt[0]] + max((int)0, cnt[1] - cmp[g[0][cnt[0]]][1]) + max((int)0, cnt[2] - cmp[g[0][cnt[0]]][2]); use[0] = (currind - (i+1)) + ((i==(n-1))?0:dp[i+1][cnt[0]+1][cnt[1]][0]); use1: //calculate use[1] if(g[1].size() <= cnt[1]) goto use2; currind = g[1][cnt[1]] + max((int)0, cnt[0] - cmp[g[1][cnt[1]]][0]) + max((int)0, cnt[2] - cmp[g[1][cnt[1]]][2]); use[1] = (currind - (i+1)) + ((i==(n-1))?0:dp[i+1][cnt[0]][cnt[1]+1][1]); use2: //calculate use[2] if(g[2].size() <= cnt[2]) goto endcalc; currind = g[2][cnt[2]] + max((int)0, cnt[0] - cmp[g[2][cnt[2]]][0]) + max((int)0, cnt[1] - cmp[g[2][cnt[2]]][1]); use[2] = (currind - (i+1)) + ((i==(n-1))?0:dp[i+1][cnt[0]][cnt[1]][2]); endcalc: dp[i][aused][bused][0] = min(use[1], use[2]); dp[i][aused][bused][1] = min(use[0], use[2]); dp[i][aused][bused][2] = min(use[1], use[0]); } } } int ans = getans(0, 0, 0, 4); if(ans>=INF) ans = -1; cout<<ans<<"\n"; } //2^(2k - 1) - 2^(k-1) /* 4 4 RGWR GRRG WGGW WWWR 5 5 RGRGW GRRGW WGGWR RWRGW RGWGW 20 YYGYYYGGGGRGYYGRGRYG */

Compilation message (stderr)

joi2019_ho_t3.cpp: In function 'int getans(int, int, int, int)':
joi2019_ho_t3.cpp:24:35: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         if(j==prev || g[j].size() <= cnt[j]) continue;
                       ~~~~~~~~~~~~^~~~~~~~~
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:60:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
                 if(cnt[0]>g[0].size() || cnt[1]>g[1].size() || cnt[2]>g[2].size()) continue;
                    ~~~~~~^~~~~~~~~~~~
joi2019_ho_t3.cpp:60:48: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
                 if(cnt[0]>g[0].size() || cnt[1]>g[1].size() || cnt[2]>g[2].size()) continue;
                                          ~~~~~~^~~~~~~~~~~~
joi2019_ho_t3.cpp:60:70: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
                 if(cnt[0]>g[0].size() || cnt[1]>g[1].size() || cnt[2]>g[2].size()) continue;
                                                                ~~~~~~^~~~~~~~~~~~
joi2019_ho_t3.cpp:64:32: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
                 if(g[0].size() <= cnt[0]) goto use1;
                    ~~~~~~~~~~~~^~~~~~~~~
joi2019_ho_t3.cpp:69:32: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
                 if(g[1].size() <= cnt[1]) goto use2;
                    ~~~~~~~~~~~~^~~~~~~~~
joi2019_ho_t3.cpp:74:32: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
                 if(g[2].size() <= cnt[2]) goto endcalc;
                    ~~~~~~~~~~~~^~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...