Chip Factory

Time Limit: 18000/9000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 3702    Accepted Submission(s): 1622

Problem Description
John is a manager of a CPU chip factory, the factory produces lots of chips everyday. To manage large amounts of products, every processor has a serial number. More specifically, the factory produces n chips today, the i-th chip produced this day has a serial number si.

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:

maxi,j,k(si+sj)⊕sk

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 input contains an integer T indicating the total number of test cases.

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
For each test case, please output an integer indicating the checksum number in a line.
 
Sample Input
2
3
1 2 3
3
100 200 300
 
Sample Output
6
400
 
Source
 
  • 题中存在XOR运算,往二进制运算考虑
  • 好吧,这题时间宽泛,n3复杂度8000ms可以过
  • 考虑对于一个正整数的二进制形式的01字串A
  • 如果用01字串B和A做XOR运算,考虑最优情况,应该是对应位置Ai!=Bi
  • 那么如果我们n2枚举Si+Sj,接下来如果有一个可以反映剩下数的每个位置01状态的数据结构,我们就可以贪心地在每一步挑选最优策略
  • 原始的十进制整数下的整数状态是不可能的,但是我们可以以二进制的形式对原始整数做tire就可以达到对剩余整数的一个数据压缩的效果
  • 方法出来了,就是做二进制字典树,然后n2枚举和,贪心搜索tire
  • 这里对于tire的建立最好是从高位开始建,原因嘛,恩本鶸从低位建树WA了,改成高位建树就AC了
 #include <iostream>
#include <string>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <climits>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
using namespace std;
typedef long long LL ;
typedef unsigned long long ULL ;
const int maxn = 1e6 + ;
const int inf = 0x3f3f3f3f ;
const int npos = - ;
const int mod = 1e9 + ;
const int mxx = + ;
const double eps = 1e- ;
const double PI = acos(-1.0) ; struct node{
int cnt;
int go[];
LL val;
};
node state[maxn<<];
int tot, root;
void add(LL w){
int cur=root;
state[cur].cnt++;
for(int i=;i>=;i--){
int k=(<<i)&w?:;
if(!state[cur].go[k]){
tot++;
state[cur].go[k]=tot;
memset(&state[tot],,sizeof(state[tot]));
}
cur=state[cur].go[k];
state[cur].cnt++;
}
state[cur].val=w;
}
void del(LL w){
int cur=root;
state[cur].cnt--;
for(int i=;i>=;i--){
int k=(<<i)&w?:;
cur=state[cur].go[k];
state[cur].cnt--;
}
}
LL search(LL w){
int cur=root;
for(int i=;i>=;i--){
int k=(<<i)&w?:;
if(state[state[cur].go[k^]].cnt){
cur=state[cur].go[k^];
}else{
cur=state[cur].go[k];
}
}
return w^state[cur].val;
}
int T, n;
LL a[maxn], ans;
int main(){
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
while(~scanf("%d",&T)){
while(T--){
root=;
tot=;
ans=-1LL;
memset(&state[],,sizeof(state[]));
memset(&state[root],,sizeof(state[root]));
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%lld",&a[i]);
add(a[i]);
}
for(int i=;i<=n;i++)
for(int j=i+;j<=n;j++){
del(a[i]);
del(a[j]);
ans=max(ans,search(a[i]+a[j]));
add(a[i]);
add(a[j]);
}
printf("%lld\n",ans);
}
}
return ;
}

HDU_5536_Chip Factory的更多相关文章

  1. PHP设计模式(三)抽象工厂模式(Abstract Factory For PHP)

    一.什么是抽象工厂模式 抽象工厂模式的用意为:给客户端提供一个接口,可以创建多个产品族中的产品对象 ,而且使用抽象工厂模式还要满足以下条件: 系统中有多个产品族,而系统一次只可能消费其中一族产品. 同 ...

  2. PHP设计模式(一)简单工厂模式 (Simple Factory For PHP)

    最近天气变化无常,身为程序猿的寡人!~终究难耐天气的挑战,病倒了,果然,程序猿还需多保养自己的身体,有句话这么说:一生只有两件事能报复你:不够努力的辜负和过度消耗身体的后患.话不多说,开始吧. 一.什 ...

  3. 菜鸟理解的工厂模式(Factory Pattern)是什么样子的?

    直接开始说了,不浪费园友宝贵的时间! 什么是工厂模式? 在学习前,先问一下:"它是什么?". 工厂模式,它是项目里面常用的设计模式之一. 它是属于创建型模式,简单的理解创建型模式就 ...

  4. Angular Service和Factory应用的区别

    Service可以用来将返回同类业务的多种返回值 Factory可以用来提供对同类业务的多个方法的调用 另外:Provider可以用来封装各独立职责

  5. 【解决方案】 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userHandler': Injection of resource dependencies failed;

    一个错误会浪费好多青春绳命 鉴于此,为了不让大家也走弯路,分享解决方案. [错误代码提示] StandardWrapper.Throwableorg.springframework.beans.fac ...

  6. Design Patterns Simplified - Part 3 (Simple Factory)【设计模式简述--第三部分(简单工厂)】

    原文链接:http://www.c-sharpcorner.com/UploadFile/19b1bd/design-patterns-simplified-part3-factory/ Design ...

  7. C#设计模式之简单工厂模式(Simple Factory)

    1. 概述 简单工厂模式就是将一个类的实例化交给一个静态工厂来执行. 2. 使用频率 中 3. 模式结构 3.1 机构图 3.2 模式中的角色 Product:抽象类,把具体产品类公共的代码进行抽象和 ...

  8. Task.Factory.StartNew的用法

    代码: private void button5_Click(object sender, EventArgs e) { ; Task.Factory.StartNew(() => { Mess ...

  9. 使用Setup Factory安装包制作工具制作安装包

    在我们开发完软件后,除了极个别案例我们把整个目录复制给客户用外,我们一般都需要做成安装包,方便整个软件的部署操作,以安装包的部署操作可能简单的是复制文件,也可能包括一些注册表.数据库等额外的操作,不过 ...

随机推荐

  1. [转]James Bach:测试人员的角色

    [转]James Bach:测试人员的角色 2015-05-13 以前,我是个开发人员.我不喜欢这个工作,无尽的压力让我疲惫.我几乎从未感觉到自己的工作做得足够好.我从未有过真正的休息.如果我没做好, ...

  2. C++函数默认参数(转)

    在代码中使用到了函数的默认参数,在函数的定义和实现中都填写的默认参数,结果出现了错误: 代码: #ifndef FIRSTPAGE_H #define FIRSTPAGE_H #include < ...

  3. getAttribute()方法

    http://www.imooc.com/code/1587 getAttribute()方法 通过元素节点的属性名称获取属性的值. 语法: elementNode.getAttribute(name ...

  4. 单调栈poj2796

    题意:给你一段区间,需要你求出(在这段区间之类的最小值*这段区间所有元素之和)的最大值...... 例如: 6 3 1 6 4 5 2 以4为最小值,向左右延伸,6 4 5  值为60....... ...

  5. 关于锚点(anchorPoint)的一些理解

    默认来说,锚点位于图层的中点.对锚点一直不太了解是怎么移动的,有时候搞明白过不久碰到了又是懵逼,这次专门记录下来自己的一些理解,有不对的也请指教一下.就用下张图来简单的说明,每个视图的左上角锚点位置为 ...

  6. 方程式漏洞之复现window2008/win7 远程命令执行漏洞

    前几天就想写的,因为一些缘故就没写.此次是在外网环境下进行的.大家在内网中也一个样. 方法: 使用Eternalblue模块,剑测是否有漏洞然后msf生成一个dll直接反弹shell. PS:win版 ...

  7. 计算机网络——OSI、TCP/IP协议族详解

    一.OSI七层协议体系结构域TCP/IP四层体系结构对比 ISO/OSI模型,即开放式通信系统互联参考模型(Open System Interconnection Reference Model),是 ...

  8. ubuntu下更改分辨率

      在虚拟机中装了ubuntu但是没有1600*900的分辨率 第一步: xrandr -q 查看现在系统中所有的分辨率 第二步: cvt 1600 900 得到所需的更改分辨率数据 第三步:  ww ...

  9. 人脸验证算法Joint Bayesian详解及实现(Matlab)

    python http://blog.csdn.net/cyh_24/article/details/49059475 github https://github.com/johnnyconstant ...

  10. git fork同步是什么意思?

    这篇文章主要介绍了git fork同步是什么意思?fork到了哪里?有什么用?怎样用?跟clone有什么差别?本文就一一解释这些问题,须要的朋友能够參考下 官方文档:http://help.githu ...