답안 #924078

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
924078 2024-02-08T11:40:30 Z MercubytheFirst Growing Vegetable is Fun 3 (JOI19_ho_t3) C++17
75 / 100
500 ms 757728 KB
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
#define pb push_back
const ll N = 105;
const ll inf = 1e9 + 37;
#define endl '\n'

/*
- have fun!
- monovariants?
- invariants?
- bs on ans?
- phrase it differently?
- quirky constraints? 
- dp? 
- greedy? 
- find slow solution and try to optimize?
*/

void solve(void){
  int n;
  string s;
  cin >> n >> s;
  s = "0" + s;
  for(int i = 1; i <= n; ++i){
    if(s[i] == 'R') s[i] = 0;
    else if(s[i] == 'G') s[i] = 1;
    else if(s[i] == 'Y') s[i] = 2;
  }
  vector<int> k[3];
  k[0].pb(0);
  k[1].pb(0);
  k[2].pb(0);
  for(int i = 1; i <= n; ++i)
    k[(int)s[i]].push_back(i);
  vector<array<int, 3> > pre(n + 1, array<int, 3>{0, 0, 0});
  for(int i = 1; i <= n; ++i){
    pre[i] = pre[i - 1];
    pre[i][s[i]]++;
  }
  int dp[n + 1][n + 1][n + 1][3];
  for(int r = 0; r <= n; ++r)
    for(int g = 0; g <= n; ++g)
      for(int y = 0; y <= n; ++y)
        for(int c = 0; c <3; ++c)
          dp[r][g][y][c] = inf;
  dp[0][0][0][0] = dp[0][0][0][1] = dp[0][0][0][2] = 0;
  int c[3] = {0};
  // cout << pre[n][0] << ' ' << pre[n][1] << ' ' << pre[n][2] << endl;
  for(c[0] = 0; c[0] <= pre[n][0]; ++c[0]){
    for(c[1] = 0; c[1] <= pre[n][1]; ++c[1]){
      for(c[2] = 0; c[2] <= pre[n][2]; ++c[2]){
        for(int col = 0; col <3; ++col){
          if(!c[0] and !c[1] and !c[2])
            continue;
          int& ans = dp[c[0]][c[1]][c[2]][col];
          if(c[col] <= 0){
            // cout << c[0] << ' ' << c[1] << ' ' << c[2] << ' ' << col << endl;
            continue;
          }
          // cout << c[0] << ' ' << c[1] << ' ' << c[2] << ' ' << col << endl;
          int i = c[0] + c[1] + c[2];
          int ni = k[col][c[col]], d = 0;
          for(int j = 0; j <3 ; ++j){
            if(j == col)
              continue;
            d += (pre[ni][j] >= c[j] ? 0 : c[j] - pre[ni][j]);
          }
          ni += d;
          for(int j = 0; j <3; ++j){
            if(j == col)
              continue;
            ans = min(ans, dp[c[0] - (col == 0)][c[1] - (col == 1)][c[2] - (col == 2)][j] + ni - i);
          }
        }
      }
    }
  }
  int ans = inf;
  for(int i = 0; i < 3; ++i){
    ans = min(ans, dp[pre[n][0]][pre[n][1]][pre[n][2]][i]);
    // cout << dp[pre[n][0]][pre[n][1]][pre[n][2]][i] << ' ';
  }
  if(ans == inf)
    ans = -1;
  cout << ans << endl;
}

signed main(){
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  // signed t; cin >> t; while(t--)
    solve();
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 3 ms 2908 KB Output is correct
19 Correct 2 ms 2908 KB Output is correct
20 Correct 2 ms 2908 KB Output is correct
21 Correct 2 ms 2904 KB Output is correct
22 Correct 2 ms 2908 KB Output is correct
23 Correct 2 ms 2908 KB Output is correct
24 Correct 2 ms 2908 KB Output is correct
25 Correct 2 ms 2908 KB Output is correct
26 Correct 2 ms 2908 KB Output is correct
27 Correct 2 ms 2908 KB Output is correct
28 Correct 2 ms 2908 KB Output is correct
29 Correct 2 ms 2904 KB Output is correct
30 Correct 3 ms 2908 KB Output is correct
31 Correct 2 ms 2908 KB Output is correct
32 Correct 2 ms 3016 KB Output is correct
33 Correct 2 ms 2908 KB Output is correct
34 Correct 2 ms 2908 KB Output is correct
35 Correct 2 ms 2652 KB Output is correct
36 Correct 2 ms 2908 KB Output is correct
37 Correct 2 ms 2652 KB Output is correct
38 Correct 2 ms 2908 KB Output is correct
39 Correct 2 ms 2908 KB Output is correct
40 Correct 2 ms 2904 KB Output is correct
41 Correct 3 ms 2908 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 442 ms 757332 KB Output is correct
3 Correct 433 ms 751888 KB Output is correct
4 Correct 425 ms 757588 KB Output is correct
5 Correct 457 ms 757332 KB Output is correct
6 Correct 442 ms 757588 KB Output is correct
7 Correct 462 ms 751692 KB Output is correct
8 Correct 430 ms 751900 KB Output is correct
9 Correct 422 ms 746080 KB Output is correct
10 Correct 442 ms 757728 KB Output is correct
11 Correct 426 ms 757332 KB Output is correct
12 Correct 64 ms 104528 KB Output is correct
13 Correct 147 ms 244564 KB Output is correct
14 Correct 243 ms 426064 KB Output is correct
15 Correct 434 ms 757452 KB Output is correct
16 Correct 429 ms 757500 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 3 ms 2908 KB Output is correct
19 Correct 2 ms 2908 KB Output is correct
20 Correct 2 ms 2908 KB Output is correct
21 Correct 2 ms 2904 KB Output is correct
22 Correct 2 ms 2908 KB Output is correct
23 Correct 2 ms 2908 KB Output is correct
24 Correct 2 ms 2908 KB Output is correct
25 Correct 2 ms 2908 KB Output is correct
26 Correct 2 ms 2908 KB Output is correct
27 Correct 2 ms 2908 KB Output is correct
28 Correct 2 ms 2908 KB Output is correct
29 Correct 2 ms 2904 KB Output is correct
30 Correct 3 ms 2908 KB Output is correct
31 Correct 2 ms 2908 KB Output is correct
32 Correct 2 ms 3016 KB Output is correct
33 Correct 2 ms 2908 KB Output is correct
34 Correct 2 ms 2908 KB Output is correct
35 Correct 2 ms 2652 KB Output is correct
36 Correct 2 ms 2908 KB Output is correct
37 Correct 2 ms 2652 KB Output is correct
38 Correct 2 ms 2908 KB Output is correct
39 Correct 2 ms 2908 KB Output is correct
40 Correct 2 ms 2904 KB Output is correct
41 Correct 3 ms 2908 KB Output is correct
42 Correct 0 ms 344 KB Output is correct
43 Correct 442 ms 757332 KB Output is correct
44 Correct 433 ms 751888 KB Output is correct
45 Correct 425 ms 757588 KB Output is correct
46 Correct 457 ms 757332 KB Output is correct
47 Correct 442 ms 757588 KB Output is correct
48 Correct 462 ms 751692 KB Output is correct
49 Correct 430 ms 751900 KB Output is correct
50 Correct 422 ms 746080 KB Output is correct
51 Correct 442 ms 757728 KB Output is correct
52 Correct 426 ms 757332 KB Output is correct
53 Correct 64 ms 104528 KB Output is correct
54 Correct 147 ms 244564 KB Output is correct
55 Correct 243 ms 426064 KB Output is correct
56 Correct 434 ms 757452 KB Output is correct
57 Correct 429 ms 757500 KB Output is correct
58 Execution timed out 502 ms 757556 KB Time limit exceeded
59 Halted 0 ms 0 KB -