結果

問題 No.684 Prefix Parenthesis
ユーザー ShadowMas
提出日時 2025-03-13 18:32:35
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 19 ms / 2,000 ms
コード長 856 bytes
コンパイル時間 2,846 ms
コンパイル使用メモリ 203,064 KB
実行使用メモリ 7,324 KB
最終ジャッジ日時 2025-03-13 18:32:40
合計ジャッジ時間 3,932 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 31
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
int main()
{
#ifndef ONLINE_JUDGE
  freopen("data/data1.in", "r", stdin);
  // freopen("data/data1.out", "w", stdout);
#endif
  int n;
  string s;
  cin >> n >> s;

  LL l = 0;
  LL r = 0;

  vector<pair<LL, LL>> lr;
  LL ans = 0;
  LL C = 0;
  for (int i = 0; i < n; i++)
  {
    if (s[i] == '(')
    {
      l++;
    }
    else
    {
      if (l > 0)
      {
        l--;
        C++;
      }
      else
        r++;
    }
    lr.emplace_back(r, l);
    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); });
  l = 0;
  for (auto p : lr)
  {
    LL take = min(l, p.first);
    l -= take;
    ans += take * 2;
    l += p.second;
  }
  cout << ans << endl;
}
0