JZOJ-senior-4322. 【NOIP2015模擬11.5】Xor
阿新 • • 發佈:2018-12-13
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);
}