Educational Codeforces Round 32 G. Xor-MST

G. Xor-MST
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given a complete undirected graph with n vertices. A number ai is assigned to each vertex, and the weight of an edge between vertices i and j is equal to ai xor aj.

Calculate the weight of the minimum spanning tree in this graph.

Input
The first line contains n (1 ≤ n ≤ 200000) — the number of vertices in the graph.

The second line contains n integers a1, a2, …, an (0 ≤ ai < 230) — the numbers assigned to the vertices.

Output
Print one number — the weight of the minimum spanning tree in the graph.

Examples
input
5
1 2 3 4 5
output
8
input
4
1 2 3 4
output
8

这里mark一个复杂度,对与一个深度不超过log的二叉树,对每一层左右儿子合并的复杂度事log。
对于这一题,建一颗二叉树,两两差值最小的点连接,这样的点肯定是高位尽可能相同,低位不同,然后以不相同的低位看成一个整体,然后让它和高位的另一个子树合并,合并的方法就是将一个子树的值建一个二叉树,然后暴力枚举另一个子树的值,在建好的二叉树上跑,找出最小值。复杂度大概是nloglog

#include<bits/stdc++.h>
using namespace std;
const int N = 2e5+7,M =N*35;
int nex[M][2];
int tot,a[N];
long long ans = 0;

void ins(int x){
    int now = 0;
    for(int i = 29;i >= 0;i --){
        int t = (x>>i)&1;
        if(nex[now][t] == 0) nex[now][t] = ++tot;
        now = nex[now][t];
    }
}

int ask(int x){
    int ret = 0;
    int now = 0;
    for(int i = 29;i >= 0;i --){
        int t = (x>>i)&1;
        if(nex[now][t]) now = nex[now][t];
        else now = nex[now][t^1],ret |= (1<<i);
    }
    return ret;
}

void dfs(int l,int r,int dep){
    if(dep == -1||l >= r) return ;
    int mid = l-1;
    while(mid < r &&(a[mid+1]>>dep&1)==0) mid ++;
    dfs(l,mid,dep-1),dfs(mid+1,r,dep-1);
    if(mid == l-1 || mid == r) return ;
    for(int i = l;i <= mid;i ++){
        ins(a[i]);
    }
    int ret=~0U>>1;
    for(int i = mid+1;i <= r;i ++){
        ret = min(ret,ask(a[i]));
    }
    ans += ret;
    for(int i = 0;i <= tot;i ++){
        nex[i][0] = nex[i][1] = 0;
    }
    tot = 0;
}

int main(){
    int n;
    cin >> n;
    for(int i = 1;i <= n;i ++) scanf("%d",&a[i]);
    sort(a+1,a+n+1);
    dfs(1,n,29);
    cout << ans << endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Educational Codeforces Round 146 (Rated for Div. 2)比赛中,有关于一个节点数量为n的问题。根据引用的结论,无论节点数量是多少,都可以将其分成若干个大小为2或3的区间使其错排,从而减少花费。根据引用的推导,我们可以通过组合数和差量来表示剩余的花费。而引用进一步推广了这个结论,可以使得任意长度的一段错排花费代价为边权和的两倍,并且通过贪心算法使得更多的边不被使用。以此来解决与节点数量相关的问题。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [Educational Codeforces Round 146 (Rated for Div. 2)(B,E详解)](https://blog.csdn.net/TT6899911/article/details/130044099)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [Educational Codeforces Round 83 (Rated for Div. 2) D](https://download.csdn.net/download/weixin_38562130/14878888)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值