結果

問題 No.3114 0→1
ユーザー Aldric Liew
提出日時 2025-04-20 13:23:57
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 920 bytes
コンパイル時間 836 ms
コンパイル使用メモリ 76,552 KB
実行使用メモリ 7,848 KB
最終ジャッジ日時 2025-04-20 13:24:00
合計ジャッジ時間 2,096 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1 WA * 2
other WA * 30
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <vector>
#include <queue>
using namespace std;

int main() {
    int N;
    string S;
    cin >> N >> S;

    // Priority queue to keep positions of '0's to flip greedily (earliest first)
    priority_queue<int, vector<int>, greater<int>> zero_positions;
    int score = 0;
    int flips = 0;

    for (int i = 0; i < N; ++i) {
        if (S[i] == '1') score++;
        else {
            score--;
            zero_positions.push(i);
        }

        // Whenever score <= 0 (invalid substring somewhere), flip a 0
        if (score <= 0) {
            // flip earliest available 0
            if (!zero_positions.empty()) {
                int pos = zero_positions.top(); zero_positions.pop();
                // pretend we flipped this 0 to 1
                score += 2;
                flips++;
            }
        }
    }

    cout << flips << endl;
    return 0;
}
0