답안 #224523

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
224523 2020-04-18T09:55:34 Z rama_pang Security Gate (JOI18_security_gate) C++14
100 / 100
1485 ms 665720 KB
#include <bits/stdc++.h>
using namespace std;

const int MOD = 1e9 + 7;
const int MAXN = 305;

void RADD(int &a, int b) { if ((a += b) >= MOD) a -= MOD; }
int MUL(int a, int b) { return 1ll * a * b % MOD; }

int n;
string a;

int dp_valid[MAXN][MAXN];
int dp_l[MAXN / 2][MAXN][MAXN][2]; // dp[A][pos][bal][done?], A: max height from left without going negitive
int dp_r_one[MAXN][MAXN][MAXN][2]; // dp[A + B/2][pos][bal][fixed?], B: final height
int dp_r_both[MAXN / 2][MAXN][2 * MAXN][3]; // dp[A][pos][bal + n][init/foundA/neg], A: max height from right without going negative

void calc_dp_valid() {
  memset(dp_valid, 0, sizeof(dp_valid));

  auto dp = dp_valid;
  dp[n][0] = 1;

  for (int i = n - 1; i >= 0; i--) {
    for (int j = 0; j <= n; j++) {
      if (a[i] != ')' && j < n) {
        RADD(dp[i][j], dp[i + 1][j + 1]);
      }
      if (a[i] != '(' && j > 0) {
        RADD(dp[i][j], dp[i + 1][j - 1]);
      }
    }
  }
}

void calc_dp_l() {
  memset(dp_l, 0, sizeof(dp_l));
  
  for (int A = 0; A <= n / 2; A++) {
    auto dp = dp_l[A];
    dp[0][0][!A] = 1;

    for (int i = 0; i < n; i++) { // pos
      for (int j = 0; j <= A; j++) { // bal
        for (int k = 0; k < 2; k++) { // done?
          if (a[i] != ')' && j < A) {
            RADD(dp[i + 1][j + 1][k || j + 1 == A], dp[i][j][k]);
          }
          if (a[i] != '(' && j > 0) {
            RADD(dp[i + 1][j - 1][k], dp[i][j][k]);
          }
        }
      }
    }
  }
}

void calc_dp_r_one() {
  memset(dp_r_one, 0, sizeof(dp_r_one));
  
  for (int M = 0; M <= n; M++) { // M = A + B/2
    auto dp = dp_r_one[M];
    dp[n][0][!M] = 1;

    for (int i = n - 1; i >= 0; i--) { // pos
      // not yet fixed M transition
      for (int j = 0; j <= n; j++) { // bal
        if (a[i] != ')' && j < n) {
          RADD(dp[i][j][0], dp[i + 1][j + 1][0]);
        }
        if (a[i] != '(' && j > 0) {
          RADD(dp[i][j][0], dp[i + 1][j - 1][0]);
        }
      }

      // unfixed -> fixed M transition
      if (a[i] != ')' && M < n) {
        RADD(dp[i][M][1], dp[i + 1][M + 1][0]);
      }
      if (a[i] != '(' && M > 0) {
        RADD(dp[i][M][1], dp[i + 1][M - 1][0]);
      }

      // fixed M transition, balance mus stay strictly above M
      for (int j = M + 1; j <= min(2 * M, n - 1); j++) { // X <= 2A + B = 2M, so X <= 2M
        if (a[i] != ')' && j < 2 * M) {
          RADD(dp[i][j][1], dp[i + 1][j + 1][1]);
        } 
        if (a[i] != '(' && j > 0){
          RADD(dp[i][j][1], dp[i + 1][j - 1][1]);
        }
      }
    }
  }
}

void calc_dp_r_both(int not_include_zero) {
  memset(dp_r_both, 0, sizeof(dp_r_both));
  
  for (int A = 0; A <= n / 2; A++) { // maximum height viewed from the right
    auto dp = dp_r_both[A];
    dp[n][n][!(A + not_include_zero)] = 1;

    for (int i = n - 1; i >= 0; i--) { // pos
      // haven't seen A
      for (int j = 0; j <= n; j++) { // bal
        if (a[i] != '(' && j < n) {
          RADD(dp[i][j + 1 + n][j + 1 == A + not_include_zero], dp[i + 1][j + n][0]);
        }
        if (a[i] != ')' && j > 0) {
          RADD(dp[i][j - 1 + n][0], dp[i + 1][j + n][0]);
        }
      }

      // seen A
      for (int j = 0; j <= n; j++) {
        if (a[i] != '(') {
          RADD(dp[i][j + 1 + n][1], dp[i + 1][j + n][1]);
        }
        if (a[i] != ')') {
          RADD(dp[i][j - 1 + n][1 + !j], dp[i + 1][j + n][1]);
        }
      }

      // gone negative
      for (int j = - n; j <= min(2 * A, n - 1); j++) {
        if (a[i] != '(' && j < 2 * A) {
          RADD(dp[i][j + 1 + n][2], dp[i + 1][j + n][2]);
        }
        if (a[i] != ')' && j > - n) {
          RADD(dp[i][j - 1 + n][2], dp[i + 1][j + n][2]);
        }
      }
    }
  }
}

int invalid_one() {
  int res = 0;
  for (int A = 0; A <= n / 2; A++) { // maximum before going negative from the left
    for (int B = 2; B <= n; B += 2) { // B = final height on the right
      for (int pos = 0; pos < n; pos += 2) {
        if (a[pos] == '(') continue;
        int val = A >= B / 2 ? dp_valid[pos + 1][B - 1] : dp_r_one[A + B / 2][pos + 1][B - 1][1];
        RADD(res, MUL(dp_l[A][pos][0][1], val));
      }
    }
  }
  return res;
}

int invalid_both() {
  int res = 0;
  for (int A = 0; A <= n / 2; A++) {
    for (int B = - n + 2; B < n; B += 2) {
      if (A + B / 2 < 0 || A + abs(B / 2) >= n / 2) continue;
      for (int pos = 0; pos < n; pos += 2) {
        if (a[pos] == '(') continue;
        RADD(res, MUL(dp_l[A][pos][0][1], dp_r_both[A + B / 2][pos + 1][B - 1 + n][2]));
      }
    }
  }
  return res;
}


int Solve() {
  if (n & 1) return 0;
  
  calc_dp_valid();
  calc_dp_l();
  calc_dp_r_one();
  calc_dp_r_both(0);

  int res = dp_valid[0][0];

  RADD(res, invalid_one());
  RADD(res, invalid_both());

  reverse(begin(a), end(a));
  for (int i = 0; i < n; i++) {
    if (a[i] == '(') {
      a[i] = ')';
    } else if (a[i] == ')') {
      a[i] = '(';
    }
  }

  calc_dp_valid();
  calc_dp_l();
  calc_dp_r_one();
  calc_dp_r_both(1);

  RADD(res, invalid_one());
  RADD(res, invalid_both());

  return res;
}

int main() {
  cin >> n >> a;
  cout << Solve() << "\n";
  return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 394 ms 665464 KB Output is correct
2 Correct 419 ms 665592 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 434 ms 665592 KB Output is correct
5 Correct 416 ms 665592 KB Output is correct
6 Correct 459 ms 665592 KB Output is correct
7 Correct 419 ms 665592 KB Output is correct
8 Correct 497 ms 665592 KB Output is correct
9 Correct 420 ms 665572 KB Output is correct
10 Correct 415 ms 665592 KB Output is correct
11 Correct 446 ms 665532 KB Output is correct
12 Correct 402 ms 665536 KB Output is correct
13 Correct 445 ms 665568 KB Output is correct
14 Correct 426 ms 665592 KB Output is correct
15 Correct 480 ms 665464 KB Output is correct
16 Correct 412 ms 665468 KB Output is correct
17 Correct 409 ms 665592 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 394 ms 665464 KB Output is correct
2 Correct 419 ms 665592 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 434 ms 665592 KB Output is correct
5 Correct 416 ms 665592 KB Output is correct
6 Correct 459 ms 665592 KB Output is correct
7 Correct 419 ms 665592 KB Output is correct
8 Correct 497 ms 665592 KB Output is correct
9 Correct 420 ms 665572 KB Output is correct
10 Correct 415 ms 665592 KB Output is correct
11 Correct 446 ms 665532 KB Output is correct
12 Correct 402 ms 665536 KB Output is correct
13 Correct 445 ms 665568 KB Output is correct
14 Correct 426 ms 665592 KB Output is correct
15 Correct 480 ms 665464 KB Output is correct
16 Correct 412 ms 665468 KB Output is correct
17 Correct 409 ms 665592 KB Output is correct
18 Correct 399 ms 665464 KB Output is correct
19 Correct 413 ms 665616 KB Output is correct
20 Correct 378 ms 665464 KB Output is correct
21 Correct 416 ms 665668 KB Output is correct
22 Correct 425 ms 665464 KB Output is correct
23 Correct 433 ms 665592 KB Output is correct
24 Correct 481 ms 665464 KB Output is correct
25 Correct 420 ms 665720 KB Output is correct
26 Correct 453 ms 665464 KB Output is correct
27 Correct 449 ms 665464 KB Output is correct
28 Correct 400 ms 665720 KB Output is correct
29 Correct 479 ms 665464 KB Output is correct
30 Correct 416 ms 665628 KB Output is correct
31 Correct 480 ms 665464 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 394 ms 665464 KB Output is correct
2 Correct 419 ms 665592 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 434 ms 665592 KB Output is correct
5 Correct 416 ms 665592 KB Output is correct
6 Correct 459 ms 665592 KB Output is correct
7 Correct 419 ms 665592 KB Output is correct
8 Correct 497 ms 665592 KB Output is correct
9 Correct 420 ms 665572 KB Output is correct
10 Correct 415 ms 665592 KB Output is correct
11 Correct 446 ms 665532 KB Output is correct
12 Correct 402 ms 665536 KB Output is correct
13 Correct 445 ms 665568 KB Output is correct
14 Correct 426 ms 665592 KB Output is correct
15 Correct 480 ms 665464 KB Output is correct
16 Correct 412 ms 665468 KB Output is correct
17 Correct 409 ms 665592 KB Output is correct
18 Correct 399 ms 665464 KB Output is correct
19 Correct 413 ms 665616 KB Output is correct
20 Correct 378 ms 665464 KB Output is correct
21 Correct 416 ms 665668 KB Output is correct
22 Correct 425 ms 665464 KB Output is correct
23 Correct 433 ms 665592 KB Output is correct
24 Correct 481 ms 665464 KB Output is correct
25 Correct 420 ms 665720 KB Output is correct
26 Correct 453 ms 665464 KB Output is correct
27 Correct 449 ms 665464 KB Output is correct
28 Correct 400 ms 665720 KB Output is correct
29 Correct 479 ms 665464 KB Output is correct
30 Correct 416 ms 665628 KB Output is correct
31 Correct 480 ms 665464 KB Output is correct
32 Correct 425 ms 665552 KB Output is correct
33 Correct 444 ms 665592 KB Output is correct
34 Correct 451 ms 665464 KB Output is correct
35 Correct 409 ms 665700 KB Output is correct
36 Correct 477 ms 665592 KB Output is correct
37 Correct 440 ms 665720 KB Output is correct
38 Correct 423 ms 665464 KB Output is correct
39 Correct 394 ms 665556 KB Output is correct
40 Correct 401 ms 665464 KB Output is correct
41 Correct 462 ms 665464 KB Output is correct
42 Correct 416 ms 665464 KB Output is correct
43 Correct 426 ms 665556 KB Output is correct
44 Correct 425 ms 665464 KB Output is correct
45 Correct 414 ms 665464 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 394 ms 665464 KB Output is correct
2 Correct 419 ms 665592 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 434 ms 665592 KB Output is correct
5 Correct 416 ms 665592 KB Output is correct
6 Correct 459 ms 665592 KB Output is correct
7 Correct 419 ms 665592 KB Output is correct
8 Correct 497 ms 665592 KB Output is correct
9 Correct 420 ms 665572 KB Output is correct
10 Correct 415 ms 665592 KB Output is correct
11 Correct 446 ms 665532 KB Output is correct
12 Correct 402 ms 665536 KB Output is correct
13 Correct 445 ms 665568 KB Output is correct
14 Correct 426 ms 665592 KB Output is correct
15 Correct 480 ms 665464 KB Output is correct
16 Correct 412 ms 665468 KB Output is correct
17 Correct 409 ms 665592 KB Output is correct
18 Correct 399 ms 665464 KB Output is correct
19 Correct 413 ms 665616 KB Output is correct
20 Correct 378 ms 665464 KB Output is correct
21 Correct 416 ms 665668 KB Output is correct
22 Correct 425 ms 665464 KB Output is correct
23 Correct 433 ms 665592 KB Output is correct
24 Correct 481 ms 665464 KB Output is correct
25 Correct 420 ms 665720 KB Output is correct
26 Correct 453 ms 665464 KB Output is correct
27 Correct 449 ms 665464 KB Output is correct
28 Correct 400 ms 665720 KB Output is correct
29 Correct 479 ms 665464 KB Output is correct
30 Correct 416 ms 665628 KB Output is correct
31 Correct 480 ms 665464 KB Output is correct
32 Correct 425 ms 665552 KB Output is correct
33 Correct 444 ms 665592 KB Output is correct
34 Correct 451 ms 665464 KB Output is correct
35 Correct 409 ms 665700 KB Output is correct
36 Correct 477 ms 665592 KB Output is correct
37 Correct 440 ms 665720 KB Output is correct
38 Correct 423 ms 665464 KB Output is correct
39 Correct 394 ms 665556 KB Output is correct
40 Correct 401 ms 665464 KB Output is correct
41 Correct 462 ms 665464 KB Output is correct
42 Correct 416 ms 665464 KB Output is correct
43 Correct 426 ms 665556 KB Output is correct
44 Correct 425 ms 665464 KB Output is correct
45 Correct 414 ms 665464 KB Output is correct
46 Correct 409 ms 665700 KB Output is correct
47 Correct 428 ms 665464 KB Output is correct
48 Correct 5 ms 256 KB Output is correct
49 Correct 429 ms 665616 KB Output is correct
50 Correct 451 ms 665464 KB Output is correct
51 Correct 438 ms 665636 KB Output is correct
52 Correct 411 ms 665596 KB Output is correct
53 Correct 464 ms 665592 KB Output is correct
54 Correct 489 ms 665464 KB Output is correct
55 Correct 432 ms 665720 KB Output is correct
56 Correct 449 ms 665464 KB Output is correct
57 Correct 384 ms 665464 KB Output is correct
58 Correct 398 ms 665552 KB Output is correct
59 Correct 443 ms 665596 KB Output is correct
60 Correct 445 ms 665592 KB Output is correct
61 Correct 429 ms 665592 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 394 ms 665464 KB Output is correct
2 Correct 419 ms 665592 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 434 ms 665592 KB Output is correct
5 Correct 416 ms 665592 KB Output is correct
6 Correct 459 ms 665592 KB Output is correct
7 Correct 419 ms 665592 KB Output is correct
8 Correct 497 ms 665592 KB Output is correct
9 Correct 420 ms 665572 KB Output is correct
10 Correct 415 ms 665592 KB Output is correct
11 Correct 446 ms 665532 KB Output is correct
12 Correct 402 ms 665536 KB Output is correct
13 Correct 445 ms 665568 KB Output is correct
14 Correct 426 ms 665592 KB Output is correct
15 Correct 480 ms 665464 KB Output is correct
16 Correct 412 ms 665468 KB Output is correct
17 Correct 409 ms 665592 KB Output is correct
18 Correct 399 ms 665464 KB Output is correct
19 Correct 413 ms 665616 KB Output is correct
20 Correct 378 ms 665464 KB Output is correct
21 Correct 416 ms 665668 KB Output is correct
22 Correct 425 ms 665464 KB Output is correct
23 Correct 433 ms 665592 KB Output is correct
24 Correct 481 ms 665464 KB Output is correct
25 Correct 420 ms 665720 KB Output is correct
26 Correct 453 ms 665464 KB Output is correct
27 Correct 449 ms 665464 KB Output is correct
28 Correct 400 ms 665720 KB Output is correct
29 Correct 479 ms 665464 KB Output is correct
30 Correct 416 ms 665628 KB Output is correct
31 Correct 480 ms 665464 KB Output is correct
32 Correct 425 ms 665552 KB Output is correct
33 Correct 444 ms 665592 KB Output is correct
34 Correct 451 ms 665464 KB Output is correct
35 Correct 409 ms 665700 KB Output is correct
36 Correct 477 ms 665592 KB Output is correct
37 Correct 440 ms 665720 KB Output is correct
38 Correct 423 ms 665464 KB Output is correct
39 Correct 394 ms 665556 KB Output is correct
40 Correct 401 ms 665464 KB Output is correct
41 Correct 462 ms 665464 KB Output is correct
42 Correct 416 ms 665464 KB Output is correct
43 Correct 426 ms 665556 KB Output is correct
44 Correct 425 ms 665464 KB Output is correct
45 Correct 414 ms 665464 KB Output is correct
46 Correct 409 ms 665700 KB Output is correct
47 Correct 428 ms 665464 KB Output is correct
48 Correct 5 ms 256 KB Output is correct
49 Correct 429 ms 665616 KB Output is correct
50 Correct 451 ms 665464 KB Output is correct
51 Correct 438 ms 665636 KB Output is correct
52 Correct 411 ms 665596 KB Output is correct
53 Correct 464 ms 665592 KB Output is correct
54 Correct 489 ms 665464 KB Output is correct
55 Correct 432 ms 665720 KB Output is correct
56 Correct 449 ms 665464 KB Output is correct
57 Correct 384 ms 665464 KB Output is correct
58 Correct 398 ms 665552 KB Output is correct
59 Correct 443 ms 665596 KB Output is correct
60 Correct 445 ms 665592 KB Output is correct
61 Correct 429 ms 665592 KB Output is correct
62 Correct 880 ms 665696 KB Output is correct
63 Correct 874 ms 665592 KB Output is correct
64 Correct 631 ms 665468 KB Output is correct
65 Correct 488 ms 665464 KB Output is correct
66 Correct 1459 ms 665464 KB Output is correct
67 Correct 1304 ms 665720 KB Output is correct
68 Correct 1462 ms 665512 KB Output is correct
69 Correct 526 ms 665700 KB Output is correct
70 Correct 616 ms 665568 KB Output is correct
71 Correct 458 ms 665464 KB Output is correct
72 Correct 565 ms 665592 KB Output is correct
73 Correct 1485 ms 665464 KB Output is correct
74 Correct 1132 ms 665592 KB Output is correct