Submission #508415

# Submission time Handle Problem Language Result Execution time Memory
508415 2022-01-13T10:41:33 Z cig32 Growing Vegetable is Fun 3 (JOI19_ho_t3) C++17
100 / 100
467 ms 757524 KB
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
mt19937_64 rng((int)std::chrono::steady_clock::now().time_since_epoch().count());
const int MAXN = 3e5 + 10;
const int MOD = 1e9 + 7;
//#define int long long

int rnd(int x, int y) { // random number generator
  int u= uniform_int_distribution<int>(x, y)(rng); return u;
}


void solve(int tc) {
  int N;
  cin >> N;
  string S;
  cin >> S;
  S = " " + S;
  int dp[N+1][N+1][N+1][3];
  for(int i=0; i<=N; i++) {
    for(int j=0; j<=N; j++) {
      for(int k=0; k<=N; k++) {
        for(int l=0; l<3; l++) {
          dp[i][j][k][l] = 1e8;
        }
      }
    }
  }
  int before[N+1][3];
  int ordinal[N+1][3]; 
  for(int i=0; i<=N; i++) {
    for(int j=0; j<3; j++) {
      before[i][j] = 0;
      ordinal[i][j] = -1;
    }
  }
  string T = "RGY";
  for(int i=1; i<=N; i++) {
    for(int j=0; j<3; j++) {
      before[i][j] = before[i-1][j] + (S[i-1] == T[j]);
    }
  }
  int counter[3] = {};
  for(int i=1; i<=N; i++) {
    int id;
    for(int j=0; j<3; j++) {
      if(T[j] == S[i]) id = j;
    }
    ordinal[counter[id] + 1][id] = i;
    counter[id]++;
  }
  for(int i=0; i<3; i++) {
    dp[0][0][0][i] = 0;
  }
  for(int i=0; i<=counter[0]; i++) {
    for(int j=0; j<=counter[1]; j++) {
      if(i+j > N) break;
      for(int k=0; k<=min(counter[2], N-i-j); k++) {
        if(i+j+k == 0) continue;
        if(i > 0) {
          int pos = ordinal[i][0];
          if(pos == -1) continue;
          pos += max(0, j - before[pos][1]) + max(0, k - before[pos][2]);
          int ma = min(dp[i-1][j][k][1], dp[i-1][j][k][2]);
          dp[i][j][k][0] = abs(ma + pos - (i+j+k));
        }
        if(j > 0) {
          int pos = ordinal[j][1];
          if(pos == -1) continue;
          pos += max(0, i - before[pos][0]) + max(0, k - before[pos][2]);
          int ma = min(dp[i][j-1][k][0], dp[i][j-1][k][2]);
          dp[i][j][k][1] = abs(ma + pos - (i+j+k));
        }
        if(k > 0) {
          int pos = ordinal[k][2];
          if(pos == -1) continue;
          pos += max(0, i - before[pos][0]) + max(0, j - before[pos][1]);
          int ma = min(dp[i][j][k-1][0], dp[i][j][k-1][1]);
          dp[i][j][k][2] = abs(ma + pos - (i+j+k));
        }
      }
    }
  }
  int ans = 1e8;
  for(int i=0; i<=N; i++) {
    for(int j=0; j<=N; j++) {
      for(int k=0; k<=N; k++) {
        if(i+j+k == N) {
          for(int l=0; l<3; l++) {
            ans = min(ans, dp[i][j][k][l]);
          }
        }
      }
    }
  }
  cout << (ans == 1e8 ? -1 : ans) << "\n";
}

int32_t main(){
  ios::sync_with_stdio(0); cin.tie(0);
  int t = 1; //cin >> t;
  for(int i=1; i<=t; i++) solve(i);
} 

Compilation message

joi2019_ho_t3.cpp: In function 'void solve(int)':
joi2019_ho_t3.cpp:50:23: warning: 'id' may be used uninitialized in this function [-Wmaybe-uninitialized]
   50 |     ordinal[counter[id] + 1][id] = i;
      |             ~~~~~~~~~~^
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 0 ms 332 KB Output is correct
6 Correct 0 ms 332 KB Output is correct
7 Correct 0 ms 332 KB Output is correct
8 Correct 0 ms 332 KB Output is correct
9 Correct 0 ms 332 KB Output is correct
10 Correct 0 ms 332 KB Output is correct
11 Correct 0 ms 332 KB Output is correct
12 Correct 0 ms 332 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 0 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 276 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 0 ms 332 KB Output is correct
6 Correct 0 ms 332 KB Output is correct
7 Correct 0 ms 332 KB Output is correct
8 Correct 0 ms 332 KB Output is correct
9 Correct 0 ms 332 KB Output is correct
10 Correct 0 ms 332 KB Output is correct
11 Correct 0 ms 332 KB Output is correct
12 Correct 0 ms 332 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 0 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 276 KB Output is correct
18 Correct 2 ms 2884 KB Output is correct
19 Correct 3 ms 2892 KB Output is correct
20 Correct 2 ms 2892 KB Output is correct
21 Correct 2 ms 2892 KB Output is correct
22 Correct 2 ms 2892 KB Output is correct
23 Correct 2 ms 2892 KB Output is correct
24 Correct 2 ms 2892 KB Output is correct
25 Correct 2 ms 2892 KB Output is correct
26 Correct 2 ms 2892 KB Output is correct
27 Correct 2 ms 2892 KB Output is correct
28 Correct 2 ms 2892 KB Output is correct
29 Correct 2 ms 2892 KB Output is correct
30 Correct 2 ms 2892 KB Output is correct
31 Correct 2 ms 2892 KB Output is correct
32 Correct 3 ms 2892 KB Output is correct
33 Correct 3 ms 2764 KB Output is correct
34 Correct 2 ms 2764 KB Output is correct
35 Correct 2 ms 2636 KB Output is correct
36 Correct 2 ms 2764 KB Output is correct
37 Correct 2 ms 2508 KB Output is correct
38 Correct 2 ms 2892 KB Output is correct
39 Correct 2 ms 2860 KB Output is correct
40 Correct 2 ms 2764 KB Output is correct
41 Correct 2 ms 2892 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 367 ms 757524 KB Output is correct
3 Correct 396 ms 751664 KB Output is correct
4 Correct 421 ms 757400 KB Output is correct
5 Correct 380 ms 757456 KB Output is correct
6 Correct 410 ms 757408 KB Output is correct
7 Correct 428 ms 751652 KB Output is correct
8 Correct 364 ms 751684 KB Output is correct
9 Correct 394 ms 746144 KB Output is correct
10 Correct 382 ms 757360 KB Output is correct
11 Correct 411 ms 757364 KB Output is correct
12 Correct 55 ms 104332 KB Output is correct
13 Correct 131 ms 244436 KB Output is correct
14 Correct 262 ms 426092 KB Output is correct
15 Correct 374 ms 757396 KB Output is correct
16 Correct 386 ms 757356 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 0 ms 332 KB Output is correct
6 Correct 0 ms 332 KB Output is correct
7 Correct 0 ms 332 KB Output is correct
8 Correct 0 ms 332 KB Output is correct
9 Correct 0 ms 332 KB Output is correct
10 Correct 0 ms 332 KB Output is correct
11 Correct 0 ms 332 KB Output is correct
12 Correct 0 ms 332 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 0 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 276 KB Output is correct
18 Correct 2 ms 2884 KB Output is correct
19 Correct 3 ms 2892 KB Output is correct
20 Correct 2 ms 2892 KB Output is correct
21 Correct 2 ms 2892 KB Output is correct
22 Correct 2 ms 2892 KB Output is correct
23 Correct 2 ms 2892 KB Output is correct
24 Correct 2 ms 2892 KB Output is correct
25 Correct 2 ms 2892 KB Output is correct
26 Correct 2 ms 2892 KB Output is correct
27 Correct 2 ms 2892 KB Output is correct
28 Correct 2 ms 2892 KB Output is correct
29 Correct 2 ms 2892 KB Output is correct
30 Correct 2 ms 2892 KB Output is correct
31 Correct 2 ms 2892 KB Output is correct
32 Correct 3 ms 2892 KB Output is correct
33 Correct 3 ms 2764 KB Output is correct
34 Correct 2 ms 2764 KB Output is correct
35 Correct 2 ms 2636 KB Output is correct
36 Correct 2 ms 2764 KB Output is correct
37 Correct 2 ms 2508 KB Output is correct
38 Correct 2 ms 2892 KB Output is correct
39 Correct 2 ms 2860 KB Output is correct
40 Correct 2 ms 2764 KB Output is correct
41 Correct 2 ms 2892 KB Output is correct
42 Correct 0 ms 204 KB Output is correct
43 Correct 367 ms 757524 KB Output is correct
44 Correct 396 ms 751664 KB Output is correct
45 Correct 421 ms 757400 KB Output is correct
46 Correct 380 ms 757456 KB Output is correct
47 Correct 410 ms 757408 KB Output is correct
48 Correct 428 ms 751652 KB Output is correct
49 Correct 364 ms 751684 KB Output is correct
50 Correct 394 ms 746144 KB Output is correct
51 Correct 382 ms 757360 KB Output is correct
52 Correct 411 ms 757364 KB Output is correct
53 Correct 55 ms 104332 KB Output is correct
54 Correct 131 ms 244436 KB Output is correct
55 Correct 262 ms 426092 KB Output is correct
56 Correct 374 ms 757396 KB Output is correct
57 Correct 386 ms 757356 KB Output is correct
58 Correct 426 ms 757420 KB Output is correct
59 Correct 467 ms 757424 KB Output is correct
60 Correct 387 ms 746260 KB Output is correct
61 Correct 454 ms 751756 KB Output is correct
62 Correct 463 ms 757412 KB Output is correct
63 Correct 413 ms 757508 KB Output is correct
64 Correct 399 ms 757420 KB Output is correct
65 Correct 428 ms 751728 KB Output is correct
66 Correct 407 ms 740556 KB Output is correct
67 Correct 425 ms 757356 KB Output is correct
68 Correct 423 ms 751788 KB Output is correct
69 Correct 412 ms 757468 KB Output is correct
70 Correct 430 ms 757424 KB Output is correct
71 Correct 410 ms 751864 KB Output is correct
72 Correct 455 ms 751760 KB Output is correct
73 Correct 369 ms 757396 KB Output is correct