結果

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

ソースコード

diff #

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

int main() {
    int n;
    string s;
    cin >> n >> s;

    char p = '+';
    for (int i = 0; i < n; i++) {
        if (s[i] == '?') {
            if (p == '+') {
                s[i] = '+';
            }
        } else {
            p = s[i];
        }
    }
    p = '+';
    for (int i = n - 1; i >= 0; i--) {
        if (s[i] == '?') {
            if (p == '+') {
                s[i] = '+';
            }
        } else {
            p = s[i];
        }
    }
    p = '+';
    priority_queue<int> pq;
    int x = 0, y = 0, r = 0;
    s.push_back('+');
    for (int i = 0; i <= n; i++) {
        if (s[i] == '+') {
            if (p == '-') {
                pq.push(y);
                if (x < 0) {
                    int t = pq.top(); pq.pop();
                    x += t * 2;
                    r++;
                }
                y = 0;
            }
            x++;
            p = '+';
        } else {
            if (s[i] == '-') x--, y++; else x++;
            p = '-';
        }
    }
    cout << r << endl;

    return 0;
}
0