CodeForces Round #173 (282E) - Sausage Maximization 字典树

本文介绍了一种利用位运算和前缀树解决特定问题的方法。通过对高位进行优先处理,并确保低位不会影响高位的策略,文章提供了一个清晰的实现思路。通过大神的点拨,原本难以解决的问题变得豁然开朗。

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

     练习赛的时候这道题死活超时....想到了高位确定后..低位不能对高位产生影响..并且高位要尽可能的为1..就是想不出比较好的方法了实现...

     围观大神博客..http://www.cnblogs.com/zhj5chengfeng/archive/2013/05/14/3077621.html

     思路很清晰了..没什么补充的..自己的思维还是不够啊...大神几句话点拨...豁然开朗...


Program:

 

#include<iostream>
#include<string.h>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stdio.h>
#include<stack>
#define oo 1000000007
#define ll long long
#define pi acos(-1.0)
using namespace std;
struct node
{
       int son[2]; 
       ll w;
}p[10000005];
ll a[100005],totol,ans,_2jie[45];
int num;
void InsertToTrie(ll x)
{
       int h=0,i,t;
       for (i=40;i>=0;i--)
       {
             if (x & _2jie[i]) t=1;
                else t=0;
             if (!p[h].son[t]) p[h].son[t]=++num;
             h=p[h].son[t];
       }       
       p[h].w=x;
       return;
}
ll SerchMax(ll x)
{
       int h,i,t;
       h=0;
       for (i=40;i>=0;i--)
       {
             if (x & _2jie[i]) t=1;
                else t=0;
             if (p[h].son[1-t]) h=p[h].son[1-t];
                else h=p[h].son[t]; 
       }
       return p[h].w;
} 
int main()
{
       int i,n;
       ll prefix,postfix; 
       _2jie[0]=1;
       for (i=1;i<=40;i++) _2jie[i]=_2jie[i-1]*2;
       while (~scanf("%d",&n))
       {
             postfix=0;
             for (i=1;i<=n;i++) scanf("%I64d",&a[i]),postfix^=a[i]; 
             memset(p,0,sizeof(p));
             ans=postfix;
             num=0;
             prefix=0;
             InsertToTrie(0);
             for (i=1;i<=n;i++)
             {
                    prefix^=a[i];
                    InsertToTrie(prefix);
                    postfix^=a[i]; 
                    ans=max(ans,SerchMax(postfix)^postfix);
             }
             printf("%I64d\n",ans);    
       }
       return 0;
}


 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值