結果

問題 No.2145 Segment +-
ユーザー tree_splaytree_splay
提出日時 2022-12-02 23:17:02
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 72 ms / 2,000 ms
コード長 1,406 bytes
コンパイル時間 2,275 ms
コンパイル使用メモリ 195,952 KB
実行使用メモリ 50,676 KB
最終ジャッジ日時 2024-04-18 08:32:55
合計ジャッジ時間 3,883 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 14 ms
50,368 KB
testcase_01 AC 14 ms
50,304 KB
testcase_02 AC 15 ms
50,304 KB
testcase_03 AC 16 ms
50,416 KB
testcase_04 AC 15 ms
50,432 KB
testcase_05 AC 14 ms
50,432 KB
testcase_06 AC 14 ms
50,304 KB
testcase_07 AC 14 ms
50,272 KB
testcase_08 AC 16 ms
50,312 KB
testcase_09 AC 14 ms
50,388 KB
testcase_10 AC 50 ms
50,548 KB
testcase_11 AC 72 ms
50,672 KB
testcase_12 AC 17 ms
50,304 KB
testcase_13 AC 15 ms
50,304 KB
testcase_14 AC 14 ms
50,304 KB
testcase_15 AC 17 ms
50,432 KB
testcase_16 AC 15 ms
50,172 KB
testcase_17 AC 15 ms
50,432 KB
testcase_18 AC 16 ms
50,296 KB
testcase_19 AC 15 ms
50,424 KB
testcase_20 AC 39 ms
50,560 KB
testcase_21 AC 19 ms
50,432 KB
testcase_22 AC 15 ms
50,176 KB
testcase_23 AC 15 ms
50,304 KB
testcase_24 AC 39 ms
50,560 KB
testcase_25 AC 52 ms
50,676 KB
testcase_26 AC 50 ms
50,552 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

#define INF 1234567890
#define ll long long

int N;
string s;
int dp[200010][30][2];

int main()
{
	ios::sync_with_stdio(0); cin.tie(0);
	cin.exceptions(ios::badbit | ios::failbit);
	cin >> N >> s;
	for(int i=0; i<200010; i++)
		for(int j=0; j<30; j++)
			for(int k=0; k<2; k++)
				dp[i][j][k] = -INF;

	// base case
	dp[0][0][0] = 0;

	// cal dp
	for(int i=0; i<N; i++)
		for(int j=0; j<30; j++)
			for(int k=0; k<2; k++)
				if (dp[i][j][k] >= 0)
				{
					if (s[i] == '?')
					{
						dp[i+1][j][k] = max(dp[i+1][j][k], dp[i][j][k] + 1);
					}
					else if (s[i] == '+')
					{
						if (k)
						{
							dp[i+1][j][k] = max(dp[i+1][j][k], dp[i][j][k] - 1);
							dp[i+1][j][0] = max(dp[i+1][j][0], dp[i][j][k] + 1);
						}
						else
						{
							dp[i+1][j][k] = max(dp[i+1][j][k], dp[i][j][k] + 1);
						}
					}
					else if (s[i] == '-')
					{
						if (k)
						{
							dp[i+1][j][k] = max(dp[i+1][j][k], dp[i][j][k] + 1);
							dp[i+1][j][0] = max(dp[i+1][j][0], dp[i][j][k] - 1);
						}
						else
						{
							dp[i+1][j][k] = max(dp[i+1][j][k], dp[i][j][k] - 1);
							if (j+1 < 30) dp[i+1][j+1][1] = max(dp[i+1][j+1][1], dp[i][j][k] + 1);
						}
					}
					else assert(0);
				}

	// find res
	for(int j=0; j<30; j++)
		if (dp[N][j][0] >= 0 || dp[N][j][1] >= 0)
		{
			cout << j << "\n";
			return 0;
		}
	assert(0);
	return 0;
}
0