結果

問題 No.684 Prefix Parenthesis
ユーザー pekempeypekempey
提出日時 2018-05-13 19:37:51
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 16 ms / 2,000 ms
コード長 979 bytes
コンパイル時間 888 ms
コンパイル使用メモリ 77,828 KB
実行使用メモリ 5,432 KB
最終ジャッジ日時 2023-09-10 19:28:29
合計ジャッジ時間 2,599 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 5 ms
4,376 KB
testcase_04 AC 10 ms
4,376 KB
testcase_05 AC 12 ms
5,412 KB
testcase_06 AC 5 ms
4,376 KB
testcase_07 AC 11 ms
5,180 KB
testcase_08 AC 3 ms
4,380 KB
testcase_09 AC 3 ms
4,380 KB
testcase_10 AC 15 ms
5,424 KB
testcase_11 AC 10 ms
5,232 KB
testcase_12 AC 3 ms
4,376 KB
testcase_13 AC 16 ms
5,288 KB
testcase_14 AC 16 ms
5,296 KB
testcase_15 AC 6 ms
4,376 KB
testcase_16 AC 10 ms
5,252 KB
testcase_17 AC 6 ms
4,380 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 14 ms
5,284 KB
testcase_20 AC 14 ms
5,276 KB
testcase_21 AC 14 ms
5,432 KB
testcase_22 AC 10 ms
5,300 KB
testcase_23 AC 11 ms
5,216 KB
testcase_24 AC 11 ms
5,216 KB
testcase_25 AC 11 ms
5,276 KB
testcase_26 AC 1 ms
4,380 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 1 ms
4,376 KB
testcase_29 AC 14 ms
5,264 KB
testcase_30 AC 15 ms
5,280 KB
権限があれば一括ダウンロードができます

ソースコード

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) {
    long long x = min(-a.first, -a.first + a.second - b.first);
    long long y = min(-b.first, -b.first + b.second - a.first);
    if (x != y) return x > y;
    x = max(-a.first, -a.first + a.second - b.first);
    y = max(-b.first, -b.first + b.second - a.first);
    return x > y;
  });
  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