結果

問題 No.684 Prefix Parenthesis
ユーザー maspymaspy
提出日時 2020-04-23 21:39:50
言語 Python3
(3.11.6 + numpy 1.26.0 + scipy 1.11.3)
結果
AC  
実行時間 225 ms / 2,000 ms
コード長 811 bytes
コンパイル時間 212 ms
コンパイル使用メモリ 11,028 KB
実行使用メモリ 27,040 KB
最終ジャッジ日時 2023-08-04 03:08:54
合計ジャッジ時間 6,283 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
7,788 KB
testcase_01 AC 16 ms
7,900 KB
testcase_02 AC 15 ms
7,796 KB
testcase_03 AC 72 ms
12,404 KB
testcase_04 AC 154 ms
18,536 KB
testcase_05 AC 170 ms
19,492 KB
testcase_06 AC 72 ms
12,268 KB
testcase_07 AC 162 ms
18,964 KB
testcase_08 AC 34 ms
9,484 KB
testcase_09 AC 39 ms
9,992 KB
testcase_10 AC 224 ms
23,664 KB
testcase_11 AC 154 ms
18,672 KB
testcase_12 AC 41 ms
10,112 KB
testcase_13 AC 225 ms
26,416 KB
testcase_14 AC 225 ms
25,652 KB
testcase_15 AC 87 ms
14,228 KB
testcase_16 AC 198 ms
24,476 KB
testcase_17 AC 122 ms
17,244 KB
testcase_18 AC 19 ms
8,476 KB
testcase_19 AC 223 ms
25,476 KB
testcase_20 AC 225 ms
25,412 KB
testcase_21 AC 223 ms
23,888 KB
testcase_22 AC 207 ms
26,344 KB
testcase_23 AC 213 ms
27,040 KB
testcase_24 AC 217 ms
23,096 KB
testcase_25 AC 224 ms
23,088 KB
testcase_26 AC 16 ms
7,836 KB
testcase_27 AC 16 ms
7,812 KB
testcase_28 AC 15 ms
7,908 KB
testcase_29 AC 221 ms
25,540 KB
testcase_30 AC 220 ms
24,204 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N = int(readline())
S = read().rstrip().decode()

L = '('
R = ')'

dp = [(0, 0)]
total_pair = 0
now_pair = 0
for x in S:
    a, b = dp[-1]
    if x == L:
        dp.append((a, b + 1))
    else:
        if b:
            now_pair += 1
            dp.append((a, b - 1))
        else:
            dp.append((a + 1, 0))
    total_pair += now_pair

dp.sort(key=lambda x: min(x))

left = []
right = []
for a, b in dp:
    if a <= b:
        left.append((a, b))
    else:
        right.append((a, b))

x = 0
y = 0
for a, b in left + right[::-1]:
    p = min(y, a)
    total_pair += p
    y -= p
    a -= p
    if not y:
        x, y = x + a, b
    else:
        x, y = x, y + b

print(total_pair * 2)
0