Submission #508409

# Submission time Handle Problem Language Result Execution time Memory
508409 2022-01-13T10:38:29 Z cig32 Growing Vegetable is Fun 3 (JOI19_ho_t3) C++17
60 / 100
500 ms 1048580 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] = 1e18;
        }
      }
    }
  }
  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(0ll, j - before[pos][1]) + max(0ll, 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(0ll, i - before[pos][0]) + max(0ll, 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(0ll, i - before[pos][0]) + max(0ll, 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 = 1e18;
  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 == 1e18 ? -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(long long 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 1 ms 204 KB Output is correct
3 Correct 0 ms 312 KB Output is correct
4 Correct 1 ms 308 KB Output is correct
5 Correct 1 ms 304 KB Output is correct
6 Correct 1 ms 312 KB Output is correct
7 Correct 1 ms 316 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 0 ms 304 KB Output is correct
11 Correct 1 ms 312 KB Output is correct
12 Correct 1 ms 312 KB Output is correct
13 Correct 1 ms 316 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 0 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 308 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 0 ms 312 KB Output is correct
4 Correct 1 ms 308 KB Output is correct
5 Correct 1 ms 304 KB Output is correct
6 Correct 1 ms 312 KB Output is correct
7 Correct 1 ms 316 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 0 ms 304 KB Output is correct
11 Correct 1 ms 312 KB Output is correct
12 Correct 1 ms 312 KB Output is correct
13 Correct 1 ms 316 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 0 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 308 KB Output is correct
18 Correct 3 ms 5580 KB Output is correct
19 Correct 3 ms 5564 KB Output is correct
20 Correct 3 ms 5568 KB Output is correct
21 Correct 4 ms 5564 KB Output is correct
22 Correct 3 ms 5564 KB Output is correct
23 Correct 3 ms 5580 KB Output is correct
24 Correct 3 ms 5560 KB Output is correct
25 Correct 3 ms 5580 KB Output is correct
26 Correct 3 ms 5580 KB Output is correct
27 Correct 3 ms 5580 KB Output is correct
28 Correct 3 ms 5580 KB Output is correct
29 Correct 5 ms 5528 KB Output is correct
30 Correct 5 ms 5580 KB Output is correct
31 Correct 3 ms 5564 KB Output is correct
32 Correct 3 ms 5580 KB Output is correct
33 Correct 3 ms 5324 KB Output is correct
34 Correct 3 ms 5296 KB Output is correct
35 Correct 3 ms 5056 KB Output is correct
36 Correct 3 ms 5324 KB Output is correct
37 Correct 3 ms 4812 KB Output is correct
38 Correct 3 ms 5552 KB Output is correct
39 Correct 4 ms 5556 KB Output is correct
40 Correct 3 ms 5312 KB Output is correct
41 Correct 3 ms 5580 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 316 KB Output is correct
2 Execution timed out 519 ms 1048580 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 0 ms 312 KB Output is correct
4 Correct 1 ms 308 KB Output is correct
5 Correct 1 ms 304 KB Output is correct
6 Correct 1 ms 312 KB Output is correct
7 Correct 1 ms 316 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 0 ms 304 KB Output is correct
11 Correct 1 ms 312 KB Output is correct
12 Correct 1 ms 312 KB Output is correct
13 Correct 1 ms 316 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 0 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 308 KB Output is correct
18 Correct 3 ms 5580 KB Output is correct
19 Correct 3 ms 5564 KB Output is correct
20 Correct 3 ms 5568 KB Output is correct
21 Correct 4 ms 5564 KB Output is correct
22 Correct 3 ms 5564 KB Output is correct
23 Correct 3 ms 5580 KB Output is correct
24 Correct 3 ms 5560 KB Output is correct
25 Correct 3 ms 5580 KB Output is correct
26 Correct 3 ms 5580 KB Output is correct
27 Correct 3 ms 5580 KB Output is correct
28 Correct 3 ms 5580 KB Output is correct
29 Correct 5 ms 5528 KB Output is correct
30 Correct 5 ms 5580 KB Output is correct
31 Correct 3 ms 5564 KB Output is correct
32 Correct 3 ms 5580 KB Output is correct
33 Correct 3 ms 5324 KB Output is correct
34 Correct 3 ms 5296 KB Output is correct
35 Correct 3 ms 5056 KB Output is correct
36 Correct 3 ms 5324 KB Output is correct
37 Correct 3 ms 4812 KB Output is correct
38 Correct 3 ms 5552 KB Output is correct
39 Correct 4 ms 5556 KB Output is correct
40 Correct 3 ms 5312 KB Output is correct
41 Correct 3 ms 5580 KB Output is correct
42 Correct 1 ms 316 KB Output is correct
43 Execution timed out 519 ms 1048580 KB Time limit exceeded
44 Halted 0 ms 0 KB -