Trie URAL 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 URAL 7192 Chip Factory (15长春J)的更多相关文章
- Trie UVALive 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 ...
随机推荐
- WinForm------GridControl单元格内容修改外表样式
private void gridView1_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDra ...
- Windows操作系统
Microsoft Windows,是美国微软公司研发的一套操作系统,它问世于1985年,起初仅仅是Microsoft-DOS模拟环境,后续的系统版本由于微软不断的更新升级,不但易用,也慢慢的成为家家 ...
- docker windows 7 mysql安装使用教程
mysql 1. 镜像的下载 Docker pull MySQL 可以从https://docker.cn/docker/mysql 看到官方的同步镜像. 大约400多M. 2. 运行 每个镜像都有一 ...
- VBA笔记(一)
开启VBA编程环境--VBE 方法一:按<Alt+F11>组合建 方法二:查看代码 宏设置 当然启用宏的设置方式不同,宏的启动方式也不一样. 首先打开"office 按钮&quo ...
- tomcat乱码原因--基本的编码问题
tomcat乱码原因:在学习servlet时候,经常会遇到中文乱码的问题,网上查只知道如何设置不乱码,其中的原理不是很明白.我认为明白其中的原理,乱码问题就很容易解决 tomcat乱码解决方法: po ...
- TopHat
What is TopHat? TopHat is a program that aligns RNA-Seq reads to a genome in order to identify exon- ...
- Ajax跨域:Jsonp原理解析
推荐先看下这篇文章:JS跨域(ajax跨域.iframe跨域)解决方法及原理详解(jsonp) JavaScript是一种在Web开发中经常使用的前端动态脚本技术.在JavaScript中,有一个很重 ...
- ThinkPHP2.2框架执行流程图,ThinkPHP控制器的执行流程
ThinkPHP2.2框架执行原理.流程图在线手册 ThinkPHP控制器的执行流程 对用户的第一次URL访问 http://<serverIp>/My/index.php/Index/s ...
- Go - 函数/方法 的 变参
变参 本质上就是一个切片.只能接收一个或多个同类型参数,且 必须放在参数列表的 尾部. func test(s string, a ...int) { fmt.Printf("%T, %v\ ...
- HMac基本介绍
基本介绍 HMAC(散列消息身份验证码: Hashed Message Authentication Code) 它不是散列函数,而是采用散列函数(MD5 or 或SHA)与共享密钥一起使用的消息身份 ...