結果

問題 No.3114 0→1
ユーザー h tsuneki
提出日時 2025-04-19 00:16:50
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 1,074 bytes
コンパイル時間 893 ms
コンパイル使用メモリ 77,504 KB
実行使用メモリ 6,272 KB
最終ジャッジ日時 2025-04-19 00:16:52
合計ジャッジ時間 2,115 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2 WA * 1
other WA * 30
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <cmath>

using namespace std;

int min_operations(int n, const string& s) {
    /*
    For a good string, any substring must have count(0) <= count(1).
    
    We'll maintain a running balance (count(1) - count(0)) and ensure
    it never goes negative. If it does, we need to convert enough 0s to 1s.
    */
    int balance = 0;  // Current balance: count(1) - count(0)
    int operations = 0;  // Total operations needed
    
    for (char c : s) {
        // Update balance
        if (c == '1') {
            balance++;
        } else {  // c == '0'
            balance--;
        }
        
        // If balance becomes negative, we need to convert some 0s to 1s
        if (balance < 0) {
            operations += abs(balance);  // Convert enough 0s to make balance 0
            balance = 0;  // After conversion, balance becomes 0
        }
    }
    
    return operations;
}

int main() {
    int n;
    string s;
    
    cin >> n;
    cin >> s;
    
    cout << min_operations(n, s) << endl;
    
    return 0;
}
0