結果

問題 No.684 Prefix Parenthesis
ユーザー saming2209saming2209
提出日時 2018-05-11 23:57:51
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 931 bytes
コンパイル時間 1,191 ms
コンパイル使用メモリ 149,532 KB
実行使用メモリ 5,332 KB
最終ジャッジ日時 2023-09-10 17:57:03
合計ジャッジ時間 2,592 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 5 ms
4,584 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 2 ms
4,380 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 AC 7 ms
4,896 KB
testcase_17 AC 5 ms
4,380 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 5 ms
5,332 KB
testcase_23 AC 5 ms
5,072 KB
testcase_24 AC 6 ms
5,196 KB
testcase_25 WA -
testcase_26 AC 1 ms
4,380 KB
testcase_27 AC 2 ms
4,376 KB
testcase_28 AC 1 ms
4,376 KB
testcase_29 WA -
testcase_30 AC 8 ms
5,328 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>

#define INF 1000000007
#define LINF 1000000000000000007

using namespace std;

typedef long long Int;
typedef pair<Int, Int> P;

int n;
P r[100000];
string s;

int main(){
	cin >> n;
	cin >> s;
	
	if(s[0] == ')') r[0] = make_pair(1, 0);
	else r[0] = make_pair(0,0);
	bool y = 0, x = 0;
	if(s[0] == ')') y = 1;
	else x = 1;
	for(int i = 1; i < n; i++){
		r[i].first = r[i-1].first;
		if(s[i] == ')'){r[i].first++; y = 1;}
		else x = 1;
		r[i].second = i;
	}
	
	if(!x || !y){cout << 0 << endl; return 0;}
	sort(r, r+ n);
	Int cnt = 0, migi = 0, hidari = 0;
	for(int i = 1; i < n; i++){
		int j = i, k = i;
		while(r[j-1].first == r[j].first){
			j++;
		}
		i = j;
		while(j >= k){
			if(j != 0) migi += r[j].first;
			hidari += r[j-1].second - r[j-1].first + 1;
			cnt += min(migi, hidari)*2;
			migi -= min(migi, hidari);
			hidari -= min(migi, hidari);
			j--;
		}
	}
	
	cout << cnt << endl;
	
	return 0;
}
0