HDU 5536 字典树

题意:就是公式。
这现场赛O(n^3)能过,觉得太没天理了。
做法:字典树,枚举两个数,然后在字典树上贪心的跑。
#include <bits/stdc++.h>
using namespace std;
const int MAXN = ;
struct Trie {
int ch[],size;
}T[MAXN];
int root = ,tot = ;
void Insert(int x) {
int o = root;
T[o].size++;
for(int k = ; k >=; k--) {
int c;
if(x&(<<k)) c = ;
else c = ;
if(!T[o].ch[c]) T[o].ch[c] = ++tot;
o = T[o].ch[c];
T[o].size++;
}
}
void Delete(int x) {
int o = root;
T[o].size--;
for(int k = ; k >=; k--) {
int c;
if(x&(<<k)) c = ;
else c = ;
o = T[o].ch[c];
T[o].size--;
}
}
int Query(int x) {
int o = root;
for(int k = ; k >=; k--) {
int c;
if(x&(<<k)) c = ;
else c = ;
if(c==) {
if(T[o].ch[]&&T[T[o].ch[]].size) o = T[o].ch[];
else o = T[o].ch[],x^=(<<k);
}
else {
if(T[o].ch[]&&T[T[o].ch[]].size) o = T[o].ch[],x^=(<<k);
else o = T[o].ch[];
}
}
return x;
}
int a[MAXN];
int main()
{
//freopen("in.txt","r",stdin);
int T_T,n;
scanf("%d",&T_T);
while(T_T--) {
scanf("%d",&n);
int ans = ;
for(int i = ; i <= n; i++) scanf("%d",&a[i]);
for(int i = ; i <= n; i++)
Insert(a[i]);
for(int i = ; i <= n; i++) {
Delete(a[i]);
for(int j = i+; j <= n; j++) {
Delete(a[j]);
ans = max(ans,Query(a[i]+a[j]));
Insert(a[j]);
}
Insert(a[i]);
}
printf("%d\n",ans);
for(int i = ; i<=tot; i++) T[i].ch[] = T[i].ch[] = T[i].size = ;
tot = ;
}
return ;
}
HDU 5536 字典树的更多相关文章
- Chip Factory HDU - 5536 字典树(删除节点|增加节点)
题意: t组样例,对于每一组样例第一行输入一个n,下面在输入n个数 你需要从这n个数里面找出来三个数(设为x,y,z),找出来(x+y)^z(同样也可以(y+z)^1)的最大值 ("^&qu ...
- HDU 5687 字典树插入查找删除
题目:http://acm.hdu.edu.cn/showproblem.php?pid=5687 2016百度之星资格赛C题,直接套用字典树,顺便巩固了一下自己对字典树的理解 #include< ...
- HDU 5384 字典树、AC自动机
题目:http://acm.hdu.edu.cn/showproblem.php?pid=5384 用字典树.AC自动机两种做法都可以做 #include<stdio.h> #includ ...
- hdu 2112(字典树+最短路)
HDU Today Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- hdu 2072(字典树模板,set,map均可做)
地址:http://acm.hdu.edu.cn/showproblem.php?pid=2072 lily的好朋友xiaoou333最近很空,他想了一件没有什么意义的事情,就是统计一篇文章里不同单词 ...
- hdu 1251 字典树的应用
这道题看了大神的模板,直接用字典树提交的会爆内存,用stl 里的map有简单有快 #include <iostream> #include <map> #include < ...
- hdu 2896 字典树解法
#include <iostream> #include <cstring> #include <cstdio> #include <cstdlib> ...
- Repository HDU - 2846 字典树
题意:给出很多很多很多很多个 单词 类似搜索引擎一下 输入一个单词 判断有一个字符串包含这个单词 思路:字典树变体,把每个单词的后缀都扔字典树里面,这里要注意dd是一个单词 但是把d 和dd都放字典树 ...
- Phone List HDU - 1671 字典树
题意:给出一堆一组一组的数字 判断有没有哪一个是另外一个的前缀 思路:字典树 插入的同时进行判断 不过 当处理一组数字的时候 需要考虑的有两点1.是否包含了其他的序列2.是否被其他序列包含 刚开始 ...
随机推荐
- git撤销commit,但未git push的命令
在git push的时候,有时候我们会想办法撤销git commit的内容 1.找到之前提交的git commit的id git log 找到想要撤销的id 2.git reset –hard id ...
- 2.rabbitmq 系列教程
rabbitmq系列教程-文章[转] 视频分享: 链接:https://pan.baidu.com/s/1s_Qr2A1o0s8Ru0exK62jqg 提取码:eb68
- 转 .net数据类型
.net 数据类型 short s=0; s = s + 1; 和short s=0; s += 1; 这两个表达式有什么区别,会报什么错误? 有区别吗??大家要想想呢还是要测试一下啊,我选择测试 ...
- jquery将日期转换成指定格式的字符串
引用jquery文件,如<script type="text/javascript" src="jquery-1.8.3.min.js"></ ...
- 2019.03.28 读书笔记 关于try catch
try catch 在不异常的时候不损耗性能,耗损性能的是throw ex,所以在非异常是,不要滥用throw,特别是很多代码习惯:if(age<0) throw new Exception(& ...
- C# 操作字符串
//(1)字符访问(下标访问s[i]) s ="ABCD"; Console.WriteLine(s[0]); // 输出"A ...
- C#动态方法调用 提高程序的扩展性
此篇将介绍C#如何在运行时动态调用方法.当某些类型是运行时动态确定时,编译时的静态编码是无法解决这些动态对象或类的方法调用的.此篇则给你一把利剑,让动态对象的方法调用成为可能. 1.动态调用dll里的 ...
- java输出九九乘法口诀表
使用双重for循环输出九九乘法口诀表 public static void main(String[] args){ formula();} /** * for 循环实现9*9乘法口诀表 * &quo ...
- Spring-boot2.0.1.BUILD-SNAPSHOT整合Elasticsearch报failed to load elasticsearch nodes错误解决办法
spring-boot整合es的application.properties的默认配置为: spring.data.elasticsearch.cluster-nodes=localhost:9200 ...
- ssh整合(spring + struts2 + hibernate)xml版
1.1分层 1.2jar节点 <dependencies> <dependency> <groupId>junit</groupId> <arti ...