結果
問題 | No.684 Prefix Parenthesis |
ユーザー |
![]() |
提出日時 | 2018-05-28 22:42:49 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 3 ms / 2,000 ms |
コード長 | 693 bytes |
コンパイル時間 | 192 ms |
コンパイル使用メモリ | 32,384 KB |
実行使用メモリ | 6,944 KB |
最終ジャッジ日時 | 2024-06-30 07:53:28 |
合計ジャッジ時間 | 1,128 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 31 |
ソースコード
#include <cstdio>int n, d, mn; long long sum, curl, depl, curr, depr, ca[100009], cb[100009]; bool fa[100009], fb[100009]; char s;int main() {scanf("%d\n", &n);for (int i = 0; i < n; ++i) {s = getchar();d += (s == '(' ? 1 : -1);sum += d;mn = (mn < d ? mn : d);if (d >= 0) ca[-mn] += d, fa[-mn] = true;else cb[d - mn] -= d, fb[d - mn] = true;}for (int i = 0; i <= n; ++i) {if (fa[i]) depl = (depl < curl - i ? depl : curl - i);curl += ca[i];}for (int i = 0; i <= n; ++i) {if (fb[i]) depr = (depr < curr - i ? depr : curr - i);curr += cb[i];}printf("%lld\n", 1LL * n * (n + 1) / 2 + (depl < depr + sum ? depl : depr + sum) * 2 - sum);return 0;}