Submission #508410

# Submission time Handle Problem Language Result Execution time Memory
508410 2022-01-13T10:39:22 Z cig32 Growing Vegetable is Fun 3 (JOI19_ho_t3) C++17
60 / 100
500 ms 757516 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<=N; i++) {
    for(int j=0; j<=N; j++) {
      if(i+j > N) continue;
      for(int k=0; k<=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 1 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 0 ms 332 KB Output is correct
5 Correct 0 ms 316 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 1 ms 332 KB Output is correct
10 Correct 0 ms 332 KB Output is correct
11 Correct 0 ms 316 KB Output is correct
12 Correct 0 ms 332 KB Output is correct
13 Correct 0 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 0 ms 332 KB Output is correct
16 Correct 0 ms 332 KB Output is correct
17 Correct 0 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 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 0 ms 332 KB Output is correct
5 Correct 0 ms 316 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 1 ms 332 KB Output is correct
10 Correct 0 ms 332 KB Output is correct
11 Correct 0 ms 316 KB Output is correct
12 Correct 0 ms 332 KB Output is correct
13 Correct 0 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 0 ms 332 KB Output is correct
16 Correct 0 ms 332 KB Output is correct
17 Correct 0 ms 332 KB Output is correct
18 Correct 2 ms 2892 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 3 ms 2892 KB Output is correct
24 Correct 2 ms 2876 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 2 ms 2892 KB Output is correct
33 Correct 2 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 3 ms 2744 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 2892 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 320 KB Output is correct
2 Correct 462 ms 757516 KB Output is correct
3 Correct 468 ms 751760 KB Output is correct
4 Execution timed out 562 ms 757324 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 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 0 ms 332 KB Output is correct
5 Correct 0 ms 316 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 1 ms 332 KB Output is correct
10 Correct 0 ms 332 KB Output is correct
11 Correct 0 ms 316 KB Output is correct
12 Correct 0 ms 332 KB Output is correct
13 Correct 0 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 0 ms 332 KB Output is correct
16 Correct 0 ms 332 KB Output is correct
17 Correct 0 ms 332 KB Output is correct
18 Correct 2 ms 2892 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 3 ms 2892 KB Output is correct
24 Correct 2 ms 2876 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 2 ms 2892 KB Output is correct
33 Correct 2 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 3 ms 2744 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 2892 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 320 KB Output is correct
43 Correct 462 ms 757516 KB Output is correct
44 Correct 468 ms 751760 KB Output is correct
45 Execution timed out 562 ms 757324 KB Time limit exceeded
46 Halted 0 ms 0 KB -