HDU 5536 Chip Factory (字典树)
来源:互联网 发布:铠钾网络招聘 编辑:程序博客网 时间:2024/05/29 19:38
题意:
给出1000个数
任取三个数
求max(ai+aj) xor ak
思路:
先计算出1e6个 ai + aj
将这1e3个数转化为2进制建树。由于数值小于1e9,树深小于32。
将1e6个 ai + aj 也转化为二进制,与trie树做抑或匹配。
每次匹配时先删除ai + aj 所在的链,再做匹配,匹配结束后再把链重新加入。
复杂度 1e6*32*5
常数小的话3s+ AC
代码:
写的麻烦了,其实不用转string
#include <bits/stdc++.h>using namespace std;const int MAXN=1e3+10;#define ls lson[rt]#define rs rson[rt]int a[MAXN],n,two[33];int tree[MAXN*40],lson[MAXN*40],rson[MAXN*40];char b[MAXN][MAXN][33],c[MAXN][33],str[33];int ppp;void update(int rt,int pos,int v,int k){ if(pos==32) return ; if(c[k][pos]=='1'){ if(rs==0) rs=++ppp; tree[rs]+=v; update(rs,pos+1,v,k); }else{ if(ls==0) ls=++ppp; tree[ls]+=v; update(ls,pos+1,v,k); }}void query(int i,int j,int pos,int rt){ if(pos==32) return ; if(b[i][j][pos]=='1'){ if(tree[ls]!=0){ query(i,j,pos+1,ls); }else{ b[i][j][pos]='0'; query(i,j,pos+1,rs); } }else{ if(tree[rs]!=0){ b[i][j][pos]='1'; query(i,j,pos+1,rs); }else{ query(i,j,pos+1,ls); } }}void trans(int x){ int st=32,len; str[st]='\0'; if(x!=0){ len=floor(log(x)/log(2))+1; int llen=len; while(llen--){ if(x%2) str[--st]='1'; else str[--st]='0'; x/=2; } }else{ len=1; str[--st]='0'; } while(len<32){ str[--st]='0'; len++; } return ;}int retrans(int x,int y){ int ans=0; for(int i=0;i<32;i++) if(b[x][y][i]=='1') ans+=two[i]; return ans;}int main(){ int T; scanf("%d",&T); two[31]=1; for(int i=30;i>=0;i--) two[i]=two[i+1]*2; ppp=MAXN*40-1; while(T--){ for(int i=1;i<=ppp;i++) tree[i]=lson[i]=rson[i]=0; ppp=1; scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d",&a[i]); for(int i=1;i<=n;i++){ for(int j=i+1;j<=n;j++){ trans(a[i]+a[j]); strcpy(b[i][j],str); } } for(int i=1;i<=n;i++){ trans(a[i]); strcpy(c[i],str); update(1,0,1,i); } long long fans=0; for(int i=1;i<=n;i++){ for(int j=i+1;j<=n;j++){ update(1,0,-1,i);update(1,0,-1,j); query(i,j,0,1); int temp=retrans(i,j); if(temp>fans) fans=temp; update(1,0,1,i);update(1,0,1,j); } } printf("%d\n",fans); }}
阅读全文
0 0
- HDU 5536 Chip Factory (字典树)
- hdu 5536 Chip Factory 字典树
- HDU 5536 Chip Factory 01字典树
- HDU-5536 Chip Factory(字典树)
- HDU 5536 Chip Factory(01字典树)
- hdu 5536 Chip Factory(字典树)
- HDU 5536 Chip Factory 【01字典树】
- hdu 5536 Chip Factory(字典树)
- hdu 5563 Chip Factory(字典树)
- hdu 5536 Chip Factory(水 | 字典树)
- HDU 5536 Chip Factory(暴力 or 01字典树)
- hdu 5536 Chip Factory(01字典树)
- HDU 5536 Chip Factory(暴力/01字典树)
- HDU 5536 Chip Factory (01字典树)
- HDU 5536 Chip Factory(字典树+经典应用)
- HDU-5536-Chip Factory【2015长春赛区】【字典树】
- HDU 5536 Chip Factory 带删除的01字典树
- hdu 5536 Chip Factory(01字典树带删除操作)
- Hibernate
- 智慧城市综合管控平台
- iOS 坐标变换
- C++智能指针解决内存泄漏问题
- table中合并单元格
- HDU 5536 Chip Factory (字典树)
- 单工、半双工、全双工、异步通信、同步通信
- (92)操作对象流ObjectStream、管道流PipedStream、任意流RandomStream
- rdc最佳实践之开发模式——git flow
- C陷阱与缺陷第四章 连接
- 新版Bintray下将Android Library 开源到JCenter 的正确姿势
- centos 6.5 安装mysql教程。
- linux文件操作总结(上)
- 基于Spring 的 Hibernate5的String 类型与Clob对应(适用于4.x)