HDU_5536_Chip Factory
Chip Factory
Time Limit: 18000/9000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 3702 Accepted Submission(s): 1622
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?
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
3
1 2 3
3
100 200 300
400
- 题中存在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的更多相关文章
- PHP设计模式(三)抽象工厂模式(Abstract Factory For PHP)
一.什么是抽象工厂模式 抽象工厂模式的用意为:给客户端提供一个接口,可以创建多个产品族中的产品对象 ,而且使用抽象工厂模式还要满足以下条件: 系统中有多个产品族,而系统一次只可能消费其中一族产品. 同 ...
- PHP设计模式(一)简单工厂模式 (Simple Factory For PHP)
最近天气变化无常,身为程序猿的寡人!~终究难耐天气的挑战,病倒了,果然,程序猿还需多保养自己的身体,有句话这么说:一生只有两件事能报复你:不够努力的辜负和过度消耗身体的后患.话不多说,开始吧. 一.什 ...
- 菜鸟理解的工厂模式(Factory Pattern)是什么样子的?
直接开始说了,不浪费园友宝贵的时间! 什么是工厂模式? 在学习前,先问一下:"它是什么?". 工厂模式,它是项目里面常用的设计模式之一. 它是属于创建型模式,简单的理解创建型模式就 ...
- Angular Service和Factory应用的区别
Service可以用来将返回同类业务的多种返回值 Factory可以用来提供对同类业务的多个方法的调用 另外:Provider可以用来封装各独立职责
- 【解决方案】 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userHandler': Injection of resource dependencies failed;
一个错误会浪费好多青春绳命 鉴于此,为了不让大家也走弯路,分享解决方案. [错误代码提示] StandardWrapper.Throwableorg.springframework.beans.fac ...
- Design Patterns Simplified - Part 3 (Simple Factory)【设计模式简述--第三部分(简单工厂)】
原文链接:http://www.c-sharpcorner.com/UploadFile/19b1bd/design-patterns-simplified-part3-factory/ Design ...
- C#设计模式之简单工厂模式(Simple Factory)
1. 概述 简单工厂模式就是将一个类的实例化交给一个静态工厂来执行. 2. 使用频率 中 3. 模式结构 3.1 机构图 3.2 模式中的角色 Product:抽象类,把具体产品类公共的代码进行抽象和 ...
- Task.Factory.StartNew的用法
代码: private void button5_Click(object sender, EventArgs e) { ; Task.Factory.StartNew(() => { Mess ...
- 使用Setup Factory安装包制作工具制作安装包
在我们开发完软件后,除了极个别案例我们把整个目录复制给客户用外,我们一般都需要做成安装包,方便整个软件的部署操作,以安装包的部署操作可能简单的是复制文件,也可能包括一些注册表.数据库等额外的操作,不过 ...
随机推荐
- Docker 监控的一点想法
目前项目内部署了docker,于是涉及到关于监控的事情,参考一些经典实例以及一些自己的想法,总结一下思路. 1.关于监控的内容 监控宿主机本身 监控宿主机本身还是比较简单的,同其他服务器监控类似,对c ...
- CWidgetMgr---cpp
#include "WidgetMgr.h" #include "XWidget.h" #include "Config.h" #inclu ...
- ubuntu下刷新dns
也是一条命令就可以:sudo /etc/init.d/dns-clean start
- 使用python对mysql主从进行监控,并调用钉钉发送报警信息
1.编写python的监控脚本 A.通过获取mysql库中的状态值来判断这个mysql主从状态是否正常 B.进行两个状态值的判断 C.进行调取钉钉机器人,发送消息 2.设置定时任务进行脚本运行 cro ...
- xslt 映射 xml
1.xslt文件映射xml文件中的A节点的时候,如果A节点有属性的话,先把属性值映射出来,然后再映射节点的值,如下: xml文件: <A age="11" sex=" ...
- 跟着百度学PHP[9]-session会话
参考:http://www.w3school.com.cn/php/php_sessions.asp session变量用于存储有关用户的会话的信息,或更改用户会话的设置,session变量保存的信息 ...
- [未解决]Exception in thread "main" java.lang.IllegalArgumentException: offset (0) + length (8) exceed the capacity of the array: 6
调用这个方法 是报错,未解决 binfo.setTradeAmount(Double.parseDouble(new String(result.getValue(Bytes.toBytes(fami ...
- thinkphp 命名规范
目录和文件命名 目录和文件名采用 小写+下划线,并且以小写字母开头: 类库.函数文件统一以.php为后缀: 类的文件名均以命名空间定义,并且命名空间的路径和类库文件所在路径一致(包括大小写): 类名和 ...
- Linux环境PHP7.0.2安装
PHP7和HHVM比较PHP7的在真实场景的性能确实已经和HHVM相当, 在一些场景甚至超过了HHVM.HHVM的运维复杂, 是多线程模型, 这就代表着如果一个线程导致crash了, 那么整个服务就挂 ...
- 素数 + 背包 - SGU 116. Index of super-prime
Index of super-prime Problem's Link Mean: 如果一个素数所在的位置还是素数,那么这个素数就是超级素数,比如3在第2位置,那么3就是超级素数. 现在给你一个数,求 ...