http://www.ifrog.cc/acm/problem/1028

题解处:http://www.ifrog.cc/acm/solution/4

#include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long LL;
const int N = 1e5 + ;
const double eps = 1e-;
const double INF = 1e12;
int ch[N*][],cnt[N*],tot,a[N],n,T,op,kase;
bool dp[(<<)+];
inline int newnode(){
++tot;
ch[tot][]=ch[tot][]=-;
cnt[tot]=;
return tot;
}
void insert(int x){
int ptr = ;
for(int i=;i>=;--i){
int nx = (x&(<<i))?:;
if(ch[ptr][nx]==-)
ch[ptr][nx]=newnode();
ptr = ch[ptr][nx];++cnt[ptr];
}
}
void Union(int &x,int y){
if(y==-)return;
if(x==-)x = newnode();
cnt[x]+=cnt[y];
Union(ch[x][],ch[y][]);
Union(ch[x][],ch[y][]);
}
int ask_xor(int x){
int ret=,ptr=;
for(int i=;i>=;--i){
int nx = (x&(<<i))?:;
if(ch[ptr][nx^]!=-){
ret|=(<<i);
ptr=ch[ptr][nx^];
}
else ptr=ch[ptr][nx];
if(ptr==-)break;
}
return ret;
}
int ask_and(){
int ret=,ptr=;
for(int i=;i>=;--i){
if(ch[ptr][]!=-&&cnt[ch[ptr][]]>=)
ret|=(<<i);
else Union(ch[ptr][],ch[ptr][]);
ptr=ch[ptr][];
}
return ret;
}
void ask_or(){
memset(dp,false,sizeof(dp));
for(int i=;i<=n;++i)dp[a[i]]=true;
for(int i=(<<);i>=;--i){
for(int j=;j<=;++j)
if(!(i&(<<j)))dp[i]|=dp[i|(<<j)];
}
int ret=,p=;
int tmp[];
for(int i=;i<=n;++i){
p=;
for(int j=;j>=;--j)
if(!(a[i]&(<<j)))tmp[++p]=(<<j);
int x=;
for(int j=;j<=p;++j){
if(dp[x|tmp[j]])x|=tmp[j];
}
ret=max(ret,x|a[i]);
}
printf("%d\n",ret);
}
int main(){
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&op);
tot=-;newnode();int ret_xor=;
for(int i=;i<=n;++i){
scanf("%d",&a[i]);
if(op==)ret_xor=max(ret_xor,ask_xor(a[i]));
insert(a[i]);
}
printf("Case #%d: ",++kase);
if(op==)printf("%d\n",ret_xor);
else if(op==)printf("%d\n",ask_and());
else ask_or();
}
return ;
}

玲珑学院OJ 1028 - Bob and Alice are playing numbers 字典树,dp的更多相关文章

  1. ifrog-1028 Bob and Alice are playing numbers(trie树)

    题目链接: Bob and Alice are playing numbers DESCRIPTION Bob and his girl friend are playing game togethe ...

  2. 玲珑学院oj 1152 概率dp

    1152 - Expected value of the expression Time Limit:2s Memory Limit:128MByte Submissions:128Solved:63 ...

  3. 玲珑学院OJ 1023 - Magic boy Bi Luo with his excited math problem 树状数组暴力

    分析:a^b+2(a&b)=a+b  so->a^(-b)+2(a&(-b))=a-b 然后树状数组分类讨论即可 链接:http://www.ifrog.cc/acm/probl ...

  4. Light OJ 1114 Easily Readable 字典树

    题目来源:Light OJ 1114 Easily Readable 题意:求一个句子有多少种组成方案 仅仅要满足每一个单词的首尾字符一样 中间顺序能够变化 思路:每一个单词除了首尾 中间的字符排序 ...

  5. 玲珑oj 1028 贪心

    http://www.ifrog.cc/acm/problem/1028 很有趣的一道题,求从n个数里挑出不同的两个,使得他俩'|','&','^'后的值尽量大,求这个最大的结果. 求最大的异 ...

  6. Light OJ 1028 - Trailing Zeroes (I) (数学-因子个数)

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1028 题目大意:n除了1有多少个因子(包括他本身) 解题思路:对于n的每个因子 ...

  7. 九度OJ 1028:继续畅通工程 (最小生成树)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:3140 解决:1338 题目描述:     省政府"畅通工程"的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有 ...

  8. [Lonlife1031]Bob and Alice are eating food(递推,矩阵快速幂)

    题目链接:http://www.ifrog.cc/acm/problem/1031 题意:6个水果中挑出n个,使得其中2个水果个数必须是偶数,问有多少种选择方法. 设中0代表偶数,1代表奇数.分别代表 ...

  9. 玲珑学院-ACM比赛1014 - Absolute Defeat

    1014 - Absolute Defeat Time Limit:2s Memory Limit:64MByte Submissions:257Solved:73 DESCRIPTION Eric ...

随机推荐

  1. Django, one-to-many, many-to-many

    1.定义关系 定义三个表,Publisher,Book,Author 一个作者有姓,有名及email地址. 出版商有名称,地址,所在城市.省,国家,网站. 书籍有书名和出版日期. 它有一个或多个作者( ...

  2. Spider-Python爬虫之PyQuery基本用法

    1.安装方法 pip install pyquery 2.引用方法 from pyquery import PyQuery as pq 3.简介 pyquery 是类型jquery 的一个专供pyth ...

  3. table中JS选取行列

    <table border="1" align="center" style="width: 500px" id="doct ...

  4. Maximun product

    Given a sequence of integers S = {S1, S2, ..., Sn}, you shoulddetermine what is the value of the max ...

  5. The Bells are Ringing(枚举)

    Description Perhaps you all have heard the mythical story about Tower of Hanoi (The details of this ...

  6. UVA 221 城市化地图(离散化思想)

    题意: 给出若干个栋楼俯视图的坐标和面积,求从俯视图的南面(可以视为正视图)看过去到底能看到多少栋楼. 输入第一个n说明有n栋楼,然后输入5个实数(注意是实数),分别是楼的左下角坐标(x,y), 然后 ...

  7. TCP传输的三次握手四次挥手策略

    为了准确无误地数据送达目标处,TCP协议采用了三次握手策略.用TCP协议把数据包送出去后,TCP不会对传送后的情况置之不理,它一定会向对方确认是否成功送达.握手中使用了TCP的标志:SYN和ACK 发 ...

  8. HDU 2475 Box

    Box Time Limit: 5000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 247564 ...

  9. Microsoft Excel 准确按照一页的宽度和高度打印

    设置 Microsoft Excel 准确按照一页的宽度和高度打印 Sheet1. VBA复制  With Worksheets("Sheet1").PageSetup  .Zoo ...

  10. 动态规划之最长递增子序列(LIS)

           在一个已知的序列{ a1,a2,……am}中,取出若干数组成新的序列{ ai1, ai2,…… aim},其中下标 i1,i2, ……im保持递增,即新数列中的各个数之间依旧保持原数列中 ...