結果

問題 No.684 Prefix Parenthesis
コンテスト
ユーザー ShadowMas
提出日時 2025-03-13 18:42:56
言語 C++17
(gcc 15.2.0 + boost 1.90.0)
コンパイル:
g++-15 -O2 -lm -std=c++17 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
WA  
実行時間 -
コード長 745 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,203 ms
コンパイル使用メモリ 216,592 KB
実行使用メモリ 7,216 KB
最終ジャッジ日時 2026-07-07 02:24:24
合計ジャッジ時間 2,834 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 21 WA * 10
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
int n, t, L, R;
LL ans;
char s[100005];
struct node
{
  int L, R;
  bool operator<(const node &t) const
  {
    if (R == t.R)
      return L + t.R > t.L + R;
    return R < t.R;
  }
};
vector<node> p;

int main()
{
#ifndef ONLINE_JUDGE
  freopen("data/data1.in", "r", stdin);
  // freopen("data/data1.out", "w", stdout);
#endif
  scanf("%d%s", &n, s);
  for (int i = 0, t = 0; s[i]; i++)
  {
    if (s[i] == '(')
      L++;
    else
      L > 0 ? t++, L-- : R++;

    ans += t << 1;
    p.push_back({L, R});
  }
  sort(p.begin(), p.end());
  L = 0;
  for (auto &&t : p)
  {
    int p = min(L, t.R);
    L -= p;
    L += t.L;
    ans += p << 1;
  }
  cout << ans;
  return 0;
}
0