# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
866009 | 2023-10-25T09:44:57 Z | guagua0407 | Growing Vegetable is Fun 3 (JOI19_ho_t3) | C++17 | 400 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); } memset(dp,0x3f3f3f3f,sizeof(dp)); 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);k++){ int r=i-j-k; if(r<0 or min(cnt[n][2],i)<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
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Runtime error | 400 ms | 1048576 KB | Execution killed with signal 9 |
2 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Runtime error | 400 ms | 1048576 KB | Execution killed with signal 9 |
2 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Runtime error | 327 ms | 1048576 KB | Execution killed with signal 9 |
2 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Runtime error | 400 ms | 1048576 KB | Execution killed with signal 9 |
2 | Halted | 0 ms | 0 KB | - |