結果

問題 No.684 Prefix Parenthesis
ユーザー aguroshouaguroshou
提出日時 2018-05-11 23:53:48
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,703 bytes
コンパイル時間 424 ms
コンパイル使用メモリ 58,916 KB
実行使用メモリ 4,508 KB
最終ジャッジ日時 2023-09-10 17:55:16
合計ジャッジ時間 1,739 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 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 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 AC 4 ms
4,376 KB
testcase_17 AC 3 ms
4,380 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 4 ms
4,380 KB
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 AC 2 ms
4,380 KB
testcase_27 AC 2 ms
4,376 KB
testcase_28 AC 2 ms
4,384 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++;
			}
			else*/
				if(z[b - 1]>=0)
			{
				f++;
			}
			ans += e;
		}
		else if (s[b - 1] == ')')
		{
			if (z[b - 1]>0)
			{
				e++;
			}
			else if (z[b - 1] <= 0)
			{
				g++;
			}
			ans += e;
			z[b] += z[b - 1] - 1;
			
		}
	}
	c = f;
	d = g;
	e = 0;
	for ( b = 0; b < f+g; b++)
	{
		if (e-f>=0)
		{
			ans += f;
			e -= f;
		}
		else if (e-f<0)
		{
			ans += e;
			e = 0;
		}

		e += g;




		if (g>0)
		{
			g--;
		}
		else if (f>0)
		{
			f--;
		}
		else
		{
			break;
		}
	}

	//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--;
	//		}
	//	}
	//}
	//
	//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;
	//}

	//if (ans<0)
	//{
	//	ans = 0;
	//}
	cout << ans*2 << endl;


	return 0;
}
0