結果
| 問題 | No.684 Prefix Parenthesis |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2025-03-13 18:52:31 |
| 言語 | C++17 (gcc 15.2.0 + boost 1.90.0) |
| 結果 |
AC
|
| 実行時間 | 10 ms / 2,000 ms |
| コード長 | 819 bytes |
| 記録 | |
| コンパイル時間 | 1,241 ms |
| コンパイル使用メモリ | 218,252 KB |
| 実行使用メモリ | 9,208 KB |
| 最終ジャッジ日時 | 2026-07-07 02:24:39 |
| 合計ジャッジ時間 | 2,937 ms |
|
ジャッジサーバーID (参考情報) |
judge2_1 / judge1_0 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 31 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
int n, t;
LL ans, L, R;
char s[100005];
struct node
{
LL L, R;
bool operator<(const node &t) const
{
return minmax(-R, -R + L - t.R) > minmax(-t.R, -t.R + t.L - R);
// if (R == t.R)
// return t.R - L < R - t.L;
// 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)
{
LL p = min(L, t.R);
L -= p;
L += t.L;
ans += p << 1;
}
cout << ans;
return 0;
}