結果

問題 No.684 Prefix Parenthesis
ユーザー pekempeypekempey
提出日時 2018-05-11 23:04:55
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 711 bytes
コンパイル時間 829 ms
コンパイル使用メモリ 76,868 KB
実行使用メモリ 4,460 KB
最終ジャッジ日時 2023-09-10 17:47:43
合計ジャッジ時間 2,157 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

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

using namespace std;

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

  int l = 0;
  int r = 0;

  vector<pair<int, int>> 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 a.second - a.first > b.second - b.first; });
  r = 0;
  for (auto p : lr) {
    int take = min(r, p.first);
    r -= take;
    ans += take * 2;
    r += p.second;
  }
  cout << ans << endl;
}
0