結果

問題 No.684 Prefix Parenthesis
ユーザー mtsdmtsd
提出日時 2018-05-11 23:44:49
言語 C++11
(gcc 11.4.0)
結果
RE  
実行時間 -
コード長 1,865 bytes
コンパイル時間 897 ms
コンパイル使用メモリ 84,504 KB
実行使用メモリ 7,560 KB
最終ジャッジ日時 2023-09-10 17:53:48
合計ジャッジ時間 6,055 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 AC 2 ms
4,380 KB
testcase_27 AC 1 ms
4,380 KB
testcase_28 AC 2 ms
4,380 KB
testcase_29 RE -
testcase_30 RE -
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘bool comp(std::pair<long long int, long long int>, std::pair<long long int, long long int>)’:
main.cpp:40:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
 ^

ソースコード

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 );
}
bool comp(pair<ll,ll>p,pair<ll,ll>q){
    if(p.first>=q.second&&q.first>=p.second){
        return p.second<=q.second;
    }
    if(p.first<q.second&&q.first>=p.second){
        return p.second<=p.first;
    }
    if(p.first>=q.second&&q.first<p.second){
        return q.first<=q.second;
    }
    if(p.first<q.second&&q.first<p.second){
        return q.first<=p.first;
    }
}

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,ll>  > v(n);
    ll ans = 0;
    rep(i,n){
        v[i].first = a[i];
        v[i].second = b[i];
        ans += c[i];
    }
    
    sort(v.begin(),v.end(),comp);
    ll tmp = 0;
    rep(i,n){
        ans +=min(tmp,v[i].second);
        tmp = max((ll)0,tmp-v[i].second);
        tmp += v[i].first;
    }
    cout << ans+ans << endl;
    return 0;
}
0