結果

問題 No.684 Prefix Parenthesis
ユーザー aguroshouaguroshou
提出日時 2018-05-11 23:25:42
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 969 bytes
コンパイル時間 520 ms
コンパイル使用メモリ 61,540 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-10 17:52:02
合計ジャッジ時間 1,918 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include<iostream>
#include <algorithm>
#include<string>
using namespace std;

int main()
{
	int a = 0, b = 0, c = 0, d = 0, e = 0, f = 0, g = 0, h = 0,ans=0;
	string s;
	cin >> a;
	cin >> s;
	int z[100001] = {};
	z[0] = 0;
	for ( b = 1; b <= a; b++)
	{
		if (s[b-1]=='(')
		{
			z[b] += z[b - 1] + 1;
			if (z[b - 1]<0)
			{
				e++;
			}
			ans += e;
			h = 1;
		}
		else if (s[b - 1] == ')')
		{
			if (z[b-1]>0)
			{
				e++;
			}
			ans += e;
			z[b] += z[b - 1] - 1;
			if (h==0)
			{
				ans--;
			}
		}
	}
	if (ans<0)
	{
		ans = 0;
	}
	for (b = 0; b <= a; b++)
	{
		//cout << z[b] << endl;
	}
	//sort(z, z + 100001);
	sort(z,z+a);
	for  (b = a; b>=0 ; b--)
	{
		if (z[b]>0)
		{
			c+=z[b];
		}
		else if (z[b]<0)
		{
			if (c>0)
			{
				if (c-z[b]<0)
				{
				ans += c;
				c += z[b];

				}
				else
				{
					ans += -z[b];
					c += z[b];

				}
			}
		}
	}
	for ( b = 0; b < a; b++)
	{
		//cout << z[b] << endl;
	}


	cout << ans*2 << endl;


	return 0;
}
0