제출 #1231724

#제출 시각아이디문제언어결과실행 시간메모리
1231724PlayVoltzGrowing Vegetable is Fun 3 (JOI19_ho_t3)C++20
100 / 100
163 ms134856 KiB
#include <cstdio> #include <stdio.h> #include <stdbool.h> #include <iostream> #include <map> #include <vector> #include <climits> #include <stack> #include <string> #include <queue> #include <algorithm> #include <set> #include <unordered_set> #include <unordered_map> #include <cmath> #include <cctype> #include <bitset> #include <iomanip> #include <cstring> #include <numeric> #include <cassert> #include <random> #include <chrono> #include <fstream> using namespace std; #define int long long #define pii pair<int, int> #define mp make_pair #define pb push_back #define fi first #define se second int32_t main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; string s; cin>>n>>s; s=' '+s; vector<int> r, g, y; vector<vector<int> > psum(3, vector<int>(n+1, 0)); for (int i=1; i<=n; ++i){ if (s[i]=='R')r.pb(i), ++psum[0][i]; else if (s[i]=='G')g.pb(i), ++psum[1][i]; else y.pb(i), ++psum[2][i]; psum[0][i]+=psum[0][i-1]; psum[1][i]+=psum[1][i-1]; psum[2][i]+=psum[2][i-1]; } vector<vector<vector<vector<int> > > > dp(r.size()+1, vector<vector<vector<int> > >(g.size()+1, vector<vector<int> >(y.size()+1, vector<int>(3, LLONG_MAX/2)))); dp[0][0][0][0]=dp[0][0][0][1]=dp[0][0][0][2]=0; for (int i=0; i<=r.size(); ++i)for (int j=0; j<=g.size(); ++j)for (int l=0; l<=y.size(); ++l)for (int p=0; p<3; ++p){ if (i&&p)dp[i][j][l][0]=min(dp[i][j][l][0], dp[i-1][j][l][p]+abs(i+j+l-r[i-1]-max(0ll, j?psum[1][g[j-1]]-psum[1][r[i-1]]:0ll)-max(0ll, l?psum[2][y[l-1]]-psum[2][r[i-1]]:0ll))); if (j&&p!=1)dp[i][j][l][1]=min(dp[i][j][l][1], dp[i][j-1][l][p]+abs(i+j+l-g[j-1]-max(0ll, i?psum[0][r[i-1]]-psum[0][g[j-1]]:0ll)-max(0ll, l?psum[2][y[l-1]]-psum[2][g[j-1]]:0ll))); if (l&&p!=2)dp[i][j][l][2]=min(dp[i][j][l][2], dp[i][j][l-1][p]+abs(i+j+l-y[l-1]-max(0ll, i?psum[0][r[i-1]]-psum[0][y[l-1]]:0ll)-max(0ll, j?psum[1][g[j-1]]-psum[1][y[l-1]]:0ll))); } if (min({dp[r.size()][g.size()][y.size()][0], dp[r.size()][g.size()][y.size()][1], dp[r.size()][g.size()][y.size()][2]})==LLONG_MAX/2)cout<<-1; else cout<<min({dp[r.size()][g.size()][y.size()][0], dp[r.size()][g.size()][y.size()][1], dp[r.size()][g.size()][y.size()][2]}); }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...