Trie UVALive 7192 Chip Factory (15长春J)
题意:从n个数中选出不同的三个数a b c,使得(a+b)^c 最大
分析:先将所有数字按位插入到字典树上,然后删除两个数字,贪心询问与剩下的数字最大异或值。
/************************************************
* Author :Running_Time
* Created Time :2015/11/1 14:58:49
* File Name :J.cpp
************************************************/ #include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <cstdlib>
#include <ctime>
using namespace std; #define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
typedef long long ll;
const int N = 1e3 + 10;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
struct Trie {
int ch[N*30][2], sz;
int cnt[N*30];
void init(void) {
sz = 1; ch[0][0] = ch[0][1] = 0;
memset (cnt, 0, sizeof (cnt));
}
void insert(int x) {
int u = 0;
for (int c, i=30; i>=0; --i) {
c = x & (1 << i) ? 1 : 0;
if (!ch[u][c]) {
ch[sz][0] = ch[sz][1] = 0;
ch[u][c] = sz++;
}
u = ch[u][c];
cnt[u]++;
}
}
void remove(int x) {
int u = 0;
for (int c, i=30; i>=0; --i) {
c = x & (1 << i) ? 1 : 0;
u = ch[u][c];
cnt[u]--;
}
}
int query(int x) {
int u = 0;
for (int c, i=30; i>=0; --i) {
c = x & (1 << i) ? 1 : 0;
if (c == 1) {
if (ch[u][0] && cnt[ch[u][0]]) u = ch[u][0];
else u = ch[u][1], x ^= (1 << i);
}
else {
if (ch[u][1] && cnt[ch[u][1]]) u = ch[u][1], x ^= (1 << i);
else u = ch[u][0];
}
}
return x;
}
}trie;
int a[N]; int main(void) {
int T; scanf ("%d", &T);
while (T--) {
int n; scanf ("%d", &n);
for (int i=1; i<=n; ++i) {
scanf ("%d", &a[i]);
}
int ans = 0;
trie.init ();
for (int i=1; i<=n; ++i) {
trie.insert (a[i]);
}
for (int i=1; i<=n; ++i) {
trie.remove (a[i]);
for (int j=i+1; j<=n; ++j) {
trie.remove (a[j]);
ans = max (ans, trie.query (a[i] + a[j]));
trie.insert (a[j]);
}
trie.insert (a[i]);
}
printf ("%d\n", ans);
} return 0;
}
Trie UVALive 7192 Chip Factory (15长春J)的更多相关文章
- Trie URAL 7192 Chip Factory (15长春J)
题目传送门 题意:从n个数中选出不同的三个数a b c,使得(a+b)^c 最大 分析:先将所有数字按位插入到字典树上,然后删除两个数字,贪心询问与剩下的数字最大异或值. /************* ...
- HDU 5536/ 2015长春区域 J.Chip Factory Trie
Chip Factory Problem Description John is a manager of a CPU chip factory, the factory produces lots ...
- 2015ACM/ICPC亚洲区长春站 J hdu 5536 Chip Factory
Chip Factory Time Limit: 18000/9000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)T ...
- hdu5536 Chip Factory 字典树+暴力 处理异或最大 令X=(a[i]+a[j])^a[k], i,j,k都不同。求最大的X。
/** 题目:hdu5536 Chip Factory 链接:http://acm.hdu.edu.cn/showproblem.php?pid=5536 题意:给定n个数,令X=(a[i]+a[j] ...
- hdu 5536 Chip Factory (01 Trie)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=5536 题面; Chip Factory Time Limit: 18000/9000 MS (Java/O ...
- ACM Changchun 2015 J. Chip Factory
John is a manager of a CPU chip factory, the factory produces lots of chips everyday. To manage larg ...
- HDU 5536 Chip Factory Trie
题意: 给出\(n(3 \leq n \leq 1000)\)个数字,求\(max(s_i+s_j) \bigoplus s_k\),而且\(i,j,k\)互不相等. 分析: 把每个数字看成一个\(0 ...
- hdu5269 Chip Factory
地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=5536 题目: Chip Factory Time Limit: 18000/9000 MS ( ...
- HDU 5536 Chip Factory
Chip Factory Time Limit: 18000/9000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)T ...
随机推荐
- POJ1904 King's Quest
King's Quest Language:Default King's Quest Time Limit: 15000MS Memory Limit: 65536K Total Submission ...
- web攻击之二:CSRF跨站域请求伪造
CSRF是什么? (Cross Site Request Forgery, 跨站域请求伪造)是一种网络的攻击方式,它在 2007 年曾被列为互联网 20 大安全隐患之一,也被称为“One Click ...
- RESTEasy常用注解
一.@Path,标注资源类或方法的相对路径 Path参数的形式有三种: 1.固定值 2.纯正则表达式 3.固定值和正则表达式的混 ...
- Java访问子类对象的实例变量
对于Java这种语言来说,一般来说,子类可以调用父类中的非private变量,但在一些特殊情况下, Java语言可以通过父类调用子类的变量 具体的还是请按下面的例子吧! package com.yon ...
- numpy.zeros(shape, dtype=float, order='C')
numpy.zeros Return a new array of given shape and type, filled with zeros. Parameters: shape : int o ...
- jquery.html5uploader.js 上传控件
插件地址:http://blog.csdn.net/never_say_goodbye/article/details/8598521 先上个效果图: 相比来说,效果还是很不错的 使用MVC3做服务器 ...
- Mac系统的launchd、守护进程daemon(2013笔记整理)
1. launchd Mac系统下通用的进程管理器,是Mac系统下非常重要的一个进程,一般来说该进程不允许直接以命令行的形式调用.只能通过其控制管理界面,launchctl来进行控制. launchd ...
- 图像滤波与OpenCV中的图像平滑处理
.About图像滤波 频率:可以这样理解图像频率,图像中灰度的分布构成一幅图像的纹理.图像的不同本质上是灰度分布规律的不同.但是诸如"蓝色天空"样的图像有着大面积近似的灰度强度,而 ...
- R语言中的字符处理
R语言中的字符处理 (2011-07-10 22:29:48) 转载▼ 标签: r语言 字符处理 字符串 连接 分割 分类: R R的字符串处理能力还是很强大的,具体有base包的几个函数和strin ...
- 阶段2-新手上路\项目-移动物体监控系统\Sprint1-声音报警子系统开发\第1节-Sprint Backlog规划
根据之前的sprint1-声音报警子系统是相对比较大的一个需求,需要把它进一步细化,然后指定sprint Backlog product Backlog是整个产品的功能列表! sprint Backl ...