Submission #938163

#TimeUsernameProblemLanguageResultExecution timeMemory
938163LittleOrangeGrowing Vegetable is Fun 3 (JOI19_ho_t3)C++17
0 / 100
470 ms1048576 KiB
#include<bits/stdc++.h> using namespace std; using ll = long long; struct BIT{ ll n; vector<ll> a; BIT(ll N):n(N),a(N+1){} void add(ll i, ll x){ for(;i<=n;i+=i&-i) a[i] += x; } ll get(ll i){ ll x = 0; for(;i;i-=i&-i) x +=a[i]; return x; } }; int main(){ ios::sync_with_stdio(0);cin.tie(0); ll n; string s; cin >> n >> s; vector<ll> cnt(3,0); vector<ll> a(n); vector<vector<ll>> ps(3); for(ll i = 0;i<n;i++) { a[i] = s[i]=='R'?0:s[i]=='G'?1:2; cnt[a[i]]++; ps[a[i]].push_back(i+1); } vector<vector<vector<vector<ll>>>> dp(3,vector<vector<vector<ll>>>(n+5,vector<vector<ll>>(n+5,vector<ll>(n+5,1e18)))); dp[0][0][0][0] = dp[1][0][0][0] = dp[2][0][0][0] = 0; for(ll i = 0;i<=cnt[0];i++){ for(ll j = 0;j<=cnt[1];j++){ for(ll k = 0;k<=cnt[2];k++){ ll pos = i+j+k; if(i>0) dp[0][i][j][k] = min(dp[0][i][j][k],min(dp[1][i-1][j][k],dp[2][i-1][j][k])+abs(pos-ps[0][i-1])); if(j>0) dp[1][i][j][k] = min(dp[1][i][j][k],min(dp[0][i][j-1][k],dp[2][i][j-1][k])+abs(pos-ps[1][j-1])); if(k>0) dp[2][i][j][k] = min(dp[2][i][j][k],min(dp[0][i][j][k-1],dp[1][i][j][k-1])+abs(pos-ps[2][k-1])); } } } ll ans = 1e18; for(ll i = 0;i<3;i++) ans = min(ans,dp[i][cnt[0]][cnt[1]][cnt[2]]); if (ans>=1e18){ cout << "-1\n"; return 0; } cout << ans/2 << "\n"; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...