結果

問題 No.684 Prefix Parenthesis
ユーザー Pachicobue
提出日時 2018-05-12 18:47:55
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 1,297 bytes
コンパイル時間 2,218 ms
コンパイル使用メモリ 200,500 KB
最終ジャッジ日時 2025-01-05 10:36:26
ジャッジサーバーID
(参考情報)
judge5 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 16 WA * 15
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define show(x) cerr << #x << " = " << (x) << endl;
using namespace std;
template <typename T, typename A>
inline ostream& operator<<(ostream& os, const vector<T, A>& v)
{
    os << "[";
    for (const auto& p : v) { os << p << ","; }
    return (os << "]" << endl);
}
template <typename S, typename T>
inline ostream& operator<<(ostream& os, const pair<S, T>& p) { return (os << "<" << p.first << "," << p.second << ">"); }

int main()
{
    int N;
    cin >> N;
    string S;
    cin >> S;
    using P = pair<int, int>;
    vector<P> data(N);
    int L = 0, R = 0;
    int depth = 0;
    int ans = 0;
    for (int i = 0; i < N; i++) {
        if (S[i] == ')') {
            if (R == 0) {
                L++;
            } else {
                depth++;
                R--;
            }
        } else {
            R++;
        }
        ans += depth;
        data[i] = {L, R};
    }
    sort(data.begin(), data.end(), [](const P& p1, const P& p2) { return (p1.first != p2.first) ? p1.first < p2.first : p1.second > p2.second; });
    R = 0;
    for (int i = 0; i < N; i++) {
        const int L = data[i].first;
        const int use = min(R, L);
        R -= use;
        ans += use;
        R += data[i].second;
    }
    cout << 2 * ans << endl;
    return 0;
}
0