JZOJ-senior-4322. 【NOIP2015模拟11.5】Xor

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/HuangXinyue1017/article/details/82934045

Time Limits: 1000 ms Memory Limits: 262144 KB Detailed Limits

Description

在这里插入图片描述

Input

在这里插入图片描述

Output

在这里插入图片描述

Sample Input

4
3 6 7 7

Sample Output

5

Data Constraint

在这里插入图片描述

Solution

贪心

二进制拆位,建01字典树
可以走不同就走(产生贡献),不可以就走相同

Code

#include<algorithm>
#include<cstdio>
#include<cmath>

#define fo(i,a,b) for(ll i=a;i<=b;++i)
#define fd(i,a,b) for(ll i=a;i>=b;--i)
#define ll long long

using namespace std;

const ll N=1e5+5,MX=55,M=55e5+5;
ll n,mx,cnt,ans;
ll a[N],t[N][MX+5],num[M],s[M][2];

void ins(ll y)
{
	ll x=1;
	fd(i,MX,1)
	{
		if(!s[x][t[y][i]]) s[x][t[y][i]]=++cnt;
		x=s[x][t[y][i]];
	}
	num[x]=y;
}

ll ask(ll y)
{
	ll x=1;
	fd(i,MX,1)
		if(!s[x][t[y][i]^1]) x=s[x][t[y][i]];
			else x=s[x][t[y][i]^1];
	return num[x];
}

int main()
{
	freopen("xor.in","r",stdin);
	freopen("xor.out","w",stdout);
	scanf("%lld",&n),cnt=1;
	fo(i,1,n)
	{
		scanf("%lld",&a[i]); ll mx=0,p=a[i];
		while(p) t[i][++mx]=p&1,p>>=1;
		ins(i);
	}
	fo(i,1,n) ans=max(ans,a[i]^a[ask(i)]);
	printf("%lld",ans);
}

猜你喜欢

转载自blog.csdn.net/HuangXinyue1017/article/details/82934045