CF1285D Dr. Evil Underscores(分治)

l

思路:

dfs搜素即可,从最高位开始搜。
如果该位的左儿子或右儿子只有一个,继续搜索有的;否则,两者取min

代码:

// Problem: CF1285D Dr. Evil Underscores
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/CF1285D
// Memory Limit: 250 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll>PLL;
typedef pair<int, int>PII;
typedef pair<double, double>PDD;
#define I_int ll
inline ll read()
{
    ll x = 0, f = 1;
    char ch = getchar();
    while(ch < '0' || ch > '9')
    {
        if(ch == '-')f = -1;
        ch = getchar();
    }
    while(ch >= '0' && ch <= '9')
    {
        x = x * 10 + ch - '0';
        ch = getchar();
    }
    return x * f;
}

inline void out(ll x){
	if (x < 0) x = ~x + 1, putchar('-');
	if (x > 9) out(x / 10);
	putchar(x % 10 + '0');
}

inline void write(ll x){
	if (x < 0) x = ~x + 1, putchar('-');
	if (x > 9) write(x / 10);
	putchar(x % 10 + '0');
	puts("");
}

#define read read()
#define closeSync ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define multiCase int T;cin>>T;for(int t=1;t<=T;t++)
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define repp(i,a,b) for(int i=(a);i<(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
#define perr(i,a,b) for(int i=(a);i>(b);i--)
ll ksm(ll a, ll b, ll p)
{
    ll res = 1;
    while(b)
    {
        if(b & 1)res = res * a % p;
        a = a * a % p;
        b >>= 1;
    }
    return res;
}
const int inf = 0x3f3f3f3f;
#define PI acos(-1)
const int maxn=4e5+100;

int n;
vector<int>v;

int dfs(vector<int>v,int k){
	if(k<0||v.size()==0) return 0;
	vector<int>p1,p2;
	for(int i=0;i<v.size();i++){
		int j=v[i];
		if(j&(1<<k)) p1.push_back(j);
		else p2.push_back(j);
	}	
	if(p1.size()==0) return dfs(p2,k-1);
	if(p2.size()==0) return dfs(p1,k-1);
	return (1<<k)+min(dfs(p1,k-1),dfs(p2,k-1));
}

int main(){
	n=read;
	rep(i,1,n) v.push_back(read);
	cout<<dfs(v,30)<<"\n";
	return 0;
} 












posted @ 2021-07-22 10:15  OvO1  阅读(39)  评论(0编辑  收藏  举报