結果

問題 No.2145 Segment +-
ユーザー 👑 rin204rin204
提出日時 2022-12-02 23:05:55
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,551 bytes
コンパイル時間 2,451 ms
コンパイル使用メモリ 195,360 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-18 08:22:46
合計ジャッジ時間 2,820 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const ll MOD = 998244353;
const int N = 1000100;
ll fac[N], invfac[N];

ll modpow(ll a, ll b, ll MOD){
	ll ret = 1;
	while (b > 0){
		if(b & 1){
			ret *= a;
			ret %= MOD;
		}
		a *= a;
		a %= MOD;
		b >>= 1;
	}
	return ret;
}

void init(){
	fac[0] = 1;
	for(int i = 1; i < N; i++) fac[i] = fac[i - 1] * i % MOD;
	invfac[N - 1] = modpow(fac[N - 1], MOD - 2, MOD);
	for(int i = N - 2; i >= 0; i--) invfac[i] = invfac[i + 1] * (i + 1) % MOD;
}

ll nCk(int n, int k){
	if(k < 0 || k > n) return 0;
	else return (fac[n] * invfac[k] % MOD) * invfac[n - k] % MOD;
}

// ++++-?-?-?-?+------+++++++------------------------

void solve(){
	int n;
	string S;
	cin >> n >> S;
	int tot = 0;
	int ma = 0;
	int mi = 0;
	int mic = 0;
	int cnt = 0;
	int ans = 0;
	int prow = 0;
	bool rev = false;
	for(auto s:S){
		if(s == '+'){
			tot++;
			prow++;
			if(tot > ma){
				if(rev){
					tot += cnt - mic;
					tot += 2 * (ma - mi);
					cnt = 0;
				}
				ma = tot;
				cnt = max(0, cnt - 2 * prow);
				prow = 0;
				rev = false;
				mic = 0;
			}
		}
		else if(s == '-'){
			prow = 0;
			tot--;
			if(tot < 0){
				if(!rev){
					ans++;
					rev = true;
					tot -= cnt;
					mi = tot;
					mic = cnt;
				}
				else if(tot < mi){
					int d1 = mi - tot;
					int d2 = cnt - mic;
					if(d1 > d2){
						mi = tot;
						mic = cnt;
					}
				}
			}
		}
		else{
			prow = 0;
			cnt += 2;
			if(rev){
				tot--;
			}
			else{
				tot++;
			}
			
		}
	}
	cout << ans << endl;
}

int main(){
	solve();
}
0