HDU 5536 Chip Factory 【01字典树删除】
题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=5536
Chip Factory
Time Limit: 18000/9000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 4915 Accepted Submission(s): 2205
Problem Description
At the end of the day, he packages all the chips produced this day, and send it to wholesalers. More specially, he writes a checksum number on the package, this checksum is defined as below:
which i,j,k are three different integers between 1 and n. And ⊕ is symbol of bitwise XOR.
Can you help John calculate the checksum number of today?
Input
The first line of each test case is an integer n, indicating the number of chips produced today. The next line has n integers s1,s2,..,sn, separated with single space, indicating serial number of each chip.
1≤T≤1000
3≤n≤1000
0≤si≤109
There are at most 10 testcases with n>100
Output
题目大意:
给 N 个数,在这 N 个数里找到三个值 i, j,k 使得(i+j)⊕ k 最大,输出这个最大值。
解题思路:
每次在01字典树里删除 i 和 j 然后 (i+j)和01字典树里的匹配求最大异或值。
AC code:
#include <bits/stdc++.h>
#define ll long long int
#define INF 0x3f3f3f3f
using namespace std; const int MAXN = 1e3+;
int ch[MAXN*][];
ll value[MAXN*];
ll b[MAXN];
int num[MAXN*];
int node_cnt; void init()
{
memset(ch[], , sizeof(ch[]));
node_cnt = ;
} void Insert(ll x)
{
int cur = ;
for(int i = ; i >= ; i--){
int index = (x>>i)&;
if(!ch[cur][index]){
memset(ch[node_cnt], , sizeof(ch[node_cnt]));
ch[cur][index] = node_cnt;
value[node_cnt] = ;
num[node_cnt++] = ;
}
cur = ch[cur][index];
num[cur]++;
}
value[cur] = x;
} void update(ll x, int d)
{
int cur = ;
for(int i =; i >= ; i--){
int index = (x>>i)&;
cur = ch[cur][index];
num[cur]+=d;
}
} ll query(ll x)
{
int cur = ;
for(int i = ; i >= ; i--)
{
int index = (x>>i)&;
if(ch[cur][index^] && num[ch[cur][index^]]) cur = ch[cur][index^];
else cur = ch[cur][index];
}
return x^value[cur];
} int main()
{
int T_case, N;
scanf("%d", &T_case);
while(T_case--)
{
scanf("%d", &N);
init();
for(int i = ; i <= N; i++)
{
scanf("%lld", &b[i]);
Insert(b[i]);
}
ll ans = ;
for(int i = ; i <= N; i++){
for(int j = ; j <= N; j++){
if(i == j) continue;
update(b[i], -);
update(b[j], -);
ans = max(ans, query(b[i]+b[j]));
update(b[i], );
update(b[j], );
}
}
printf("%lld\n", ans);
}
return ;
}
最新文章
- GCD
- morse code
- C# 使用Trace记录程序日志
- silverlight Canvas、StackPanel、Grid三者之间的关系
- 最简单的方式理解Vue的自定义指令与混合
- ios auto layout demystified (一)
- web双机热备添加心跳检测ip的时候填了网关导致外网ip不能上网
- noip 2009 细胞分裂
- Contest - 2014 SWJTU ACM 手速测试赛(2014.10.31)
- 讲一个使用jquery-slick旋转木马效果插件案例
- 用CSS画一个带阴影的三角形的示例代码
- Scrum冲刺阶段7
- 20165319 2017-2018-2《Java程序设计》课程总结
- 深入理解String类详解
- Python序列化之Json基础
- byte[]->;new String(byte[]) ->; getByte()引发的不一致问题
- MySQL的体系结构图
- December 25th 2016 Week 53rd Sunday
- System.Reflection 获取描述
- Codeforces Round #222 (Div. 1) 博弈 + dp