Codeforces 282E Sausage Maximization【字典树+贪心】

解决一个算法问题,通过切割整数序列获得两个部分的最大异或值。采用贪心策略配合二进制树实现高效求解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

E. Sausage Maximization
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

The Bitlandians are quite weird people. They have their own problems and their own solutions. They have their own thoughts and their own beliefs, they have their own values and their own merits. They have their own dishes and their own sausages!

In Bitland a sausage is an array of integers! A sausage's deliciousness is equal to the bitwise excluding OR (thexor operation) of all integers in that sausage.

One day, when Mr. Bitkoch (the local cook) was going to close his BitRestaurant, BitHaval and BitAryo, the most famous citizens of Bitland, entered the restaurant and each ordered a sausage.

But Mr. Bitkoch had only one sausage left. So he decided to cut a prefix (several, may be zero, first array elements) of the sausage and give it to BitHaval and a postfix (several, may be zero, last array elements) of the sausage and give it to BitAryo. Note that one or both pieces of the sausage can be empty. Of course, the cut pieces mustn't intersect (no array element can occur in both pieces).

The pleasure of BitHaval and BitAryo is equal to the bitwise XOR of their sausages' deliciousness. An empty sausage's deliciousness equals zero.

Find a way to cut a piece of sausage for BitHaval and BitAryo that maximizes the pleasure of these worthy citizens.

Input

The first line contains an integer n (1 ≤ n ≤ 105).

The next line contains n integers a1, a2, ..., an(0 ≤ ai ≤ 1012) — Mr. Bitkoch's sausage.

Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use thecin, cout streams or the%I64d specifier.

Output

Print a single integer — the maximum pleasure BitHaval and BitAryo can get from the dinner.

Examples
Input
2
1 2
Output
3
Input
3
1 2 3
Output
3
Input
2
1000 1000
Output
1000

题目大意:


给一个长度为n的整数序列,现在要你截取这个序列的一个前缀和一个后缀(前缀和后缀不能相交),使得前缀和后缀的异或值最大。


思路:


贪心去做,对于每个查询,同样处理成35位二进制01字符串,对应进行查询,如果当前位子是0,那么尽量往1那边走,同理,如果当前位子是1,那么尽量往0那边走即可。


那么O(n)建树+查询即可。


Ac代码:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<iostream>
using namespace std;
#define ll __int64
ll a[100002];
ll pre[100002];
ll back[100002];
#define maxn 2
typedef struct tree
{
    tree *nex[maxn];
    ll v;
    ll val;
}tree;
ll output;
tree root;
void init()
{
    for(ll i=0;i<maxn;i++)
    {
        root.nex[i]=NULL;
    }
}
void creat(char *str,ll va)
{
    ll len=strlen(str);
    tree *p=&root,*q;
    for(ll i=0;i<len;i++)
    {
        ll id=str[i]-'0';
        if(p->nex[id]==NULL)
        {
            q=(tree *)malloc(sizeof(root));
            q->v=1;
            for(ll j=0;j<2;j++)
            {
                q->nex[j]=NULL;
            }
            p->nex[id]=q;
        }
        else
        {
            p->nex[id]->v++;
        }
        p=p->nex[id];
        if(i==len-1)
        {
            p->val=va;
        }
    }
}
void find(char *str,ll query)
{
    ll len=strlen(str);
    tree *p=&root;
    for(ll i=0;i<len;i++)
    {
        ll id=str[i]-'0';
        if(p->nex[1-id]!=0)
        {
            p=p->nex[1-id];
        }
        else
        p=p->nex[id];
        if(p==NULL)
        return ;
        if(i==len-1)
        {
            output=max(p->val^query,output);
        }
    }
}
int main()
{
    ll n;
    while(~scanf("%I64d",&n))
    {
        output=0;
        init();
        memset(back,0,sizeof(back));
        memset(pre,0,sizeof(pre));
        memset(a,0,sizeof(a));
        for(ll i=1;i<=n;i++)scanf("%I64d",&a[i]);
        for(ll i=1;i<=n;i++)pre[i]=pre[i-1]^a[i],output=max(pre[i],output);
        for(ll i=n;i>=1;i--)back[i]=back[i+1]^a[i],output=max(back[i],output);
        for(ll i=n;i>=1;i--)
        {
            char s[50];
            ll a=pre[i];
            s[36]='\0';
            for(ll j=35;j>=0;j--)
            {
                if(a)
                {
                    s[j]=a%2+'0';
                    a/=2;
                }
                else
                {
                    s[j]='0';
                }
            }
            find(s,pre[i]);
            a=back[i];
            for(ll j=35;j>=0;j--)
            {
                if(a)
                {
                    s[j]=a%2+'0';
                    a/=2;
                }
                else
                {
                    s[j]='0';
                }
            }
            creat(s,back[i]);
        }
        printf("%I64d\n",output);
    }
}









评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值