• Codeforces Round #173 (Div. 2) E. Sausage Maximization —— 字典树 + 前缀和


    题目链接:http://codeforces.com/problemset/problem/282/E


    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 (the xor 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 the cincout streams or the %I64dspecifier.

    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


    题解:

    1.预处理前缀异或、后缀异或。

    2.枚举每一个前缀异或(i:0~n):

    2.1.将此前缀异或加入到Trie树中,

    2.2.根据后一位的后缀异或,在Trie树中查找与之异或后的最大值,并一直更新ans。



    学习之处:

    当需要在一个序列的中间删除若干个连续元素,使得满足xx条件时:

    1.预处理出前缀和、后缀和。

    2.枚举每一个前缀和:将此前缀和插入某种数据结构中,再用后一位的后缀和在此数据结构中查找。

    类似的题目:http://blog.csdn.net/dolfamingo/article/details/71001021


    代码如下:

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 typedef long long LL;
     4 const double eps = 1e-6;
     5 const int INF = 2e9;
     6 const LL LNF = 9e18;
     7 const int mod = 1e9+7;
     8 const int maxn = 1e5+10;
     9 
    10 typedef struct node
    11 {
    12     struct node *next[2];
    13 }Node, *Trie;
    14 
    15 Trie T;
    16 LL n, a[maxn], pre[maxn], rev[maxn];
    17 
    18 void init()
    19 {
    20     scanf("%I64d",&n);
    21     for(int i = 1; i<=n; i++)
    22         scanf("%I64d",&a[i]);
    23     for(int i = 1; i<=n; i++)   //前缀
    24         pre[i] = pre[i-1]^a[i];
    25     for(int i = n; i>=1; i--)   //后缀
    26         rev[i] = rev[i+1]^a[i];
    27 
    28     T = new Node;   //初始化Trie树
    29     T->next[0] = T->next[1] = NULL;
    30 }
    31 
    32 void add(LL x)
    33 {
    34     Trie p = T;
    35     for(int i = 50; i>=0; i--)  //从高位到低位
    36     {
    37         int d = (x>>i)&1;
    38         if(p->next[d]==NULL)    //此位的d不存在, 则新建
    39             p->next[d] = new Node, p->next[d]->next[0] = p->next[d]->next[1] = NULL;
    40         p = p->next[d];
    41     }
    42 }
    43 
    44 LL query(LL x)
    45 {
    46     LL tmp = 0;
    47     Trie p = T;
    48     for(int i = 50; i>=0; i--)
    49     {
    50         //如果!d路存在,则可加上(d ^ !d = 1), 并顺着这条路走下去; 否则走d路(!d路和d路至少一路存在)
    51         int d = (x>>i)&1;
    52         if(p->next[!d]) tmp += 1LL*(1LL<<i), p = p->next[!d];
    53         else p = p->next[d];
    54     }
    55     return tmp;
    56 }
    57 
    58 void solve()
    59 {
    60     LL ans = 0;
    61     for(int i = 0; i<=n; i++) //i为0时, 没有前缀;i为n时,没有后缀。
    62     {
    63         add(pre[i]);
    64         ans = max( ans, query(rev[i+1]) );
    65     }
    66     printf("%I64d
    ",ans);
    67 }
    68 
    69 int main()
    70 {
    71     init();
    72     solve();
    73 }
    View Code
  • 相关阅读:
    CentOS 7 上安装vim(默认未安装)
    SecureCRT8.3+SecureCRT_keygen完成注册
    vi/vim编辑器使用方法详解
    linux系统中如何进入退出vim编辑器,方法及区别
    centos7配置IP地址
    Linux入门基础教程
    【PHP 基础类库】Prototype 原型版教学文章!
    【PHP 模板引擎】Prototype 原型版发布!
    【JavaScript 封装库】BETA 5.0 测试版发布!
    【JavaScript 封装库】Prototype 原型版发布!
  • 原文地址:https://www.cnblogs.com/DOLFAMINGO/p/7538668.html
Copyright © 2020-2023  润新知