結果

問題 No.2844 Birthday Party Decoration
ユーザー Carpenters-Cat
提出日時 2024-08-23 21:36:09
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 919 bytes
コンパイル時間 2,533 ms
コンパイル使用メモリ 206,620 KB
最終ジャッジ日時 2025-02-23 23:34:11
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample WA * 1
other WA * 4
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll Upb(ll x, int c) {
	if (x & (1ll << c)) {
		return x;
	}
	return ((x >> c) | 1) << c;
}
ll Lwb(ll x, int c) {
	if (x & (1ll << c)) {
		return x;
	}
	ll b = x & ((1ll << c) - 1);
	ll r = x - b - 1;
	if (r < 0) {
		return -(1ll << 61);
	} else {
		return r;
	}
}
using P = pair<ll, int>;
void solve() {
	int N;
	ll X;
	cin >> N >> X;
	std::vector<P> L, R;
	for (int i = 0; i < N; i ++) {
		int c;
		cin >> c;
		L.emplace_back(Lwb(X, c), i);
		R.emplace_back(Upb(X, c), i);
	}
	R.emplace_back(X, N);
	sort(L.begin(), L.end());
	sort(R.begin(), R.end(), greater<P>());
	vector<int> did(N + 1, 0);
	ll ans = (1ll << 61);
	for (auto [l, i] : L) {
		did[i] = 1;
		while (did[R.back().second]) {
			R.pop_back();
		}
		ans = min(ans, abs(X - l) + abs(R.back().first - X));
	}
	cout << ans << endl;
}
int main () {
	int T;
	cin >> T;
	while (T--) solve();
}
0