結果

問題 No.3114 0→1
コンテスト
ユーザー Yu_212
提出日時 2025-04-19 08:54:36
言語 C++23
(gcc 15.3.0 + boost 1.92.0 + ACL)
コンパイル:
g++-15 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
WJ  
(最新)
AC  
(最初)
実行時間 -
コード長 1,487 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
最終ジャッジ日時 2026-10-16 03:23:53
ジャッジサーバーID
(参考情報)
judge1_0 / judge3_0
このコードへのチャレンジ
(要ログイン)

ソースコード

diff #
raw source code

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

const int iinf = 1e9;
const ll inf = 1e18;

template<typename T>
ostream& operator<<(ostream &o, vector<T> v) {
    for (int i = 0; i < v.size(); i++)
        o << v[i] << (i+1<v.size()?" ":"");
    return o;
}

struct Edge { int to; ll cost; };

int main() {
    cin.tie(0)->sync_with_stdio(false);

    int N;
    string S;
    cin >> N >> S;
    S = "11" + S;

    vector<ll> dp(4, inf), ndp(4, inf);
    auto valid = [&](int b1, int b2){
        if(b1==0 && b2==0) return false;
        return true;
    };
    dp[3] = 0;

    for(int i = 2; i < N+2; i++){
        fill(ndp.begin(), ndp.end(), inf);
        for(int st = 1; st < 4; st++){
            if(dp[st] == inf) continue;
            int p1 = (st>>1)&1, p2 = st&1;
            if(S[i]=='0'){
                int x = 0;
                if(!(p1==0 && p2==1 && x==0)){
                    if(valid(p2,x)){
                        int nst = (p2<<1) | x;
                        ndp[nst] = min(ndp[nst], dp[st] + 0);
                    }
                }
            }
            {
                int x = 1;
                if(valid(p2,x)){
                    int nst = (p2<<1) | x;
                    ndp[nst] = min(ndp[nst], dp[st] + (S[i]=='0'?1:0));
                }
            }
        }
        swap(dp, ndp);
    }

    ll ans = inf;
    for(int st = 1; st < 4; st++){
        ans = min(ans, dp[st]);
    }
    cout << ans << "\n";
    return 0;
}
0