結果

問題 No.684 Prefix Parenthesis
ユーザー WA_TLEWA_TLE
提出日時 2018-05-02 18:53:35
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 11 ms / 2,000 ms
コード長 1,897 bytes
コンパイル時間 1,270 ms
コンパイル使用メモリ 136,084 KB
実行使用メモリ 6,084 KB
最終ジャッジ日時 2023-09-10 18:35:45
合計ジャッジ時間 2,661 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 4 ms
4,376 KB
testcase_04 AC 7 ms
4,676 KB
testcase_05 AC 8 ms
5,348 KB
testcase_06 AC 4 ms
4,376 KB
testcase_07 AC 8 ms
4,640 KB
testcase_08 AC 2 ms
4,384 KB
testcase_09 AC 3 ms
4,380 KB
testcase_10 AC 10 ms
5,748 KB
testcase_11 AC 8 ms
5,380 KB
testcase_12 AC 2 ms
4,384 KB
testcase_13 AC 8 ms
5,388 KB
testcase_14 AC 8 ms
5,376 KB
testcase_15 AC 4 ms
4,376 KB
testcase_16 AC 9 ms
5,556 KB
testcase_17 AC 6 ms
4,376 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 11 ms
5,848 KB
testcase_20 AC 11 ms
5,516 KB
testcase_21 AC 10 ms
5,324 KB
testcase_22 AC 9 ms
5,252 KB
testcase_23 AC 8 ms
5,324 KB
testcase_24 AC 9 ms
5,344 KB
testcase_25 AC 9 ms
6,084 KB
testcase_26 AC 1 ms
4,380 KB
testcase_27 AC 1 ms
4,376 KB
testcase_28 AC 1 ms
4,380 KB
testcase_29 AC 11 ms
5,852 KB
testcase_30 AC 10 ms
5,504 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<deque>
#include<queue>
#include<vector>
#include<algorithm>
#include<iostream>
#include<set>
#include<cmath>
#include<tuple>
#include<string>
#include<chrono>
#include<functional>
#include<iterator>
#include<random>
#include<unordered_set>
#include<array>
#include<map>
#include<iomanip>
using namespace std;
typedef long long int llint;
typedef long double lldo;
#define mp make_pair
#define mt make_tuple
#define pub push_back
#define puf push_front
#define pob pop_back
#define pof pop_front
#define fir first
#define sec second
#define res resize
#define ins insert
#define era erase
//cout<<setprecision(20)
const llint mod=1000000007;
const llint big=2.19e15+1;
const long double pai=3.141592653589793238462643383279502884197;
const long double eps=1e-15;
template <class T,class U>void mineq(T& a,U b){if(a>b){a=b;}}
template <class T,class U>void maxeq(T& a,U b){if(a<b){a=b;}}
llint gcd(llint a,llint b){if(a%b==0){return b;}else return gcd(b,a%b);}
llint lcm(llint a,llint b){return a/gcd(a,b)*b;}
template<class T> void SO(T& ve){sort(ve.begin(),ve.end());}
template<class T> void REV(T& ve){reverse(ve.begin(),ve.end());}
int LBI(vector<llint>&ar,llint in){return lower_bound(ar.begin(),ar.end(),in)-ar.begin();}
int UBI(vector<llint>&ar,llint in){return upper_bound(ar.begin(),ar.end(),in)-ar.begin();}
int main(void){
	llint n,i;cin>>n;
	string str;cin>>str;
	vector<pair<llint,llint>>subue;
	vector<pair<llint,llint>>subst;
	llint gen=0,sai=0;
	for(i=0;i<n;i++){
		if(str[i]=='('){gen++;}else{gen--;}
		mineq(sai,gen);
		if(gen>=0){subue.pub(mp(sai,gen));}
		else{subst.pub(mp(sai,gen));}
	}
	sai=0;gen=0;
	SO(subue);REV(subue);
	SO(subst);
	//上方向を使い切る->下方向
	for(auto it:subue){
		mineq(sai,gen+it.fir);
		gen+=it.sec;
	}
	for(auto it:subst){
		mineq(sai,gen+it.fir);
		gen+=it.sec;
	}
	llint ans=n*(n+1)/2;
	ans-=gen;
	ans+=sai*2;
	cout<<ans<<endl;
}
0