結果

問題 No.684 Prefix Parenthesis
ユーザー aguroshou
提出日時 2018-05-11 23:25:42
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
WA  
実行時間 -
コード長 969 bytes
コンパイル時間 551 ms
コンパイル使用メモリ 59,768 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-06-28 09:03:08
合計ジャッジ時間 1,558 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 13 WA * 18
権限があれば一括ダウンロードができます

ソースコード

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