結果

問題 No.2145 Segment +-
コンテスト
ユーザー 00 Sakuda
提出日時 2026-02-05 01:42:02
言語 C++23
(gcc 15.2.0 + boost 1.89.0)
結果
AC  
実行時間 142 ms / 2,000 ms
コード長 1,262 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 5,749 ms
コンパイル使用メモリ 204,504 KB
実行使用メモリ 7,848 KB
最終ジャッジ日時 2026-02-05 01:42:25
合計ジャッジ時間 5,381 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 24
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <iostream>
#include <vector>
#include <iomanip>
#include <map>
#include <set>
#include <numeric>
#include <queue>
#include <deque>
#include <algorithm>
#include <stack>
#include <unordered_map>
using namespace std;
using ll = long long;
void chmax(int& a, int b) {
	a = max(a, b);
}
int main() {
	int inf = (int)1e9;
	int N;cin >> N;
	string S;cin >> S;
	int K = 20;
	vector<vector<int>> dp(K + 1, vector<int>(2, -inf));
	dp[0][0] = 0;
	for (int i = 0;i < N;i++) {
		vector<vector<int>> ndp(K + 1, vector<int>(2, -inf));
		vector<int> nxt;
		if (S[i] == '?') {
			nxt = {-1, 1};
		} else if (S[i] == '+') {
			nxt = {1};
		} else {
			nxt = {-1};
		}
		for (int j = 0;j <= K;j++) {
			if (dp[j][0]  >= 0) {
				//not take
				for (auto c : nxt) {
					chmax(ndp[j][0], dp[j][0] + c);
				}

				//take
				if (j+1 <= K) {
				for (auto c : nxt) {
					chmax(ndp[j + 1][1], dp[j][0] + ((-1) * c));
				}
				}
			}

			if (dp[j][1]  >= 0) {
				for (auto c : nxt) {
					//use
					chmax(ndp[j][1], dp[j][1] + ((-1) * c));

					// not use
					chmax(ndp[j][0], dp[j][1] + c);

				}
			}
		}

		swap(dp, ndp);
	}

	int res = inf;
	for (int k = 0;k <= K;k++) for (int f = 0;f < 2;f++) {
		if (dp[k][f] >= 0) res = min(res, k);
	}
	cout << res << endl;
}
0