結果

問題 No.3754 Mischievous Resident (Hard)
コンテスト
ユーザー marc2825
提出日時 2026-08-07 12:07:52
言語 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
結果
TLE  
実行時間 -
コード長 3,308 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,221 ms
コンパイル使用メモリ 224,520 KB
実行使用メモリ 9,904 KB
最終ジャッジ日時 2026-10-02 20:52:56
合計ジャッジ時間 10,874 ms
ジャッジサーバーID
(参考情報)
judge2_1 / judge4_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 21 TLE * 1 -- * 26
権限があれば一括ダウンロードができます

ソースコード

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;
}


// RL ペアを逆向きに愚直再帰する。
// 現在の間隔が D、左側・右側をあと A, B だけ逆向きに動かす。
ll solve_pair(ll A, ll B, ll D) {
    if (A == 0) return calc(B, D);
    if (B == 0) return calc(A, D);

    ll res = INF;

    // 次に左側を動かす
    if (A <= D) {
        // 1 回で左側が完成
        res = min(res, 1 + calc(B, D + A));
    } else {
        // 完成しないなら D だけ動かしてよい
        res = min(res, 1 + solve_pair(A - D, B, 2 * D));
    }

    // 次に右側を動かす
    if (B <= D) {
        // 1 回で右側が完成
        res = min(res, 1 + calc(A, D + B));
    } else {
        // 完成しないなら D だけ動かしてよい
        res = min(res, 1 + solve_pair(A, B - D, 2 * D));
    }

    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 += solve_pair(X, Y, 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