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安装包制作工具制作安装包
在我们开发完软件后,除了极个别案例我们把整个目录复制给客户用外,我们一般都需要做成安装包,方便整个软件的部署操作,以安装包的部署操作可能简单的是复制文件,也可能包括一些注册表.数据库等额外的操作,不过 ...
随机推荐
- Spring学习10-SpringMV核心组件2及SpringMVC项目示例
一.SpringMVC核心接口 5.ViewResolver接口--视图解析接口 继承体系: 5.View接口--视图接口 二.SpringMVC项目示例 (1)建立动态we ...
- 把一张图片 转成二进制流 用AFNetworking POST 上传到服务器.
把一张图片 转成二进制流 用AFNetworking POST 上传到服务器. AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOper ...
- Windows 8.1下安装Mac OS X 10.8虚拟机
转载自http://blog.csdn.net/jordanxinwang/article/details/43637799 1.准备 宿主操作系统:Windows 8.1 64位.特别地,需要CPU ...
- 浏览器 本地预览图片 window.url.createobjecturl
第一种方式 <script type="text/javascript"> function setImagePreview() { var docObj = docu ...
- 基于html5和jquery的篮球跳动游戏
今天给大家分享一款基于html5和jquery的篮球跳动游戏.这款实例和之前分享的HTML5重力感应小球冲撞动画类似.用鼠标拖动篮球,篮球在页面上跳动,可在演示中看下效果.效果图如下: 在线预览 ...
- 纯css3实现的鼠标悬停动画按钮
今天给大家带来一款纯css3实现的鼠标悬停动画按钮.这款按钮鼠标经过前以正方形的形式,当鼠标经过的时候以动画的形式变成圆形.效果图如下: 在线预览 源码下载 实现的代码. html代码: < ...
- PHP——分页显示的完善(加查询,用类简化sql语句)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 大数据(11) - kafka的安装与使用
一.Kafka概述 1.Kafka是什么 在流式计算中,Kafka一般用来缓存数据,Storm通过消费Kafka的数据进行计算. 1)Apache Kafka是一个开源消息系统,由Scala写成.是由 ...
- thinkphp 使用外部php或html 原理
- TF Boys (TensorFlow Boys ) 养成记(三): TensorFlow 变量共享
上次说到了 TensorFlow 从文件读取数据,这次我们来谈一谈变量共享的问题. 为什么要共享变量?我举个简单的例子:例如,当我们研究生成对抗网络GAN的时候,判别器的任务是,如果接收到的是生成器生 ...