結果
| 問題 | No.684 Prefix Parenthesis | 
| コンテスト | |
| ユーザー |  beet | 
| 提出日時 | 2018-05-17 13:33:06 | 
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 13 ms / 2,000 ms | 
| コード長 | 891 bytes | 
| コンパイル時間 | 1,805 ms | 
| コンパイル使用メモリ | 174,104 KB | 
| 実行使用メモリ | 5,740 KB | 
| 最終ジャッジ日時 | 2024-06-28 13:28:05 | 
| 合計ジャッジ時間 | 2,995 ms | 
| ジャッジサーバーID (参考情報) | judge5 / judge1 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 31 | 
ソースコード
#include<bits/stdc++.h>
using namespace std;
using Int = long long;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}
//INSERT ABOVE HERE
signed main(){
  Int n;
  string s;
  cin>>n>>s;
  
  using P = pair<Int, Int>;
  vector<P> v;
  Int a=0,b=0,c=0,ans=0;
  for(Int i=0;i<n;i++){
    if(s[i]=='(') a++;
    else if(a) a--,c++;
    else b++;
    ans+=c;
    v.emplace_back(b,a);
  }
  auto comp=[&](P a,P b){
    if((a.first<=a.second)^(b.first<=b.second))
      return (a.first<=a.second);
    if(a.first<=a.second) return a.first<b.first;
    return a.second>b.second;
  };
  
  Int res=0;
  sort(v.begin(),v.end(),comp);
  for(Int i=0;i<n;i++){
    ans+=min(res,v[i].first);
    res-=min(res,v[i].first);
    res+=v[i].second;
  }
  
  ans<<=1;
  cout<<ans<<endl;
  return 0;
}
            
            
            
        