QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#285384#7607. The Doubling Game 2Sorting#TL 0ms0kbC++141.5kb2023-12-16 18:21:062023-12-16 18:21:06

Judging History

你现在查看的是最新测评结果

  • [2023-12-16 18:21:06]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:0kb
  • [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

output:


result: