結果

問題 No.684 Prefix Parenthesis
ユーザー chocorusk
提出日時 2019-07-20 19:37:00
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 14 ms / 2,000 ms
コード長 1,106 bytes
コンパイル時間 1,210 ms
コンパイル使用メモリ 109,288 KB
実行使用メモリ 6,296 KB
最終ジャッジ日時 2024-12-30 03:46:41
合計ジャッジ時間 2,784 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 31
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#define popcount __builtin_popcount
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> P;

int main()
{
    int n; cin>>n;
    string s; cin>>s;
    vector<P> v1, v2;
    ll x=0, y=0, cnt=0;
    ll ans=0;
    for(int i=0; i<n; i++){
        if(s[i]=='(') x++;
        else if(x==0) y++;
        else x--, cnt++;
        if(x>=y) v1.push_back({y, x});
        else v2.push_back({x, y});
        ans+=2*cnt+2*y;
    }
    sort(v1.begin(), v1.end());
    sort(v2.begin(), v2.end(), greater<P>());
    ll m=0, sum=0;
    for(auto p:v1){
        sum-=p.first;
        m=min(m, sum);
        sum+=p.second;
    }
    for(auto p:v2){
        sum-=p.second;
        m=min(m, sum);
        sum+=p.first;
    }
    ans+=2*m;
    cout<<ans<<endl;
    return 0;
}
0