結果

問題 No.684 Prefix Parenthesis
ユーザー PulmnPulmn
提出日時 2018-05-04 20:40:25
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 12 ms / 2,000 ms
コード長 625 bytes
コンパイル時間 1,447 ms
コンパイル使用メモリ 152,044 KB
実行使用メモリ 6,136 KB
最終ジャッジ日時 2023-09-10 18:36:15
合計ジャッジ時間 2,766 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 4 ms
4,380 KB
testcase_04 AC 8 ms
4,692 KB
testcase_05 AC 9 ms
5,248 KB
testcase_06 AC 4 ms
4,380 KB
testcase_07 AC 7 ms
4,660 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,384 KB
testcase_10 AC 11 ms
5,528 KB
testcase_11 AC 8 ms
5,272 KB
testcase_12 AC 3 ms
4,380 KB
testcase_13 AC 12 ms
5,276 KB
testcase_14 AC 11 ms
5,268 KB
testcase_15 AC 5 ms
4,380 KB
testcase_16 AC 9 ms
5,244 KB
testcase_17 AC 5 ms
4,388 KB
testcase_18 AC 2 ms
4,388 KB
testcase_19 AC 11 ms
5,832 KB
testcase_20 AC 11 ms
5,604 KB
testcase_21 AC 11 ms
5,292 KB
testcase_22 AC 10 ms
5,336 KB
testcase_23 AC 8 ms
5,272 KB
testcase_24 AC 8 ms
5,296 KB
testcase_25 AC 8 ms
6,136 KB
testcase_26 AC 2 ms
4,380 KB
testcase_27 AC 2 ms
4,384 KB
testcase_28 AC 1 ms
4,380 KB
testcase_29 AC 11 ms
5,768 KB
testcase_30 AC 11 ms
5,700 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

ll n;
string s;
vector<pair<ll,ll> > a,b;

int main(){
	cin>>n>>s;
	assert(1<=n&&n<=100000);
	assert(s.size()==n);
	ll t=0,m=0,S=0;
	for(int i=0;i<n;i++){
		ll d=(s[i]=='('?1:-1);
		t+=d;
		S+=d*(n-i);
		m=min(m,t);
		if(t>=0) a.push_back({-m,t});
		else b .push_back({m-t,t});
	}
	sort(a.begin(),a.end());
	sort(b.begin(),b.end());
	t=0,m=0;
	for(int i=0;i<a.size();i++){
		ll A=a[i].first,B=a[i].second;
		m=min(m,t-A);
		t+=B;
	}
	for(int i=0;i<b.size();i++){
		ll A=b[i].first,B=b[i].second;
		m=min(m,t+A+B);
		t+=B;
	}
	cout<<n*(n+1)/2+2*m-t<<endl;
}
0