题目传送门

题意:从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)的更多相关文章

  1. Trie UVALive 7192 Chip Factory (15长春J)

    题目传送门 题意:从n个数中选出不同的三个数a b c,使得(a+b)^c 最大 分析:先将所有数字按位插入到字典树上,然后删除两个数字,贪心询问与剩下的数字最大异或值. /************* ...

  2. HDU 5536/ 2015长春区域 J.Chip Factory Trie

    Chip Factory Problem Description John is a manager of a CPU chip factory, the factory produces lots ...

  3. 2015ACM/ICPC亚洲区长春站 J hdu 5536 Chip Factory

    Chip Factory Time Limit: 18000/9000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)T ...

  4. 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] ...

  5. hdu 5536 Chip Factory (01 Trie)

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=5536 题面; Chip Factory Time Limit: 18000/9000 MS (Java/O ...

  6. 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 ...

  7. HDU 5536 Chip Factory Trie

    题意: 给出\(n(3 \leq n \leq 1000)\)个数字,求\(max(s_i+s_j) \bigoplus s_k\),而且\(i,j,k\)互不相等. 分析: 把每个数字看成一个\(0 ...

  8. hdu5269 Chip Factory

    地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=5536 题目: Chip Factory Time Limit: 18000/9000 MS ( ...

  9. HDU 5536 Chip Factory

    Chip Factory Time Limit: 18000/9000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)T ...

随机推荐

  1. DOM0级事件处理和DOM2级事件处理

    转自:http://www.cnblogs.com/holyson/p/3914406.html 0级DOM 分为2个:一是在标签内写onclick事件  二是在JS写onlicke=function ...

  2. Windows XP系统下添加任务计划常出现问题解决办法

    Windows XP系统下添加任务计划常出现问题解决办法 计划任务就是让电脑在指定的时间内执行指定的动作(计划动作),这些动作可以是一个程序,也可以是一个批处理,但是至少是可以运行的(通俗一些就是双击 ...

  3. C语言基础(3)-二进制、八进制、十六进制

    1.二进制 一个位只能表示0或者1两种状态,简称bit(比特) 一个字节为8个二进制数,称为8位,简称BYTE(字节) 一个字为2个字节,简称WORD. 两个字为双字,简称DWORD,占32个bit ...

  4. 守护进程demon.c

    1 #include <stdio.h> #include <unistd.h> #include <errno.h> #include <time.h> ...

  5. 微信安卓版下载 Android微信各版本列表

    前面ytkah弄了一个iso微信各版本列表,现在就来整一个微信 for Android各版本列表,方便大伙下载.每个版本都放出一些新的功能或修复相关错误,详情可以点击下面的版本链接进行查看.资源收集于 ...

  6. mount -t nfs 的使用

    服务安装:1. 在VMware Ubuntu中安装NFS服务: sudo apt-get install nfs-kernel-server2. 安装成功会出现配置文件/etc/exports. ls ...

  7. HTTP的报文与状态码

    本文是<HTTP权威指南>的读书笔记 HTTP报文是简单的格式化数据块.每条报文都包含一条来自客户端的请求或一条来自服务器的响应.它们由三部分组成: 对报文进行描述的起始行(start l ...

  8. css pre标签

    浏览器:firfox49.0.2 在使用<pre>标签输出格式化文本的时候,遇到了一个小问题. 要在页面的底部输出两行文本,但是最后一行的文字总是距离屏幕的底部太大.下面图中的样子: 相关 ...

  9. MySql: 忘记root密码

    win7 + mysql 5.6.35 C:\Windows\system32>mysql --versionmysql Ver 14.14 Distrib 5.6.35, for Win64 ...

  10. C语言 链表排序

    #include <stdio.h> #include <stdlib.h> #include <assert.h> typedef struct node{ in ...