結果

問題 No.2145 Segment +-
ユーザー 👑 rin204rin204
提出日時 2022-12-02 22:54:14
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 1,464 bytes
コンパイル時間 1,962 ms
コンパイル使用メモリ 194,332 KB
最終ジャッジ日時 2025-02-09 04:10:52
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 23 WA * 1
権限があれば一括ダウンロードができます

ソースコード

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;
	bool rev = false;
	for(auto s:S){
		if(s == '+'){
			tot++;
			if(tot > ma){
				if(rev){
					tot += cnt - mic;
					tot += 2 * (ma - mi);
				}
				ma = tot;
				cnt = max(0, cnt - 1);
				rev = false;
				mic = 0;
			}
		}
		else if(s == '-'){
			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{
			cnt += 2;
			if(rev){
				tot--;
			}
			else{
				tot++;
			}
			
		}
	}
	cout << ans << endl;
}

int main(){
	solve();
}
0