結果

問題 No.3754 Mischievous Resident (Hard)
コンテスト
ユーザー marc2825
提出日時 2026-08-07 12:21:18
言語 C++17
(gcc 15.3.0 + boost 1.92.0 + ACL)
コンパイル:
g++-15 -O2 -lm -std=c++17 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 60 ms / 2,000 ms
+ 903µs
コード長 2,787 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,224 ms
コンパイル使用メモリ 225,744 KB
実行使用メモリ 9,904 KB
最終ジャッジ日時 2026-10-02 20:52:53
合計ジャッジ時間 8,693 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 48
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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

using ll = long long;
const ll INF = 1LL << 60;


// 間隔 d の状態から、片側を合計 x だけ逆向きに動かすための最小操作回数
ll calc(ll x, ll d) {
    ll sum = 0;
    int k = 0;
    while (sum < x) {
        sum = sum * 2 + d;
        k++;
    }
    return k;
}

// 目標間隔 D、逆向きの移動距離 (A, B) について、A 側を先に完成させる最小操作回数
ll solve(ll A, ll B, ll D) {
    ll res = INF;

    for (int q = 0; q < 30; ++q) {
        ll H = (1LL << q) * D;
        ll P = (1LL << q) - 1;

        ll l = max(0LL, P - (B - 1) / D);
        if (A > H) l = max(l, (A - H + D - 1) / D);

        ll r = min(P, (A - 1) / D);
        if (l > r) continue;

        ll a = l;
        ll x = A - D * a;
        ll y = B - D * (P - a);

        res = min(res, (ll)q + 1 + calc(y, H + x));
    }

    return res;
}


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

    int Q;
    cin >> Q;
    while (Q--) {
        int N, M;
        cin >> N >> M;

        vector<pair<int, int>> A(M);
        for (auto& [s, g] : A) cin >> s;
        for (auto& [s, g] : A) cin >> g;

        sort(A.begin(), A.end());

        bool ok = true;
        for (int i = 1; i < M; ++i) {
            auto [s1, g1] = A[i - 1];
            auto [s2, g2] = A[i];

            if (g1 > g2 ||
                (g1 == g2 && !(s1 == s2 && s1 == g1))) {
                ok = false;
                break;
            }
        }

        if (!ok) {
            cout << -1 << '\n';
            continue;
        }


        // 1: R, -1: L, 0: S
        vector<int> type(M);
        for (int i = 0; i < M; ++i) {
            auto [s, g] = A[i];
            if (s < g) type[i] = 1;
            if (s > g) type[i] = -1;
        }

        vector<bool> paired(M);
        ll ans = 0;

        // RL ペア
        for (int i = 0; i + 1 < M; ++i) {
            if (type[i] == 1 && type[i + 1] == -1) {
                paired[i] = paired[i + 1] = true;

                auto [s1, g1] = A[i];
                auto [s2, g2] = A[i + 1];

                ll X = g1 - s1;
                ll Y = s2 - g2;
                ll D = g2 - g1;

                ans += min(solve(X, Y, D), solve(Y, X, D));
            }
        }

        // RL ペアに含まれない
        for (int i = 0; i < M; ++i) {
            if (paired[i]) continue;

            auto [s, g] = A[i];

            if (type[i] == 1) {
                if (i + 1 == M) ans++;
                else ans += calc(g - s, A[i + 1].second - g);
            }

            if (type[i] == -1) {
                if (i == 0) ans++;
                else ans += calc(s - g, g - A[i - 1].second);
            }
        }

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