結果

問題 No.684 Prefix Parenthesis
ユーザー mtsdmtsd
提出日時 2018-05-11 23:00:35
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,734 bytes
コンパイル時間 2,264 ms
コンパイル使用メモリ 87,504 KB
実行使用メモリ 8,300 KB
最終ジャッジ日時 2023-09-10 17:45:47
合計ジャッジ時間 3,761 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 14 ms
7,992 KB
testcase_14 AC 13 ms
7,752 KB
testcase_15 WA -
testcase_16 AC 8 ms
7,536 KB
testcase_17 AC 6 ms
5,680 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 9 ms
8,040 KB
testcase_23 AC 12 ms
8,124 KB
testcase_24 AC 15 ms
8,056 KB
testcase_25 WA -
testcase_26 AC 2 ms
4,380 KB
testcase_27 AC 1 ms
4,380 KB
testcase_28 AC 1 ms
4,380 KB
testcase_29 WA -
testcase_30 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <string>
#include <cmath>
#include <algorithm>
#include <utility>
#include <queue>
#include <set>
#include <map>
#include <iomanip>
#include <cstdio>

using namespace std;
typedef  long long ll;
typedef pair<int,int> PII;
typedef vector<int> VI;
typedef vector<VI> VVI;
#define  MP make_pair
#define  PB push_back
#define inf  1000000007
#define rep(i,n) for(int i=0;i<(int)(n);++i)

template<typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val){
    std::fill( (T*)array, (T*)(array+N), val );
}

int main(){
    int n;
    string s;
    cin >> n >> s;
    vector<ll>a(n),b(n),c(n);
    if(s[0]=='('){
        a[0]++;
    }else{
        b[0]++;
    }
    rep(i,n-1){
        if(s[i+1]=='('){
            a[i+1]=a[i]+1;
            b[i+1]=b[i];
            c[i+1]=c[i];
        }else{
            if(a[i]>0){
                a[i+1] = a[i]-1;
                b[i+1] = b[i];
                c[i+1] = c[i]+1;
            }else{
                b[i+1] = b[i]+1;
                b[i+1] = b[i];
                c[i+1] = c[i];
            }
        }
    }
    vector<pair<ll,pair<ll,ll> > > v(n);
    rep(i,n){
        v[i].first = a[i];
        v[i].second.first = a[i]-b[i];
        v[i].second.second = c[i];
    }
    // rep(i,n){
    //     cout << i << " " <<a[i] << " " << b[i] << " " << c[i] << endl;
    // }
    sort(v.begin(),v.end(),greater<pair<int,pair<int,int> > >());
    ll ans = 0;
    ll tmp = 0;
    rep(i,n){
        v[i].second.first -= v[i].first;
        ans +=v[i].second.second;
        ans +=min(tmp,-v[i].second.first);
        tmp = max((ll)0,tmp+v[i].second.first);
        tmp += v[i].first;
    }
    cout << ans+ans << endl;
    return 0;
}
0