結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,384 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 5 ms
4,380 KB
testcase_04 AC 11 ms
4,380 KB
testcase_05 AC 13 ms
5,232 KB
testcase_06 AC 5 ms
4,380 KB
testcase_07 AC 11 ms
5,220 KB
testcase_08 AC 3 ms
4,376 KB
testcase_09 AC 3 ms
4,376 KB
testcase_10 AC 17 ms
5,412 KB
testcase_11 AC 11 ms
5,272 KB
testcase_12 AC 3 ms
4,380 KB
testcase_13 AC 18 ms
5,300 KB
testcase_14 AC 19 ms
5,296 KB
testcase_15 AC 7 ms
4,380 KB
testcase_16 AC 12 ms
5,232 KB
testcase_17 AC 7 ms
4,380 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 16 ms
5,248 KB
testcase_20 AC 16 ms
5,244 KB
testcase_21 AC 16 ms
5,288 KB
testcase_22 AC 12 ms
5,192 KB
testcase_23 AC 15 ms
5,240 KB
testcase_24 AC 12 ms
5,292 KB
testcase_25 AC 11 ms
5,300 KB
testcase_26 AC 1 ms
4,380 KB
testcase_27 AC 1 ms
4,380 KB
testcase_28 AC 1 ms
4,376 KB
testcase_29 AC 15 ms
5,200 KB
testcase_30 AC 16 ms
5,256 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) { return minmax(-a.first, -a.first + a.second - b.first) > minmax(-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