• 问题描述
  • 农民约翰在喂奶牛的时候被另一个问题卡住了。他的所有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. MFC_2.9 使用变参函数

    使用变参函数 #include <stdio.h>​// 包含一个头文件,提供不定参数的宏#include <stdarg.h>​// 用于输出不定数量的整数值void pri ...

  2. vue之基础---组件基础

    (1)基本示例 Vue组件示例 /* 先注册组件,定义一个名为button-component的新组件 */ Vue.component('button-component',{ data:funct ...

  3. MongoDB 启动和关闭

    重启命令 service mongodb restart 启动命令 mongod -f /data/tools/mongodb/config/config.conf 必须要带配置文件才能启动 关闭命令 ...

  4. 运行容器出现docker: Error response from daemon: driver failed programming external connectivity on endpoint elegant_ptolemy (7fe85ca6bd744449ff82b81c1577d73b6821c4e51780c8238fad6aa0cb940522): (iptables fai

    运行容器时出现以下报错: docker: Error response from daemon: driver failed programming external connectivity on ...

  5. Calendar的用法

    DAY_OF_MONTH的主要作用是cal.get(DAY_OF_MONTH),用来获得这一天在是这个月的第多少天 Calendar.DAY_OF_YEAR的主要作用是cal.get(DAY_OF_Y ...

  6. thupc & cts & apio & thusc 游记 (北京17日游记)

    thupc & cts & apio & thusc 游记 (北京17日游记) Day 0 和隔壁校两人py了一下,六个人组了两队,(左哼哼)与(右哼哼),我和Camoufla ...

  7. Python学习第二阶段Day2,模块time/datetime、random、os、sys、shutil

    1.Time.  Datetime(常用) UTC时间:为世界标准时间,时区为0的时间 北京时间,UTC+8东八区 import time print(time.time()) # timestamp ...

  8. Java 十二周总结

  9. 抓包工具的感触(charles and fiddler)

    最近测mobile,一直徘徊在fiddler 和 charles之间: charles 的证书装了 ,才能正常抓包: 后来因为重定向,分享到扣扣,微信的跳转功能,跳转到wap 或者跳转到PC  或者跳 ...

  10. 什么是restful