結果

問題 No.884 Eat and Add
コンテスト
ユーザー zelda_master
提出日時 2026-09-19 18:21:36
言語 C++14
(gcc 15.3.0 + boost 1.92.0 + ACL)
コンパイル:
g++-15 -O2 -lm -std=c++14 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 6 ms / 1,000 ms
+ 132µs
コード長 1,176 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 456 ms
コンパイル使用メモリ 85,884 KB
実行使用メモリ 10,000 KB
最終ジャッジ日時 2026-09-19 18:21:40
合計ジャッジ時間 1,778 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge4_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 9
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <iostream>
#include <cstdio>
#include <vector>

using namespace std;

typedef long long LL;
typedef pair<int, int> PII;

const int N = 200010;

string s;
vector<PII> vec;
LL cost[N], gap[N], ps[N], f[N];

int main() {
    // freopen("apple.in", "r", stdin);
    // freopen("apple.out", "w", stdout);

    cin >> s;

    int n = s.size();
    s = ' ' + s;
    for (int l = 1; l <= n; ) {
        while (l <= n && s[l] == '0') ++l;
        if (l > n) break;
        int r = l;
        while (r < n && s[r + 1] == s[l]) ++r;
        vec.push_back({ l, r });
        l = r + 1;
    }
    if (vec.empty()) {
        puts("0");
        return 0;
    }

    for (int i = 0; i < vec.size(); ++i) {
        cost[i] = min(2, vec[i].second - vec[i].first + 1);
    }
    for (int i = 1; i < vec.size(); ++i) {
        gap[i] = vec[i].first - vec[i - 1].second - 1;
        ps[i] = ps[i - 1] + gap[i];
    }

    LL best = 0LL;
    f[0] = cost[0];
    for (int i = 1; i < vec.size(); ++i) {
        best = min(best, f[i - 1] - ps[i]);
        f[i] = f[i - 1] + cost[i];
        f[i] = min(f[i], best + ps[i] + 2);
    }

    printf("%lld\n", f[vec.size() - 1]);

    return 0;
}
0