Submission #99752

#TimeUsernameProblemLanguageResultExecution timeMemory
99752MrTEKGrowing Vegetable is Fun 3 (JOI19_ho_t3)C++14
100 / 100
148 ms163064 KiB
#include <bits/stdc++.h>

using namespace std;

typedef long long int ll;
typedef pair<int,int> ii;

const int N = 4e2 + 5;
const int INF = 1e9;

int n,f[3],mpx[300],dp[N][N][N][3];
char s[N];
vector <pair <int,ii>> pos[3];

inline int calc(pair <int,ii> x,pair <int,ii> y) {
  return abs(x.first - y.first) + abs(x.second.first - y.second.first) + abs(x.second.second - y.second.second);
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL); cout.tie(NULL);
  mpx['R'] = 0;
  mpx['G'] = 1;
  mpx['Y'] = 2;
  cin >> n >> (s + 1);
  for (int i = 1 ; i <= n ; i++) {
    f[mpx[s[i]]]++;
    pos[mpx[s[i]]].push_back({f[0],{f[1],f[2]}});
  }
  for (int r = 0 ; r <= f[0] ; r++) {
    for (int g = 0 ; g <= f[1] ; g++) {
      for (int y = 0 ; y <= f[2] ; y++) {
        if (r + g + y == 0)
          continue;
        for (int i = 0 ; i < 3 ; i++)
          dp[r][g][y][i] = INF;
        if (r > 0)
          dp[r][g][y][0] = min(dp[r - 1][g][y][1],dp[r - 1][g][y][2]) + calc(pos[0][r - 1],{r,{g,y}});
        if (g > 0)
          dp[r][g][y][1] = min(dp[r][g - 1][y][0],dp[r][g - 1][y][2]) + calc(pos[1][g - 1],{r,{g,y}});
        if (y > 0)
          dp[r][g][y][2] = min(dp[r][g][y - 1][0],dp[r][g][y - 1][1]) + calc(pos[2][y - 1],{r,{g,y}});
      }
    }
  }
  int ans = INF;
  for (int i = 0 ; i < 3 ; i++)
    ans = min(ans,dp[f[0]][f[1]][f[2]][i]);
  if (ans == INF)
    cout << "-1" << endl;
  else
    cout << ans / 2 << endl;
}

Compilation message (stderr)

joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:27:15: warning: array subscript has type 'char' [-Wchar-subscripts]
     f[mpx[s[i]]]++;
               ^
joi2019_ho_t3.cpp:28:17: warning: array subscript has type 'char' [-Wchar-subscripts]
     pos[mpx[s[i]]].push_back({f[0],{f[1],f[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...