結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,384 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,384 KB
testcase_03 AC 4 ms
4,380 KB
testcase_04 AC 8 ms
4,688 KB
testcase_05 AC 9 ms
5,344 KB
testcase_06 AC 4 ms
4,376 KB
testcase_07 AC 8 ms
4,616 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 3 ms
4,380 KB
testcase_10 AC 12 ms
5,444 KB
testcase_11 AC 8 ms
5,260 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 12 ms
5,560 KB
testcase_14 AC 11 ms
5,300 KB
testcase_15 AC 5 ms
4,448 KB
testcase_16 AC 9 ms
5,244 KB
testcase_17 AC 5 ms
4,380 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 12 ms
5,816 KB
testcase_20 AC 11 ms
5,612 KB
testcase_21 AC 12 ms
5,368 KB
testcase_22 AC 10 ms
5,372 KB
testcase_23 AC 8 ms
5,376 KB
testcase_24 AC 9 ms
5,356 KB
testcase_25 AC 9 ms
6,196 KB
testcase_26 AC 1 ms
4,380 KB
testcase_27 AC 1 ms
4,376 KB
testcase_28 AC 2 ms
4,376 KB
testcase_29 AC 12 ms
5,764 KB
testcase_30 AC 12 ms
5,576 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++){
	    assert(s[i]=='('||s[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