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. [svc]ip routing和no ip routing

    ip routing: 查路由表, 如果ping的目的在RT中没有,不发出任何包(arp也不会发出) 如果RT中存在,则arp 下一跳,相当于no ip routing+配置网关 注: 静态路由: 指 ...

  2. 【ecshop后台详解】系统设置-商店设置

    商店设置是我们ecshop新用户第一步先要设置的地方,因为里面相当于网站的基础.包括公司名称,电话,地址,tittle等重要的信息都是这里修改,如果这里没有修改的话,如果有访客来到你网站可能以为走错了 ...

  3. UIViewController的生命周期及iOS程序执行顺序 和ios6 处理内存警告

    当一个视图控制器被创建,并在屏幕上显示的时候. 代码的执行顺序1. alloc                                   创建对象,分配空间2.init (initWithN ...

  4. linux下编译 静态库、动态库,动态库依赖静态库

    xx.o : xx.h xx.cpp -lstdc++ -o xx.o -c xx.cpp -I ./ libxx.a : xx.o ar -crv libxx.a xx.o libTest.so : ...

  5. mysql学习笔记1---mysql ERROR 1045 (28000): 错误解决办法

    mysql ERROR 1045 (28000): 错误解决办法 在启动mysql服务后,登陆mysql的窗口的时候,执行mysql命令,结果报错,没法登陆.   (在安装mysql和配置的时候,我是 ...

  6. java POi excel 写入大批量数据

    直接贴代码: package jp.co.misumi.mdm.batch.common.jobrunner; import java.io.File; import java.io.FileNotF ...

  7. RabbitMQ之任务队列【译】

    在第一个教程里面,我们写了一个程序从一个有名字的队列中发送和接收消息,在这里我们将要创建一个分发耗时任务给多个worker的任务队列. 任务队列核心思想就是避免执行一个资源密集型的任务,而程序要等待其 ...

  8. Linux开机启动文件rc.local无法执行怎么办?

    rc.local是Linux系统中的一个重要的开机启动文件,每次开机都要执行这个文件.但是有一些用户的Linux系统无法执行这个文件,并导致了一系列的问题.遇到这个问题我们应该怎么办呢? 在Linux ...

  9. Amazon EC2云端服务器的使用方法

    Amazon的EC2服务器可以理解为虚拟机,不过它是不需要安装系统的,它是根据镜像自动创建的.在申请EC2的时候,可以选择操作系统的类型,如Redhat Enterprise 6或ubuntu 12等 ...

  10. UASCO Wormholes 解析 and C 语言实现

    题目大意: 农场有N个洞(2<=N<=12,N为偶数),且组成N/2个连接对.每一个洞的给出平面坐标(x,y). 假设A和B相连,则进入A会从B出来,进入B会从A出来.有仅仅会沿着x轴正方 ...