結果

問題 No.2844 Birthday Party Decoration
ユーザー Tatsu_mr
提出日時 2024-08-23 22:30:58
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 11 ms / 2,000 ms
コード長 1,151 bytes
コンパイル時間 1,739 ms
コンパイル使用メモリ 197,152 KB
最終ジャッジ日時 2025-02-24 00:01:49
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 4
権限があれば一括ダウンロードができます

ソースコード

diff #

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

using lint = long long;

lint INF = -3000000000000000000;

lint nearL(lint x, lint i) {
    lint a = (1LL << i), b = (1LL << (i + 1LL));
    if (x & a) {
        return x;
    }
    lint q = (x - (b - 1)) / b;
    if (b * q + (b - 1) > x) {
        q--;
    }
    lint res = b * q + (b - 1);
    return (res >= 0 ? res : INF);
}

lint nearR(lint x, lint i) {
    lint a = (1LL << i), b = (1LL << (i + 1LL));
    if (x & a) {
        return x;
    }
    lint q = (x - a) / b;
    if (b * q + a < x) {
        q++;
    }
    return b * q + a;
}

int main() {
    int t;
    cin >> t;
    while (t--) {
        lint n, x;
        cin >> n >> x;
        vector<lint> c(n);
        for (int i = 0; i < n; i++) {
            cin >> c[i];
        }
        vector<lint> l(n), r(n);
        for (int i = 0; i < n; i++) {
            l[i] = nearL(x, c[i]);
            r[i] = nearR(x, c[i]);
        }
        lint ml = *min_element(l.begin(), l.end()), mr = *max_element(r.begin(), r.end());
        lint dl = abs(x - ml), dr = abs(x - mr);
        cout << min({2 * dl + 2 * dr, 2 * dl, 2 * dr}) << endl;
    }
}
0