結果

問題 No.3114 0→1
ユーザー AKI
提出日時 2025-04-20 12:55:08
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 4 ms / 2,000 ms
コード長 705 bytes
コンパイル時間 1,965 ms
コンパイル使用メモリ 193,920 KB
実行使用メモリ 7,844 KB
最終ジャッジ日時 2025-04-20 12:55:11
合計ジャッジ時間 2,849 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #

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

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int N;
    string S;
    if (!(cin >> N >> S)) return 0;

    vector<int> dp(N, 0);
    int totalZero = 0;

    for (int i = 0; i < N; ++i) {
        if (S[i] == '0') ++totalZero;

        if (S[i] == '1') {
            dp[i] = (i ? dp[i - 1] : 0);
        } else {
            int not_take = (i ? dp[i - 1] : 0);             // この 0 を 1 に変える
            int take = 1 + (i >= 3 ? dp[i - 3] : 0);        // この 0 を残す
            dp[i] = max(not_take, take);
        }
    }

    int minFlips = totalZero - dp[N - 1];
    cout << minFlips << '\n';
    return 0;
}
0