結果
問題 | No.684 Prefix Parenthesis |
ユーザー |
|
提出日時 | 2018-05-11 23:04:55 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 711 bytes |
コンパイル時間 | 734 ms |
コンパイル使用メモリ | 77,000 KB |
実行使用メモリ | 6,944 KB |
最終ジャッジ日時 | 2024-06-28 08:59:19 |
合計ジャッジ時間 | 1,825 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 20 WA * 11 |
ソースコード
#include <iostream> #include <algorithm> #include <vector> #include <string> using namespace std; int main() { int n; string s; cin >> n >> s; int l = 0; int r = 0; vector<pair<int, int>> 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 a.second - a.first > b.second - b.first; }); r = 0; for (auto p : lr) { int take = min(r, p.first); r -= take; ans += take * 2; r += p.second; } cout << ans << endl; }