結果
| 問題 | No.684 Prefix Parenthesis | 
| コンテスト | |
| ユーザー |  夕叢霧香(ゆうむらきりか) | 
| 提出日時 | 2018-05-27 22:49:00 | 
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 17 ms / 2,000 ms | 
| コード長 | 846 bytes | 
| コンパイル時間 | 861 ms | 
| コンパイル使用メモリ | 80,228 KB | 
| 最終ジャッジ日時 | 2025-01-05 10:47:50 | 
| ジャッジサーバーID (参考情報) | judge3 / judge1 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 31 | 
ソースコード
#include<algorithm>
#include<iostream>
#include<vector>
using namespace std;
typedef long long lint;
typedef vector<int>vi;
typedef pair<int,int>pii;
#define rep(i,n)for(int i=0;i<(int)(n);++i)
struct cmp{
  bool operator()(pii a,pii b)const{
    int f=a.first-a.second;
    f=b.first+max(f,-b.second);
    int s=b.first-b.second;
    s=a.first+max(s,-a.second);
    return f==s?-a.second<-b.second:f<s;
  }
};
int main(){
  int n;
  string s;
  cin>>n>>s;
  int mi=0,cur=0;
  vector<pii> kenchon(n);
  rep(i,n){
    cur+=s[i]=='('?1:-1;
    mi=min(mi,cur);
    kenchon[i]=pii(cur,mi);
  }
  sort(kenchon.begin(),kenchon.end(),cmp());
  lint sum=(lint)n*(lint)(n+1)/2;
  lint ans=0;
  lint cursum=0;
  rep(i,n){
    ans=kenchon[i].first+max(ans,(lint)-kenchon[i].second);
    cursum+=kenchon[i].first;
  }
  cout<<sum+(cursum-ans-ans)<<endl;
}
            
            
            
        