usaco6.1-Cow XOR:trie树

时间:2022-01-09 17:31:09

Cow XOR
Adrian Vladu -- 2005

Farmer John is stuck with another problem while feeding his cows.
All of his N (1 ≤ N ≤ 100,000) cows (numbered 1..N) are
lined up in front of the barn, sorted by their rank in their social
hierarchy. Cow #1 has the highest rank; cow #N has the least rank.
Every cow had additionally been assigned a non-unique integer number
in the range 0..(221 - 1).

Help FJ choose which cows will be fed first by selecting a
sequence of consecutive cows in the line such that the bitwise "xor"
between their assigned numbers has the maximum value. If there are
several such sequences, choose the sequence for which its last cow
has the highest rank. If there still is a tie, choose the shortest
sequence.

TIME LIMIT: 0.5 sec

PROGRAM NAME: cowxor

INPUT FORMAT

  • Line 1: A single integer N
  • Lines 2..N+1: N integers ranging from 0 to 221 - 1, representing the cows' assigned
    numbers. Line j describes cow of social hierarchy j-1.

SAMPLE INPUT (file cowxor.in)

5
1
0
5
4
2

INPUT DETAILS:

There are 5 cows. Cow #1 had been assigned with 1; cow #2 with 0; cow #3 with 5; cow #4 with 4; cow #5 with 2.

OUTPUT FORMAT

  • Line 1: Three space-separated integers, respectively: the maximum requested value, the position where the sequence begins, the position where the sequence ends.

SAMPLE OUTPUT (file cowxor.out)

6 4 5

OUTPUT DETAILS:

4 xor 2 = 6 (001) xor (010) = (011)


题目大意:找到一个连续的区间,使各数异或之后的结果最大。

算法:用a[i]表示a[1...i]的异或结果,则任一区间的异或结果s[i...j]=a[i-1] xor a[j].

现在问题变成了,对于每一个j,我们需要找到一个i≤j,使得a[i-1] xor a[j]最大。

这时候我们可以用trie树实现这样的一个查找。

我们将a中的元素先转化成二进制字符串再插入到trie中,因此trie树中每一个节点对应一条0和一条1的边。

此时a[j]的二进制字符串对应一条从trie树的根节点走到叶子节点的路径,我们在找符合条件的a[i-1]的过程就是从根节点出发,在某一深度上尽量沿着与a[j]相反的边来走,就是说,假设a[j]的某一位是1,那a[i-1]应尽量取0(就是走0的边),不存在此边的时候再选另一边。

注意这里的数组不能开到221,usaco会MLE,实际上完全不用这么大,数据虽然很大,但是并不是每个结点都有0,1两条边。

Executing...
   Test 1: TEST OK [0.008 secs, 13748 KB]
   Test 2: TEST OK [0.005 secs, 13748 KB]
   Test 3: TEST OK [0.005 secs, 13748 KB]
   Test 4: TEST OK [0.005 secs, 13748 KB]
   Test 5: TEST OK [0.049 secs, 13748 KB]
   Test 6: TEST OK [0.124 secs, 13748 KB]
   Test 7: TEST OK [0.227 secs, 13748 KB]
   Test 8: TEST OK [0.213 secs, 13748 KB]
   Test 9: TEST OK [0.227 secs, 13748 KB]
   Test 10: TEST OK [0.230 secs, 13748 KB]
   Test 11: TEST OK [0.308 secs, 13748 KB]
   Test 12: TEST OK [0.289 secs, 13748 KB]
   Test 13: TEST OK [0.227 secs, 13748 KB]
   Test 14: TEST OK [0.221 secs, 13748 KB]
   Test 15: TEST OK [0.343 secs, 13748 KB]
   Test 16: TEST OK [0.329 secs, 13748 KB]
   Test 17: TEST OK [0.348 secs, 13748 KB]
   Test 18: TEST OK [0.221 secs, 13748 KB]
   Test 19: TEST OK [0.046 secs, 13748 KB]
   Test 20: TEST OK [0.008 secs, 13748 KB]

All tests OK.

 #include <iostream>
#include <cstring>
#include <vector>
#include <stdio.h>
#include <string>
using namespace std;
#define BIT_AT(x,n) ((x & (1<<n))>>n) const int maxnode = ;
const int sigma_size = ; // 字母表为全体小写字母的Trie
struct Trie
{
int ch[maxnode][sigma_size]; // ch[i][j]表示i节点通过字母为j的边的连接下一节点的编号,没有即为0
int val[maxnode];
int sz; // 结点总数
void clear()
{
sz = ; // 初始时只有一个根结点
memset(ch[], , sizeof(ch[]));
}
int idx(char c)
{
return c - ''; // 字符c的编号
} // 插入字符串s,附加信息为v。注意v必须非0,因为0代表“本结点不是单词结点”
// 附加信息为i,即第几个单词
void insert(const char *s, int v)
{
int u = , n = strlen(s);
for(int i = ; i < n; i++)
{
int c = idx(s[i]);
if(!ch[u][c]) // 结点不存在
{
memset(ch[sz], , sizeof(ch[sz]));
val[sz] = ; // 中间结点的附加信息为0,因此clear时不用memset()
ch[u][c] = sz++; // 新建结点
}
u = ch[u][c]; // 往下走
}
val[u] = v; // 字符串的最后一个字符的附加信息为v
} int find_min(int x,int &min_i)
{
int Min=;
int u = ;
for(int i = ; i <= ; i++)
{
int tmp=BIT_AT(x,-i);
int c = (tmp^);
if(!ch[u][c])
c=tmp;
u = ch[u][c];
Min=Min*+c;
}
min_i=val[u];
return Min;
}
};
//////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////
Trie trie;
// 将某个数的二进制转换成22位字符串
string toString(int x)
{
string str;
for(int i=;i>=;i--)
{
str.append(BIT_AT(x,i)==?"":"");
}
return str;
} int a[]={};
int b[]={}; int main()
{
freopen("cowxor.in","r",stdin);
freopen("cowxor.out","w",stdout);
int n;
cin>>n;
trie.clear();
// 初始插入0
trie.insert(toString().c_str(),); int ans=-,pos1=-,pos2=-;
for(int i=;i<=n;i++)
{
int t;
scanf("%d",&t);
b[i]=t;
a[i]=a[i-]^t;
int min_i;
int tmp=trie.find_min(a[i],min_i)^a[i];
if(ans<tmp/* || (ans==tmp && b[pos2]<t) || (ans==tmp && b[pos2]==t && pos2-pos1>i-min_i-1)*/)
{
ans=tmp;
pos1=min_i+;
pos2=i;
}
trie.insert(toString(a[i]).c_str(),i);
}
printf("%d %d %d\n",ans,pos1,pos2); return ;
}

usaco6.1-Cow XOR:trie树的更多相关文章

  1. Xor - Trie树

    题目描述 求一棵带边权的树的一条最大 Xor 路径的值.这里的"路径"不一定从根到叶子结点,中间一段路径只要满足条件也可以. 输入格式 第一行,一个整数 N ,表示一颗树有 N 个 ...

  2. &lbrack;USACO&rsqb;6&period;1&period;3 cow xor(二进制&plus;Trie)

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

  3. 51nod 1295 XOR key &lpar;可持久化Trie树)

    1295 XOR key  题目来源: HackerRank 基准时间限制:1.5 秒 空间限制:262144 KB 分值: 160 难度:6级算法题   给出一个长度为N的正整数数组A,再给出Q个查 ...

  4. 51nod 1295 XOR key &vert; 可持久化Trie树

    51nod 1295 XOR key 这也是很久以前就想做的一道板子题了--学了一点可持久化之后我终于会做这道题了! 给出一个长度为N的正整数数组A,再给出Q个查询,每个查询包括3个数,L, R, X ...

  5. HDU 5269 ZYB loves Xor I Trie树

    题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5269 bc:http://bestcoder.hdu.edu.cn/contests/con ...

  6. HDU 4825 Xor Sum (trie树处理异或)

    Xor Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others)Total S ...

  7. CodeForces979D:Kuro and GCD and XOR and SUM(Trie树&amp&semi;指针&amp&semi;Xor)

    Kuro is currently playing an educational game about numbers. The game focuses on the greatest common ...

  8. Poj 3764 The xor-longest Path&lpar;Trie树&plus;xor&plus;贪心&rpar;

    The xor-longest Path Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 6455 Accepted: 1392 ...

  9. NBUT 1525 Cow Xor(01字典树&plus;前缀思想)

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

随机推荐

  1. vuex复习方案

    这次复习vuex,发现官方vuex2.0的文档写得太简略了,有些看不懂了.然后看了看1.0的文档,感觉很不错.那以后需要复习的话,还是先看1.0的文档吧.

  2. ACM&sol;ICPC 之 BFS&lpar;离线&rpar;&plus;康拓展开 &lpar;HDU1430-魔板&rpar;

    魔板问题,一道经典的康拓展开+BFS问题,为了实现方便,我用string类来表示字符串,此前很少用string类(因为不够高效,而且相对来说我对char数组的相关函数比较熟),所以在这里也发现了很多容 ...

  3. Codeforces 543D Road Improvement(树形DP &plus; 乘法逆元)

    题目大概说给一棵树,树的边一开始都是损坏的,要修复一些边,修复完后要满足各个点到根的路径上最多只有一条坏的边,现在以各个点为根分别求出修复边的方案数,其结果模1000000007. 不难联想到这题和H ...

  4. 基于adt-bundle-windows-x86的android开发环境搭建

    0,简介: 最近简单着手了解 android 开发.工欲善其事,必先利其器. 我本人不太喜欢使用java 开发,所以简单了解了下其 c# c++都可以进行android 开发,用c++的话要使用NDK ...

  5. PHPCMS列表页伪静态

    phpcms v9内容管理系统可以方便建立网站,并且生成静态化,但是列表页往往采取伪静态,因为列表页太多每发一篇文章就生成一遍静态效率太低,phpcms列表页及分页伪静态规则如何设置呢? phpcms ...

  6. 【转】cas注册后自动登录

    本文转自:http://denger.iteye.com/blog/805743  1. 关于CAS的介绍不再累述,我想涉及过SSO同学应该都会对该框架所有了解,我们目前项目采用的CAS Server ...

  7. java二维码生成

    import java.io.File; import java.nio.file.Path; import java.util.HashMap; import com.google.zxing.Ba ...

  8. 函数防抖 &amp&semi; 函数节流

    避免一个函数频繁执行 - 避免程序卡顿 js 是单线程的,setTimeout 这样的函数是异步的 异步的代码,交给对应的模块进行处理 模块在会将异步任务,在主线程执行完所有同步代码后,加入事件队列 ...

  9. 开源框架SpringMvc和Struts2的区别

    1.机制 spring mvc 和 struts2的加载机制不同:spring mvc的入口是servlet,而struts2是filter:(servlet和filter的区别?) 2.性能 spr ...

  10. Address localhost&colon;1099 is already in use

    在 ItelliJ idea中创建了Servlet,启动tomcat时系统报错: Error running Tomcat 7.0.47: Address localhost:1099 is alre ...