Submission #825468

# Submission time Handle Problem Language Result Execution time Memory
825468 2023-08-14T21:13:38 Z taher Growing Vegetable is Fun 3 (JOI19_ho_t3) C++17
100 / 100
198 ms 207080 KB
#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#include "C:\GCC\debug.h"
#else
#define debug(...) void(42)
#endif

int Convert(char c) {
  if (c == 'R') {
    return 0;
  }
  if (c == 'G') {
    return 1;
  }
  return 2;
}

const int inf = 166789;
const int N = 402;

int dp[N][N][N][3];
int lb[N][3];
vector<int> at[3];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int n;
  cin >> n;
  string s;
  cin >> s;

  vector<int> cnt(26);
  for (int i = 0; i < n; i++) {
    cnt[s[i] - 'A'] += 1;
  }
  vector<int> order(26);
  iota(order.begin(), order.end(), 0);
  sort(order.begin(), order.end(), [&](int i, int j) {
    return cnt[i] > cnt[j];
  });
  vector<char> trans(26);
  trans[order[0]] = 'Y';
  trans[order[1]] = 'G';
  trans[order[2]] = 'R';
  for (int i = 0; i < n; i++) {
    s[i] = trans[s[i] - 'A'];
  }

  for (int i = 0; i < n; i++) {
    at[Convert(s[i])].push_back(i);
  }

  lb[0][0] = lb[0][1] = lb[0][2] = 0;
  lb[0][Convert(s[0])] = 1;

  for (int i = 1; i < n; i++) {
    lb[i][0] = lb[i - 1][0];
    lb[i][1] = lb[i - 1][1];
    lb[i][2] = lb[i - 1][2];
    lb[i][Convert(s[i])] += 1;
  }


  auto Advance = [&](int loc, int type, int nR, int nG, int nY) {
    int bRs = max(0, nR - lb[loc][0]);
    int bGs = max(0, nG - lb[loc][1]);
    int bYs = max(0, nY - lb[loc][2]);
    if (type == 0) {
      return loc + max(0, bGs) + max(0, bYs);
    } else if (type == 1) {
      return loc + max(0, bRs) + max(0, bYs);
    }
    return loc + max(0, bRs) + max(0, bGs);
  };

  for (int i = 0; i <= (int) at[0].size(); i++) {
    for (int j = 0; j <= (int) at[1].size(); j++) {
      for (int p = 0; p <= 2; p++) {
        dp[n][i][j][p] = 0;
      }
    }
  }

  for (int i = n - 1; i >= 0; i--) {
    for (int nR = min((int) at[0].size(), i); nR >= 0; nR--) {
      for (int nG = min(i - nR, (int) at[1].size()); nG >= 0; nG--) {
        dp[i][nR][nG][0] = dp[i][nR][nG][1] = dp[i][nR][nG][2] = inf;

        auto Compute = [&](int previous_char) {
          int nY = i  - (nR + nG);
          int ans = inf;
          if (nR < (int) at[0].size() && previous_char != 0) {
            ans = min(ans, dp[i + 1][nR + 1][nG][0] + max(0, Advance(at[0][nR], 0, nR, nG, nY) - i));
          }
          if (nG < (int) at[1].size() && previous_char != 1) {
            ans = min(ans, dp[i + 1][nR][nG + 1][1] + max(0, Advance(at[1][nG], 1, nR, nG, nY) - i));
          }
          if (nY < (int) at[2].size() && previous_char != 2) {
            ans = min(ans, dp[i + 1][nR][nG][2] + max(0, Advance(at[2][nY], 2, nR, nG, nY) - i));
          }
          return ans;
        };


        if (i > 0) {
          for (int previous_char = 0; previous_char <= 2; previous_char++) {
            dp[i][nR][nG][previous_char] = min(dp[i][nR][nG][previous_char], Compute(previous_char));
          }
        } else {
          dp[i][nR][nG][0] = min(dp[i][nR][nG][0], Compute(-1));
        }
      }
    }
  }

  if (dp[0][0][0][0] == inf) {
    cout << -1 << '\n';
  } else {
    cout << dp[0][0][0][0] << '\n';
  }
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 324 KB Output is correct
2 Correct 0 ms 328 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 0 ms 596 KB Output is correct
6 Correct 1 ms 596 KB Output is correct
7 Correct 1 ms 596 KB Output is correct
8 Correct 1 ms 596 KB Output is correct
9 Correct 0 ms 468 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 0 ms 596 KB Output is correct
12 Correct 1 ms 596 KB Output is correct
13 Correct 1 ms 596 KB Output is correct
14 Correct 1 ms 596 KB Output is correct
15 Correct 1 ms 596 KB Output is correct
16 Correct 0 ms 456 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 324 KB Output is correct
2 Correct 0 ms 328 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 0 ms 596 KB Output is correct
6 Correct 1 ms 596 KB Output is correct
7 Correct 1 ms 596 KB Output is correct
8 Correct 1 ms 596 KB Output is correct
9 Correct 0 ms 468 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 0 ms 596 KB Output is correct
12 Correct 1 ms 596 KB Output is correct
13 Correct 1 ms 596 KB Output is correct
14 Correct 1 ms 596 KB Output is correct
15 Correct 1 ms 596 KB Output is correct
16 Correct 0 ms 456 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 3 ms 4692 KB Output is correct
19 Correct 2 ms 4436 KB Output is correct
20 Correct 2 ms 4308 KB Output is correct
21 Correct 2 ms 4692 KB Output is correct
22 Correct 2 ms 3268 KB Output is correct
23 Correct 2 ms 4436 KB Output is correct
24 Correct 2 ms 3156 KB Output is correct
25 Correct 1 ms 980 KB Output is correct
26 Correct 1 ms 980 KB Output is correct
27 Correct 2 ms 3284 KB Output is correct
28 Correct 3 ms 4820 KB Output is correct
29 Correct 3 ms 4564 KB Output is correct
30 Correct 2 ms 4436 KB Output is correct
31 Correct 2 ms 4052 KB Output is correct
32 Correct 2 ms 4564 KB Output is correct
33 Correct 1 ms 1236 KB Output is correct
34 Correct 1 ms 1748 KB Output is correct
35 Correct 2 ms 3540 KB Output is correct
36 Correct 2 ms 4180 KB Output is correct
37 Correct 2 ms 3540 KB Output is correct
38 Correct 2 ms 3924 KB Output is correct
39 Correct 3 ms 4820 KB Output is correct
40 Correct 1 ms 724 KB Output is correct
41 Correct 1 ms 2132 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 324 KB Output is correct
2 Correct 3 ms 4052 KB Output is correct
3 Correct 3 ms 4036 KB Output is correct
4 Correct 3 ms 4052 KB Output is correct
5 Correct 3 ms 4052 KB Output is correct
6 Correct 3 ms 4052 KB Output is correct
7 Correct 3 ms 3988 KB Output is correct
8 Correct 3 ms 4004 KB Output is correct
9 Correct 3 ms 4052 KB Output is correct
10 Correct 3 ms 4036 KB Output is correct
11 Correct 3 ms 4052 KB Output is correct
12 Correct 2 ms 1992 KB Output is correct
13 Correct 3 ms 2772 KB Output is correct
14 Correct 2 ms 3284 KB Output is correct
15 Correct 3 ms 4052 KB Output is correct
16 Correct 3 ms 4052 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 324 KB Output is correct
2 Correct 0 ms 328 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 0 ms 596 KB Output is correct
6 Correct 1 ms 596 KB Output is correct
7 Correct 1 ms 596 KB Output is correct
8 Correct 1 ms 596 KB Output is correct
9 Correct 0 ms 468 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 0 ms 596 KB Output is correct
12 Correct 1 ms 596 KB Output is correct
13 Correct 1 ms 596 KB Output is correct
14 Correct 1 ms 596 KB Output is correct
15 Correct 1 ms 596 KB Output is correct
16 Correct 0 ms 456 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 3 ms 4692 KB Output is correct
19 Correct 2 ms 4436 KB Output is correct
20 Correct 2 ms 4308 KB Output is correct
21 Correct 2 ms 4692 KB Output is correct
22 Correct 2 ms 3268 KB Output is correct
23 Correct 2 ms 4436 KB Output is correct
24 Correct 2 ms 3156 KB Output is correct
25 Correct 1 ms 980 KB Output is correct
26 Correct 1 ms 980 KB Output is correct
27 Correct 2 ms 3284 KB Output is correct
28 Correct 3 ms 4820 KB Output is correct
29 Correct 3 ms 4564 KB Output is correct
30 Correct 2 ms 4436 KB Output is correct
31 Correct 2 ms 4052 KB Output is correct
32 Correct 2 ms 4564 KB Output is correct
33 Correct 1 ms 1236 KB Output is correct
34 Correct 1 ms 1748 KB Output is correct
35 Correct 2 ms 3540 KB Output is correct
36 Correct 2 ms 4180 KB Output is correct
37 Correct 2 ms 3540 KB Output is correct
38 Correct 2 ms 3924 KB Output is correct
39 Correct 3 ms 4820 KB Output is correct
40 Correct 1 ms 724 KB Output is correct
41 Correct 1 ms 2132 KB Output is correct
42 Correct 0 ms 324 KB Output is correct
43 Correct 3 ms 4052 KB Output is correct
44 Correct 3 ms 4036 KB Output is correct
45 Correct 3 ms 4052 KB Output is correct
46 Correct 3 ms 4052 KB Output is correct
47 Correct 3 ms 4052 KB Output is correct
48 Correct 3 ms 3988 KB Output is correct
49 Correct 3 ms 4004 KB Output is correct
50 Correct 3 ms 4052 KB Output is correct
51 Correct 3 ms 4036 KB Output is correct
52 Correct 3 ms 4052 KB Output is correct
53 Correct 2 ms 1992 KB Output is correct
54 Correct 3 ms 2772 KB Output is correct
55 Correct 2 ms 3284 KB Output is correct
56 Correct 3 ms 4052 KB Output is correct
57 Correct 3 ms 4052 KB Output is correct
58 Correct 198 ms 197584 KB Output is correct
59 Correct 181 ms 191112 KB Output is correct
60 Correct 186 ms 201424 KB Output is correct
61 Correct 194 ms 207080 KB Output is correct
62 Correct 12 ms 11476 KB Output is correct
63 Correct 22 ms 18760 KB Output is correct
64 Correct 74 ms 64600 KB Output is correct
65 Correct 110 ms 102228 KB Output is correct
66 Correct 195 ms 204600 KB Output is correct
67 Correct 179 ms 183140 KB Output is correct
68 Correct 190 ms 204576 KB Output is correct
69 Correct 193 ms 205140 KB Output is correct
70 Correct 186 ms 198788 KB Output is correct
71 Correct 177 ms 185252 KB Output is correct
72 Correct 38 ms 52172 KB Output is correct
73 Correct 9 ms 11220 KB Output is correct