hdu5536 Chip Factory
来源:互联网 发布:中信建投股票交易软件 编辑:程序博客网 时间:2024/06/15 23:06
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
whichi,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?
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
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 integern , 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 most10 testcases with n>100
The first line of each test case is an integer
There are at most
Output
For each test case, please output an integer indicating the checksum number in a line.
Sample Input
231 2 33100 200 300
Sample Output
6400
题意:给你n个数,让你找出3个不同下标对应的数,使得(s[i]+s[j])^s[k]的值最大。
思路:我们可以建一棵trie树,先把n个数都插入,然后枚举n*n个(i,j),先把trie树上的s[i],s[j]先消去,然后在树上找到异或和最大的k。我们每个节点记录一个val值。
插入时对所有经过节点的val值加1,删除就将对应节点的val值减1。在树上匹配的时候就只走那些val值为正的节点。匹配的过程中,首先看树中最高位能否异或得到1。
能的话就往能的那个方向走,否则往另外一个方向走。
插入时对所有经过节点的val值加1,删除就将对应节点的val值减1。在树上匹配的时候就只走那些val值为正的节点。匹配的过程中,首先看树中最高位能否异或得到1。
能的话就往能的那个方向走,否则往另外一个方向走。
#include<iostream>#include<stdio.h>#include<stdlib.h>#include<string.h>#include<math.h>#include<vector>#include<map>#include<set>#include<queue>#include<stack>#include<string>#include<algorithm>using namespace std;typedef long long ll;#define inf 99999999#define pi acos(-1.0)#define maxnode 100000#define maxn 1005int a[maxn];int sz,tot;int ch[maxnode][2];int val[maxnode];char d[maxn][40];void init(){ sz=0; memset(ch,0,sizeof(ch)); memset(val,0,sizeof(val));}int idx(char c){ return c-'0';}void update(char *s,int num){ int i,u=0,j; int len=strlen(s); char s1[40]; for(i=0;i<len;i++){ int c=idx(s[i]); if(!ch[u][c]){ sz++; val[sz]+=num; ch[u][c]=sz; u=sz; } else{ u=ch[u][c]; val[u]+=num; } }}int b[40],cnt;int chazhao(char *s){ int i,j,u=0; int len=strlen(s); int num=0; for(i=0;i<len;i++){ int c=idx(s[i]); if(val[ch[u][1^c ] ]){ b[i]=1; u=ch[u][1^c ]; } else{ b[i]=0; u=ch[u][c]; } num=num*2+b[i]; } return num;}int main(){ int n,m,i,j,T,t,k; char s[40]; scanf("%d",&T); while(T--) { init(); memset(s,0,sizeof(s)); scanf("%d",&n); for(i=1;i<=n;i++){ scanf("%d",&a[i]); t=a[i]; int tot=0; while(t){ s[tot++]=t%2+'0'; t>>=1; } while(tot<35){ s[tot++]='0'; } s[tot]='\0'; reverse(s,s+tot); update(s,1); strcpy(d[i],s); } int ans=0; for(i=1;i<n;i++){ update(d[i],-1); for(j=i+1;j<=n;j++){ int num=a[i]+a[j]; update(d[j],-1); tot=0; char str[40]; while(num){ str[tot++]=num%2+'0'; num>>=1; } while(tot<35){ str[tot++]='0'; } str[tot]='\0'; reverse(str,str+tot); cnt=0; int temp=chazhao(str); ans=max(ans,temp); update(d[j],1); } update(d[i],1); } printf("%d\n",ans); } return 0;}
0 0
- hdu5536 Chip Factory
- hdu5536 Chip Factory (暴力+优化)
- HDU5536 Chip Factory(字典树)
- hdu5536 Chip Factory(Tire)
- HDU5536 Chip Factory(trie树)
- HDU5536 Chip Factory 【字典树】
- HDU5536 chip factory 【01字典树】
- HDU5536 Chip Factory(01字典树)
- HDU5536:Chip Factory(字典树)
- hdu5536 Chip Factory (异或最大值)
- HDU5536 Chip Factory(Trie树,二进制)
- HDU5536 Chip Factory Trie(01字典树)
- hdu5536 Chip Factory (01字典树删除操作)
- 【HDU5536 2015长春赛区 J】【字典树or暴力】Chip Factory 字典序做法+暴力做法
- HDU5536 Chip Factory 被外表欺骗的水题 国庆咸鱼
- Chip Factory
- hdu 5536 Chip Factory
- HDU 5536 Chip Factory
- 哈希表基本操作
- 【最长公共子序列 - (nm)】
- USACO 1.2 milk2
- flash的google地图
- MongoDB学习笔记之-- MongoDB安装与配置(CentOS6.5)
- hdu5536 Chip Factory
- Android Activity生命周期整理
- flex调用webservice绑定DataGrid
- C++数据结构栈的实现
- Silverlight+wcf程序
- 哈希表入门
- nyoj283对称排序
- iOS开发的一些奇巧淫技 包括一款GIF框架
- c#编程之UDP通信