結果

問題 No.684 Prefix Parenthesis
ユーザー pekempeypekempey
提出日時 2018-05-11 23:28:36
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 793 bytes
コンパイル時間 990 ms
コンパイル使用メモリ 77,480 KB
実行使用メモリ 5,556 KB
最終ジャッジ日時 2023-09-10 18:37:26
合計ジャッジ時間 2,831 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,384 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 4 ms
4,380 KB
testcase_04 AC 8 ms
4,380 KB
testcase_05 AC 9 ms
5,340 KB
testcase_06 AC 4 ms
4,380 KB
testcase_07 AC 9 ms
5,388 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 3 ms
4,380 KB
testcase_10 AC 11 ms
5,272 KB
testcase_11 AC 8 ms
5,340 KB
testcase_12 AC 3 ms
4,380 KB
testcase_13 AC 10 ms
5,312 KB
testcase_14 AC 10 ms
5,360 KB
testcase_15 AC 5 ms
4,380 KB
testcase_16 AC 9 ms
5,268 KB
testcase_17 AC 6 ms
4,380 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 10 ms
5,296 KB
testcase_20 AC 11 ms
5,536 KB
testcase_21 AC 11 ms
5,332 KB
testcase_22 AC 10 ms
5,368 KB
testcase_23 AC 10 ms
5,420 KB
testcase_24 AC 10 ms
5,296 KB
testcase_25 AC 10 ms
5,356 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 1 ms
4,376 KB
testcase_28 AC 2 ms
4,376 KB
testcase_29 AC 11 ms
5,332 KB
testcase_30 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>

using namespace std;

int main() {
  int n;
  string s;
  cin >> n >> s;

  long long l = 0;
  long long r = 0;

  vector<pair<long long, long long>> lr;
  long long ans = 0;
  long long C = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '(') {
      r++;
    } else {
      if (r > 0) {
        r--;
        C++;
      } else {
        l++;
      }
    }
    lr.emplace_back(l, r);
    ans += C * 2;
  }
  sort(lr.begin(), lr.end(), [](auto a, auto b) { return min(-a.first, -a.first + a.second - b.first) > min(-b.first, -b.first + b.second - a.first); });
  r = 0;
  for (auto p : lr) {
    long long take = min(r, p.first);
    r -= take;
    ans += take * 2;
    r += p.second;
  }
  cout << ans << endl;
}
0