結果

問題 No.3015 右に寄せろ!
ユーザー じきお。
提出日時 2025-01-25 14:08:26
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 40 ms / 2,000 ms
コード長 1,356 bytes
コンパイル時間 6,359 ms
コンパイル使用メモリ 282,464 KB
実行使用メモリ 21,604 KB
最終ジャッジ日時 2025-01-25 23:08:49
合計ジャッジ時間 6,900 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 36
権限があれば一括ダウンロードができます

ソースコード

diff #

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

#ifdef LOCAL
#include <debug.hpp>
#else
#define debug(...)
#endif

vector<pair<char, int>> run_length_encodeing(const string& S) {
    vector<pair<char, int>> res;
    for (auto c : S) {
        if (!res.empty() && c == res.back().first) {
            res.back().second++;
        } else {
            res.emplace_back(c, 1);
        }
    }
    return res;
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);
    string S;
    cin >> S;
    auto res = run_length_encodeing(S);

    ll ans = 0;
    stack<pair<char, ll>> stk;
    for (auto&& [c, cnt] : res) {
        if (stk.empty()) {
            stk.emplace(c, cnt);
        } else if (stk.top().first == '1' && stk.top().second >= 2 && c == '0') {
            auto [tc, tcnt] = stk.top();
            stk.pop();
            ans += tcnt / 2 * cnt;
            if (tcnt & 1) {
                stk.emplace(tc, 1);
                stk.emplace(c, cnt);
                stk.emplace(tc, tcnt - 1);
            } else {
                stk.emplace(c, cnt);
                stk.emplace(tc, tcnt);
            }
        } else if (stk.top().first == c) {
            stk.top().second += cnt;
        } else {
            stk.emplace(c, cnt);
        }
    }

    cout << ans << '\n';
}
0