結果

問題 No.684 Prefix Parenthesis
ユーザー chocoruskchocorusk
提出日時 2019-07-20 19:37:00
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 12 ms / 2,000 ms
コード長 1,106 bytes
コンパイル時間 948 ms
コンパイル使用メモリ 106,396 KB
実行使用メモリ 6,172 KB
最終ジャッジ日時 2023-08-28 18:45:03
合計ジャッジ時間 3,168 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,500 KB
testcase_03 AC 4 ms
4,380 KB
testcase_04 AC 8 ms
4,612 KB
testcase_05 AC 9 ms
5,244 KB
testcase_06 AC 5 ms
4,380 KB
testcase_07 AC 8 ms
4,668 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 3 ms
4,380 KB
testcase_10 AC 12 ms
5,556 KB
testcase_11 AC 8 ms
5,320 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 12 ms
5,400 KB
testcase_14 AC 11 ms
5,320 KB
testcase_15 AC 5 ms
4,380 KB
testcase_16 AC 10 ms
5,336 KB
testcase_17 AC 6 ms
4,380 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 12 ms
5,808 KB
testcase_20 AC 11 ms
5,556 KB
testcase_21 AC 11 ms
5,356 KB
testcase_22 AC 10 ms
5,348 KB
testcase_23 AC 9 ms
5,408 KB
testcase_24 AC 8 ms
5,316 KB
testcase_25 AC 9 ms
6,172 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 2 ms
4,380 KB
testcase_29 AC 12 ms
5,756 KB
testcase_30 AC 12 ms
5,512 KB
権限があれば一括ダウンロードができます

ソースコード

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