QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#285384 | #7607. The Doubling Game 2 | Sorting# | TL | 0ms | 0kb | C++14 | 1.5kb | 2023-12-16 18:21:06 | 2023-12-16 18:21:06 |
answer
#include<bits/stdc++.h>
using namespace std;
using vi = vector<int>;
using pii = pair<int, int>;
using ll = long long;
using vll = vector<ll>;
int B;
struct Node {
int bit;
bool all0 = true;
Node *l, *r;
Node(int b) : bit(b) {
}
void set(ll x) {
if(bit == B) {
all0 = false;
} else {
if(all0) {
all0 = false;
l = new Node(bit+1);
r = new Node(bit+1);
}
Node* nxt = (((x >> bit) & 1) == ((x >> (bit+1)) & 1)) ? l : r;
nxt->set(x);
}
}
pii calc() {
if(all0) {
return {0, 1};
}
if(bit == B) {
return {1, 0};
}
int l0, l1, r0, r1;
tie(l0, l1) = l->calc();
tie(r0, r1) = r->calc();
return {min(l0 + r0, 1 + l1 + r1), min(l1 + r1, 1 + l0 + r0)};
}
};
void solve() {
int n, m;
cin >> n >> m;
vll v(m);
for(int i = 0; i < m; i++) {
cin >> v[i];
v[i]--;
}
B = 0;
while((1<<B) != n) {
B++;
}
// B++;
// cerr << "B = " << B << endl;
Node* root = new Node(0);
for(ll i : v) {
root->set(i);
}
pii ans = root->calc();
cout << min(ans.first, ans.second) << endl;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(0);
solve();
return 0;
}
详细
Test #1:
score: 0
Time Limit Exceeded
input:
5 1 2 1 3 1 4 4 5