結果

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

ソースコード

diff #

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

int main() {
    string S;
    cin >> S;
    int N = S.length();

    int flips = 0;
    int balance = 0; // 1s - 0s

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

        // If at any point balance goes negative, fix it by flipping a 0
        if (balance < 0) {
            flips++;
            balance += 2; // flip the current 0 to 1
        }
    }

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