結果

問題 No.2844 Birthday Party Decoration
ユーザー tottoripapertottoripaper
提出日時 2024-08-24 01:25:46
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 14 ms / 2,000 ms
コード長 1,409 bytes
コンパイル時間 2,651 ms
コンパイル使用メモリ 213,396 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-08-24 01:25:50
合計ジャッジ時間 3,350 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
6,812 KB
testcase_01 AC 14 ms
6,944 KB
testcase_02 AC 9 ms
6,940 KB
testcase_03 AC 9 ms
6,940 KB
testcase_04 AC 10 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define fst(t) std::get<0>(t)
#define snd(t) std::get<1>(t)

using ll = std::int64_t;
using P = std::tuple<ll, ll>;

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

    int T;
    std::cin >> T;

    for(int _=0;_<T;_++){
        int N;
        ll X;
        std::cin >> N >> X;

        std::vector<ll> c(N);
        for(int i=0;i<N;i++){
            std::cin >> c[i];
        }

        std::vector<P> p;
        for(int i=0;i<N;i++){
            if(X >> c[i] & 1){
                p.emplace_back(X, i);
                continue;
            }

            if(X >= (1ll << c[i])){
                p.emplace_back((X & ~((1ll << c[i]) - 1)) - 1, i);
            }
            p.emplace_back((X & ~((1ll << c[i]) - 1)) | (1ll << c[i]), i);
        }
        std::sort(std::begin(p), std::end(p));

        ll res = std::numeric_limits<ll>::max();
        for(int i=0;i+N<=p.size();i++){
            int last = -1;
            ll set = 0;
            for(int j=i;j<p.size();j++){
                set |= 1ll << snd(p[j]);
                if(set == (1ll << N) - 1){
                    last = j;
                    break;
                }
            }

            if(last != -1){
                res = std::min<ll>(res, 2ll * (std::max(fst(p[last]), X) - std::min(fst(p[i]), X)));
            }
        }

        std::cout << res << std::endl;
    }
}
0