• 问题描述
  • 农民约翰在喂奶牛的时候被另一个问题卡住了。他的所有N(1 <= N <= 100,000)个奶牛在他面前排成一行(按序号1..N的顺序),按照它们的社会等级排序。奶牛#1有最高的社会等级,奶牛#N最低。每个奶牛同时被指定了一个不唯一的附加值,这个数在0..2^21 - 1的范围内。

    帮助农民约翰找出应该从哪一头奶牛开始喂,使得从这头奶牛开始的一个连续的子序列上,奶牛的附加值的异或最大。

    如果有多个这样的子序列,选择结尾的奶牛社会等级最高的。如果还不唯一,选择最短的。

  • 输入
  • 第1行:一个单独的整数N。
    第2到N + 1行:N个0..2^21 - 1之间的整数,代表每头奶牛的被赋予的数。第j行描述了社会等级j - 1的奶牛。
  • 输出
  • 第 1 行: 3个空格隔开的整数,分别为:最大的异或值,序列的起始位置、终止位置
  • 样例输入
  • 5
    1
    0
    5
    4
    2
  • 样例输出
  • 6 4 5
  • 提示
  • 最大异或值为6,从第4个开始喂,到第5个结束。
    4 异或 2 = 6
    (100) 异或 (010) = (110)
  • 来源
  • USACO (NOCOW)

大概思路就是利用异或和的性质

维护一个前缀,找两个前缀异或值最大的就行了。

比如  sum[5]^sum[2] 的值就是  a[3]^a[4]^a[5]的值

可是  为什么 我在这里提交就能过https://ac.2333.moe/Problem/view.xhtml?id=1525

而在 这里提交 http://42.247.7.121/zh/Problem/Details/1873  死活过不了第二组数据....

/* ***********************************************
Author :guanjun
Created Time :2016/10/6 12:31:51
File Name :nk1873.cpp
************************************************ */
//#include <bits/stdc++.h>
#include <iostream>
#include <stdio.h>
#include <cstring>
#include <cmath>
#include <algorithm>
#define ull unsigned long long
#define ll long long
#define mod 90001
#define INF 0x3f3f3f3f
#define maxn 100010
#define cle(a) memset(a,0,sizeof(a))
const ull inf = 1LL << ;
const double eps=1e-;
using namespace std; bool cmp(int a,int b){
return a>b;
}
int sum[maxn];
struct node{
int next[];
int v;
void init(){
v=;
memset(next,-,sizeof next);
}
};
node L[maxn*];
int tot=;
int Max,be,ge;
char s[];
void add(char s[],int len,int num){
int now=;
for(int i=;i<len;i++){
int t=s[i]-'';
int next=L[now].next[t];
if(next==-){
next=++tot;
L[next].init();
L[now].next[t]=next;
}
now=next;
L[now].v=num;
}
L[now].v=num;
}
void query(char s[],int len,int num){
int now=;
int ans=;
for(int i=;i<len;i++){
int t=s[i]-'';
int next=L[now].next[-t];
if(next==-||L[now].v==){
next=L[now].next[t];
}
else{
ans+=pow(2.0,-i-);
}
now=next;
}
//cout<<"ans "<<ans<<endl;
if(ans>Max){
Max=ans;
be=L[now].v+;
ge=num;
}
}
void cha(int x,char *s){
for(int i=;i<=;i++){
s[i]=x%+'';
x/=;
}
s[]='\0';
}
int main()
{
//#ifndef ONLINE_JUDGE
//freopen("in.txt","r",stdin);
//#endif
//freopen("out.txt","w",stdout);
int n,x;
while(cin>>n){
L[].init();
sum[]=;
for(int i=;i<=n;i++){
scanf("%d",&x);
sum[i]=sum[i-]^x;
} Max=sum[];
cha(sum[],s);
reverse(s,s+);
add(s,,); for(int i=;i<=n;i++){
cha(sum[i],s);
reverse(s,s+);
query(s,,i);
add(s,,i);
}
printf("%d %d %d\n",Max,be,ge);
}
return ;
}

[1525] Cow Xor的更多相关文章

  1. NBUT 1525 Cow Xor(01字典树+前缀思想)

    [1525] Cow Xor 时间限制: 2000 ms 内存限制: 65535 K 问题描述 农民约翰在喂奶牛的时候被另一个问题卡住了.他的所有N(1 <= N <= 100,000)个 ...

  2. USACO 6.1 Cow XOR

    Cow XORAdrian Vladu -- 2005 Farmer John is stuck with another problem while feeding his cows. All of ...

  3. [USACO]6.1.3 cow xor(二进制+Trie)

    题意:给你一个序列(n<=100000),求出一个连续的子序列[i,j]使得ai xor ai+1 xor…… xor aj最大,求出这个最大值(其中每个数<=2^21) 分析:题目和求一 ...

  4. C Cow XOR 奶牛异或

    时间限制 : 10000 MS   空间限制 : 65536 KB 问题描述 农民约翰在喂奶牛的时候被另一个问题卡住了.他的所有N(1 <= N <= 100,000)个奶牛在他面前排成一 ...

  5. usaco6.1-Cow XOR:trie树

    Cow XOR Adrian Vladu -- 2005 Farmer John is stuck with another problem while feeding his cows. All o ...

  6. USACO 6.1 章节

    Postal Vans 题目大意 4*n的网格,要经过所有点的有向有环,不重复经过点的路径总数 n<=1000 题解 显然 插头dp 以4为切面 问题是,会发现 超精度 解决呢要么实现高精度,要 ...

  7. [LeetCode] Maximum XOR of Two Numbers in an Array 数组中异或值最大的两个数字

    Given a non-empty array of numbers, a0, a1, a2, … , an-1, where 0 ≤ ai < 231. Find the maximum re ...

  8. POJ 3278 Catch That Cow(bfs)

    传送门 Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 80273   Accepted: 25 ...

  9. 二分+DP+Trie HDOJ 5715 XOR 游戏

    题目链接 XOR 游戏 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

随机推荐

  1. 安卓app测试之Monkeyrunner

    一.MonkeyRunner简介 MonkeyRunner提供了系列的API ,MonkeyRunner可以完成模拟事件及截图操作 ,分为以下三类: MonkeyRunner:用来连接设备或模拟器的 ...

  2. 关于ORB SLAM2资源整理(持续更新)

    ORB SLAM2源码讲解(吴博) https://www.youtube.com/watch?v=2GVE7FTW7AU 泡泡机器人视频整理: http://space.bilibili.com/3 ...

  3. 后台中的sql注入

    aa.getSqlMap().put("order"," and a.id not in(\'"+po.getId()+"\')"); \' ...

  4. react antD moment

    import moment from 'moment' console.log(moment().add(1, 'days').format('YYYY-MM-DD')) //当前时间前一天 cons ...

  5. cgroup代码浅析(1)

    前置:这里使用的linux版本是4.8,x86体系. cgroup_init_early(); 聊这个函数就需要先了解cgroup. cgroup概念 这个函数就是初始化cgroup所需要的参数的.c ...

  6. 题解 洛谷P1501/BZOJ2631【[国家集训队]Tree II】

    Link-Cut-Tree 的懒标记下传正确食用方法. 我们来逐步分析每一个操作. 1:+ u v c:将u到v的路径上的点的权值都加上自然数c; 解决方法: 很显然,我们可以 split(u,v) ...

  7. [Algorithm] 11. Linked List Cycle

    Description Given a linked list, determine if it has a cycle in it. To represent a cycle in the give ...

  8. MySQL异常:com.mysql.jdbc.PacketTooBigException: Packet for query is too large

    ### Cause: com.mysql.jdbc.PacketTooBigException: Packet for query is too large (1169 > 1024). You ...

  9. git 忽略文件[.gitignore]常用配置

    .idea .buildpath .project .settings .Ds_Store composer.json composer.lock a.php /public/uploads /run ...

  10. python3支持excel读写

    1.安装setuptools-17.0.tar.gz cmd 进入命令行 cd C:\Users\vivi\Desktop\pythonforexcel\setuptools-17.0\setupto ...