답안 #825469

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
825469 2023-08-14T21:19:27 Z taher Growing Vegetable is Fun 3 (JOI19_ho_t3) C++17
100 / 100
474 ms 1017568 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 = 123456789;
const int N = 402;

int dp[N][N][N][4];
int lb[N][3];

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

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

  vector<vector<int>> at(3);
  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 index_of_char, int type, int nR, int nG, int nY) {
    if (type == 0) {
      int bigger_gs = nG - lb[index_of_char][1];
      int bigger_ys = nY - lb[index_of_char][2];
      return index_of_char + max(0, bigger_gs) + max(0, bigger_ys);
    } else if (type == 1) {
      int bigger_rs = nR - lb[index_of_char][0];
      int bigger_ys = nY - lb[index_of_char][2];
      return index_of_char + max(0, bigger_rs) + max(0, bigger_ys);
    } else {
      int bigger_rs = nR - lb[index_of_char][0];
      int bigger_gs = nG - lb[index_of_char][1];
      return index_of_char + max(0, bigger_rs) + max(0, bigger_gs);
    }
  };

  memset(dp, -1, sizeof(dp));

  function<int(int, int, int, int)> DP = [&](int i, int nR, int nG, int previous_char) {
    if (i == n) {
      return 0;
    }

    if (dp[i][nR][nG][previous_char] != -1) {
      return dp[i][nR][nG][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 dp[i][nR][nG][previous_char] = ans;
  };

  int res = DP(0, 0, 0, 3);
  if (res == inf) {
    cout << -1 << '\n';
  } else {
    cout << res << '\n';
  }
  return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 380 ms 1017324 KB Output is correct
2 Correct 315 ms 1017276 KB Output is correct
3 Correct 338 ms 1017276 KB Output is correct
4 Correct 303 ms 1017316 KB Output is correct
5 Correct 321 ms 1017336 KB Output is correct
6 Correct 307 ms 1017292 KB Output is correct
7 Correct 324 ms 1017372 KB Output is correct
8 Correct 309 ms 1017240 KB Output is correct
9 Correct 319 ms 1017400 KB Output is correct
10 Correct 303 ms 1017316 KB Output is correct
11 Correct 306 ms 1017320 KB Output is correct
12 Correct 323 ms 1017236 KB Output is correct
13 Correct 307 ms 1017348 KB Output is correct
14 Correct 332 ms 1017364 KB Output is correct
15 Correct 321 ms 1017292 KB Output is correct
16 Correct 301 ms 1017268 KB Output is correct
17 Correct 314 ms 1017348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 380 ms 1017324 KB Output is correct
2 Correct 315 ms 1017276 KB Output is correct
3 Correct 338 ms 1017276 KB Output is correct
4 Correct 303 ms 1017316 KB Output is correct
5 Correct 321 ms 1017336 KB Output is correct
6 Correct 307 ms 1017292 KB Output is correct
7 Correct 324 ms 1017372 KB Output is correct
8 Correct 309 ms 1017240 KB Output is correct
9 Correct 319 ms 1017400 KB Output is correct
10 Correct 303 ms 1017316 KB Output is correct
11 Correct 306 ms 1017320 KB Output is correct
12 Correct 323 ms 1017236 KB Output is correct
13 Correct 307 ms 1017348 KB Output is correct
14 Correct 332 ms 1017364 KB Output is correct
15 Correct 321 ms 1017292 KB Output is correct
16 Correct 301 ms 1017268 KB Output is correct
17 Correct 314 ms 1017348 KB Output is correct
18 Correct 326 ms 1017324 KB Output is correct
19 Correct 312 ms 1017296 KB Output is correct
20 Correct 313 ms 1017268 KB Output is correct
21 Correct 310 ms 1017244 KB Output is correct
22 Correct 302 ms 1017268 KB Output is correct
23 Correct 313 ms 1017252 KB Output is correct
24 Correct 310 ms 1017292 KB Output is correct
25 Correct 305 ms 1017244 KB Output is correct
26 Correct 319 ms 1017292 KB Output is correct
27 Correct 306 ms 1017292 KB Output is correct
28 Correct 317 ms 1017272 KB Output is correct
29 Correct 312 ms 1017300 KB Output is correct
30 Correct 303 ms 1017328 KB Output is correct
31 Correct 306 ms 1017352 KB Output is correct
32 Correct 348 ms 1017344 KB Output is correct
33 Correct 315 ms 1017344 KB Output is correct
34 Correct 317 ms 1017296 KB Output is correct
35 Correct 313 ms 1017428 KB Output is correct
36 Correct 306 ms 1017260 KB Output is correct
37 Correct 317 ms 1017424 KB Output is correct
38 Correct 322 ms 1017364 KB Output is correct
39 Correct 314 ms 1017252 KB Output is correct
40 Correct 310 ms 1017276 KB Output is correct
41 Correct 318 ms 1017292 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 317 ms 1017340 KB Output is correct
2 Correct 333 ms 1017376 KB Output is correct
3 Correct 308 ms 1017400 KB Output is correct
4 Correct 310 ms 1017420 KB Output is correct
5 Correct 320 ms 1017348 KB Output is correct
6 Correct 315 ms 1017312 KB Output is correct
7 Correct 310 ms 1017400 KB Output is correct
8 Correct 330 ms 1017568 KB Output is correct
9 Correct 306 ms 1017424 KB Output is correct
10 Correct 338 ms 1017396 KB Output is correct
11 Correct 316 ms 1017424 KB Output is correct
12 Correct 321 ms 1017368 KB Output is correct
13 Correct 306 ms 1017352 KB Output is correct
14 Correct 309 ms 1017424 KB Output is correct
15 Correct 319 ms 1017324 KB Output is correct
16 Correct 317 ms 1017364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 380 ms 1017324 KB Output is correct
2 Correct 315 ms 1017276 KB Output is correct
3 Correct 338 ms 1017276 KB Output is correct
4 Correct 303 ms 1017316 KB Output is correct
5 Correct 321 ms 1017336 KB Output is correct
6 Correct 307 ms 1017292 KB Output is correct
7 Correct 324 ms 1017372 KB Output is correct
8 Correct 309 ms 1017240 KB Output is correct
9 Correct 319 ms 1017400 KB Output is correct
10 Correct 303 ms 1017316 KB Output is correct
11 Correct 306 ms 1017320 KB Output is correct
12 Correct 323 ms 1017236 KB Output is correct
13 Correct 307 ms 1017348 KB Output is correct
14 Correct 332 ms 1017364 KB Output is correct
15 Correct 321 ms 1017292 KB Output is correct
16 Correct 301 ms 1017268 KB Output is correct
17 Correct 314 ms 1017348 KB Output is correct
18 Correct 326 ms 1017324 KB Output is correct
19 Correct 312 ms 1017296 KB Output is correct
20 Correct 313 ms 1017268 KB Output is correct
21 Correct 310 ms 1017244 KB Output is correct
22 Correct 302 ms 1017268 KB Output is correct
23 Correct 313 ms 1017252 KB Output is correct
24 Correct 310 ms 1017292 KB Output is correct
25 Correct 305 ms 1017244 KB Output is correct
26 Correct 319 ms 1017292 KB Output is correct
27 Correct 306 ms 1017292 KB Output is correct
28 Correct 317 ms 1017272 KB Output is correct
29 Correct 312 ms 1017300 KB Output is correct
30 Correct 303 ms 1017328 KB Output is correct
31 Correct 306 ms 1017352 KB Output is correct
32 Correct 348 ms 1017344 KB Output is correct
33 Correct 315 ms 1017344 KB Output is correct
34 Correct 317 ms 1017296 KB Output is correct
35 Correct 313 ms 1017428 KB Output is correct
36 Correct 306 ms 1017260 KB Output is correct
37 Correct 317 ms 1017424 KB Output is correct
38 Correct 322 ms 1017364 KB Output is correct
39 Correct 314 ms 1017252 KB Output is correct
40 Correct 310 ms 1017276 KB Output is correct
41 Correct 318 ms 1017292 KB Output is correct
42 Correct 317 ms 1017340 KB Output is correct
43 Correct 333 ms 1017376 KB Output is correct
44 Correct 308 ms 1017400 KB Output is correct
45 Correct 310 ms 1017420 KB Output is correct
46 Correct 320 ms 1017348 KB Output is correct
47 Correct 315 ms 1017312 KB Output is correct
48 Correct 310 ms 1017400 KB Output is correct
49 Correct 330 ms 1017568 KB Output is correct
50 Correct 306 ms 1017424 KB Output is correct
51 Correct 338 ms 1017396 KB Output is correct
52 Correct 316 ms 1017424 KB Output is correct
53 Correct 321 ms 1017368 KB Output is correct
54 Correct 306 ms 1017352 KB Output is correct
55 Correct 309 ms 1017424 KB Output is correct
56 Correct 319 ms 1017324 KB Output is correct
57 Correct 317 ms 1017364 KB Output is correct
58 Correct 438 ms 1017544 KB Output is correct
59 Correct 439 ms 1017540 KB Output is correct
60 Correct 456 ms 1017424 KB Output is correct
61 Correct 432 ms 1017428 KB Output is correct
62 Correct 318 ms 1017352 KB Output is correct
63 Correct 320 ms 1017360 KB Output is correct
64 Correct 334 ms 1017432 KB Output is correct
65 Correct 369 ms 1017340 KB Output is correct
66 Correct 420 ms 1017448 KB Output is correct
67 Correct 417 ms 1017372 KB Output is correct
68 Correct 443 ms 1017428 KB Output is correct
69 Correct 450 ms 1017460 KB Output is correct
70 Correct 474 ms 1017396 KB Output is correct
71 Correct 446 ms 1017420 KB Output is correct
72 Correct 330 ms 1017344 KB Output is correct
73 Correct 303 ms 1017360 KB Output is correct