結果
| 問題 |
No.684 Prefix Parenthesis
|
| コンテスト | |
| ユーザー |
mtsd
|
| 提出日時 | 2018-05-11 23:44:03 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
RE
|
| 実行時間 | - |
| コード長 | 1,869 bytes |
| コンパイル時間 | 777 ms |
| コンパイル使用メモリ | 91,100 KB |
| 実行使用メモリ | 8,960 KB |
| 最終ジャッジ日時 | 2024-06-28 09:04:24 |
| 合計ジャッジ時間 | 5,583 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 6 RE * 25 |
コンパイルメッセージ
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]
40 | }
| ^
ソースコード
#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;
}
mtsd