답안 #508412

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
508412 2022-01-13T10:41:08 Z cig32 Growing Vegetable is Fun 3 (JOI19_ho_t3) C++17
60 / 100
500 ms 757400 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<=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;
      |             ~~~~~~~~~~^
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 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 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 0 ms 332 KB Output is correct
13 Correct 0 ms 332 KB Output is correct
14 Correct 0 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 0 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 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 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 0 ms 332 KB Output is correct
13 Correct 0 ms 332 KB Output is correct
14 Correct 0 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 0 ms 332 KB Output is correct
18 Correct 3 ms 2892 KB Output is correct
19 Correct 2 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 3 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 3 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 3 ms 2764 KB Output is correct
34 Correct 3 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 8 ms 2984 KB Output is correct
39 Correct 8 ms 2912 KB Output is correct
40 Correct 2 ms 2768 KB Output is correct
41 Correct 2 ms 2892 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Execution timed out 506 ms 757400 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 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 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 0 ms 332 KB Output is correct
13 Correct 0 ms 332 KB Output is correct
14 Correct 0 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 0 ms 332 KB Output is correct
18 Correct 3 ms 2892 KB Output is correct
19 Correct 2 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 3 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 3 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 3 ms 2764 KB Output is correct
34 Correct 3 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 8 ms 2984 KB Output is correct
39 Correct 8 ms 2912 KB Output is correct
40 Correct 2 ms 2768 KB Output is correct
41 Correct 2 ms 2892 KB Output is correct
42 Correct 0 ms 204 KB Output is correct
43 Execution timed out 506 ms 757400 KB Time limit exceeded
44 Halted 0 ms 0 KB -