제출 #573453

#제출 시각아이디문제언어결과실행 시간메모리
573453MohammadAghilGrowing Vegetable is Fun 3 (JOI19_ho_t3)C++14
100 / 100
110 ms163024 KiB
      #include <bits/stdc++.h>
//   #pragma GCC optimize ("Ofast,unroll-loops")
// #pragma GCC target ("avx2")
    using namespace std;
  typedef long long ll;
   typedef pair<int, int> pp;
    #define er(args ...) cerr << __LINE__ << ": ", err(new istringstream(string(#args)), args), cerr << endl
      #define per(i,r,l) for(int i = (r); i >= (l); i--)
        #define rep(i,l,r) for(int i = (l); i < (r); i++)
           #define all(x) x.begin(), x.end()
              #define sz(x) (int)(x).size()
                  #define pb push_back
                      #define ss second
                           #define ff first
                                   void err(istringstream *iss){}template<typename T,typename ...Args> void err(istringstream *iss,const T &_val, const Args&...args){string _name;*iss>>_name;if(_name.back()==',')_name.pop_back();cerr<<_name<<" = "<<_val<<", ",err(iss,args...);}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const ll mod = 1e9 + 7, maxn = 4e2 + 5, lg = 22, lng = 26, inf = ll(1e9) + 5;
ll pw(ll a,ll b,ll md=mod){if(!b)return 1;ll k=pw(a,b>>1ll);return k*k%md*(b&1ll?a:1)%md;}

vector<int> pos[3];
int dp[maxn][maxn][maxn][3];
int ptr[maxn][3], a[maxn];

int main(){
     cin.tie(0) -> sync_with_stdio(0);
     int n; cin >> n;
     string s; cin >> s;
     rep(i,0,n){
          int t = 0;
          if(s[i] == 'G') t = 1;
          if(s[i] == 'Y') t = 2;
          rep(j,0,3) ptr[i][j] = sz(pos[j]);
          pos[t].pb(i), a[i] = t;
     }
     
     auto clc_pos = [&](int a, int b, int c, int pos){
          return pos + max(0, a - ptr[pos][0]) + max(0, b - ptr[pos][1]) + max(0, c - ptr[pos][2]);
     };

     int A = sz(pos[0]), B = sz(pos[1]), C = sz(pos[2]);
     rep(i,0,A+1) rep(j,0,B+1) rep(k,0,C+1) rep(l,0,3) dp[i][j][k][l] = inf;
     if(A) dp[1][0][0][0] = pos[0][0];
     if(B) dp[0][1][0][1] = pos[1][0];
     if(C) dp[0][0][1][2] = pos[2][0];
     rep(p,1,n){
          rep(i,0,min(p,A)+1){
               rep(j,0,min(p,B)+1){
                    int k = p - i - j;
                    if(k < 0) break;
                    if(k > C) continue;
                    rep(l,0,3){
                         if(i < A && l) dp[i+1][j][k][0] = min(dp[i+1][j][k][0], dp[i][j][k][l] + clc_pos(i, j, k, pos[0][i]) - p);
                         if(j < B && l - 1) dp[i][j+1][k][1] = min(dp[i][j+1][k][1], dp[i][j][k][l] + clc_pos(i, j, k, pos[1][j]) - p);
                         if(k < C && l - 2) dp[i][j][k+1][2] = min(dp[i][j][k+1][2], dp[i][j][k][l] + clc_pos(i, j, k, pos[2][k]) - p);
                    }
               }
          }
     }
     int ans = inf;
     rep(i,0,3) ans = min(ans, dp[A][B][C][i]);
     cout << (ans == inf? -1: ans) << '\n';
     return 0; 
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...