結果

問題 No.684 Prefix Parenthesis
ユーザー maroon_kurimaroon_kuri
提出日時 2018-05-11 22:57:07
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 12 ms / 2,000 ms
コード長 2,740 bytes
コンパイル時間 1,746 ms
コンパイル使用メモリ 175,640 KB
実行使用メモリ 8,096 KB
最終ジャッジ日時 2023-09-10 18:36:25
合計ジャッジ時間 3,024 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,560 KB
testcase_01 AC 2 ms
5,500 KB
testcase_02 AC 2 ms
5,572 KB
testcase_03 AC 4 ms
5,828 KB
testcase_04 AC 7 ms
6,500 KB
testcase_05 AC 9 ms
7,396 KB
testcase_06 AC 4 ms
5,900 KB
testcase_07 AC 7 ms
6,748 KB
testcase_08 AC 3 ms
5,800 KB
testcase_09 AC 3 ms
5,720 KB
testcase_10 AC 11 ms
7,612 KB
testcase_11 AC 8 ms
7,284 KB
testcase_12 AC 3 ms
5,644 KB
testcase_13 AC 11 ms
7,400 KB
testcase_14 AC 12 ms
7,244 KB
testcase_15 AC 5 ms
6,220 KB
testcase_16 AC 8 ms
7,244 KB
testcase_17 AC 5 ms
6,156 KB
testcase_18 AC 2 ms
5,752 KB
testcase_19 AC 10 ms
7,812 KB
testcase_20 AC 10 ms
7,520 KB
testcase_21 AC 10 ms
7,280 KB
testcase_22 AC 8 ms
7,332 KB
testcase_23 AC 7 ms
7,548 KB
testcase_24 AC 8 ms
7,300 KB
testcase_25 AC 8 ms
8,096 KB
testcase_26 AC 2 ms
5,600 KB
testcase_27 AC 2 ms
5,640 KB
testcase_28 AC 2 ms
5,584 KB
testcase_29 AC 10 ms
7,660 KB
testcase_30 AC 10 ms
7,488 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

using ll=int64_t;
#define int ll

#define FOR(i,a,b) for(int i=int(a);i<int(b);i++)
#define REP(i,b) FOR(i,0,b)
#define MP make_pair
#define PB push_back
#define ALL(x) x.begin(),x.end()
#ifdef MAROON_LOCAL
#define cerr (cerr<<"-- line "<<__LINE__<<" -- ")
#else
class CerrDummy{}cerrDummy;
template<class T>
CerrDummy& operator<<(CerrDummy&cd,const T&){
	return cd;
}
using charTDummy=char;
using traitsDummy=char_traits<charTDummy>;
CerrDummy& operator<<(CerrDummy&cd,basic_ostream<charTDummy,traitsDummy>&(basic_ostream<charTDummy,traitsDummy>&)){
	return cd;
}
#define cerr cerrDummy
#endif
#define REACH cerr<<"reached"<<endl
#define DMP(x) cerr<<#x<<":"<<x<<endl
#define ZERO(x) memset(x,0,sizeof(x))
#define ONE(x) memset(x,-1,sizeof(x))

using pi=pair<int,int>;
using vi=vector<int>;
using ld=long double;

template<class T,class U>
ostream& operator<<(ostream& os,const pair<T,U>& p){
	os<<"("<<p.first<<","<<p.second<<")";
	return os;
}

template<class T>
ostream& operator <<(ostream& os,const vector<T>& v){
	os<<"{";
	REP(i,(int)v.size()){
		if(i)os<<",";
		os<<v[i];
	}
	os<<"}";
	return os;
}

ll read(){
	ll i;
	scanf("%" SCNd64,&i);
	return i;
}

void printSpace(){
	printf(" ");
}

void printEoln(){
	printf("\n");
}

void print(ll x,int suc=1){
	printf("%" PRId64,x);
	if(suc==1)
		printEoln();
	if(suc==2)
		printSpace();
}

string readString(){
	static char buf[3341000];
	scanf("%s",buf);
	return string(buf);
}

char* readCharArray(){
	static char buf[3341000];
	static int bufUsed=0;
	char* ret=buf+bufUsed;
	scanf("%s",ret);
	bufUsed+=strlen(ret)+1;
	return ret;
}

template<class T,class U>
void chmax(T& a,U b){
	if(a<b)
		a=b;
}

template<class T,class U>
void chmin(T& a,U b){
	if(b<a)
		a=b;
}

template<class T>
T Sq(const T& t){
	return t*t;
}

#define CAPITAL
void Yes(bool ex=true){
	#ifdef CAPITAL
	cout<<"YES"<<endl;
	#else
	cout<<"Yes"<<endl;
	#endif
	if(ex)exit(0);
}
void No(bool ex=true){
	#ifdef CAPITAL
	cout<<"NO"<<endl;
	#else
	cout<<"No"<<endl;
	#endif
	if(ex)exit(0);
}

const ll infLL=LLONG_MAX/3;

#ifdef int
const int inf=infLL;
#else
const int inf=INT_MAX/2-100;
#endif

signed main(){
	int n=read();
	string s=readString();
	int cur=0,mn=0;
	vector<pi> seq1,seq2;
	for(auto c:s){
		if(c=='(')
			cur++;
		else
			cur--;
		chmin(mn,cur);
		if(cur>=0){
			seq1.PB(pi(-mn,cur));
		}else{
			seq2.PB(pi(cur-mn,cur));
		}
	}
	sort(ALL(seq1));
	sort(ALL(seq2),greater<pi>());
	cur=0;
	mn=0;
	for(auto p:seq1){
		cerr<<p<<endl;
		chmin(mn,cur-p.first);
		cur+=p.second;
		chmin(mn,cur);
	}
	for(auto p:seq2){
		cerr<<p<<endl;
		cur+=p.second;
		chmin(mn,cur);
		chmin(mn,cur-p.first);
	}
	int ans=n*(n+1)/2;
	ans-=0-mn;
	ans-=cur-mn;
	print(ans);
}
0