結果

問題 No.3114 0→1
ユーザー Yu_212
提出日時 2025-04-19 08:54:36
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 6 ms / 2,000 ms
コード長 1,487 bytes
コンパイル時間 3,368 ms
コンパイル使用メモリ 277,940 KB
実行使用メモリ 6,272 KB
最終ジャッジ日時 2025-04-19 08:54:41
合計ジャッジ時間 4,812 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #

#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