結果
問題 | No.684 Prefix Parenthesis |
ユーザー |
![]() |
提出日時 | 2020-04-23 21:39:50 |
言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
結果 |
AC
|
実行時間 | 275 ms / 2,000 ms |
コード長 | 811 bytes |
コンパイル時間 | 231 ms |
コンパイル使用メモリ | 12,672 KB |
実行使用メモリ | 29,420 KB |
最終ジャッジ日時 | 2024-10-14 00:12:58 |
合計ジャッジ時間 | 6,663 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 31 |
ソースコード
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)