玲珑学院OJ 1028 - Bob and Alice are playing numbers 字典树,dp
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的更多相关文章
- 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 ...
- 玲珑学院oj 1152 概率dp
1152 - Expected value of the expression Time Limit:2s Memory Limit:128MByte Submissions:128Solved:63 ...
- 玲珑学院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 ...
- Light OJ 1114 Easily Readable 字典树
题目来源:Light OJ 1114 Easily Readable 题意:求一个句子有多少种组成方案 仅仅要满足每一个单词的首尾字符一样 中间顺序能够变化 思路:每一个单词除了首尾 中间的字符排序 ...
- 玲珑oj 1028 贪心
http://www.ifrog.cc/acm/problem/1028 很有趣的一道题,求从n个数里挑出不同的两个,使得他俩'|','&','^'后的值尽量大,求这个最大的结果. 求最大的异 ...
- Light OJ 1028 - Trailing Zeroes (I) (数学-因子个数)
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1028 题目大意:n除了1有多少个因子(包括他本身) 解题思路:对于n的每个因子 ...
- 九度OJ 1028:继续畅通工程 (最小生成树)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:3140 解决:1338 题目描述: 省政府"畅通工程"的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有 ...
- [Lonlife1031]Bob and Alice are eating food(递推,矩阵快速幂)
题目链接:http://www.ifrog.cc/acm/problem/1031 题意:6个水果中挑出n个,使得其中2个水果个数必须是偶数,问有多少种选择方法. 设中0代表偶数,1代表奇数.分别代表 ...
- 玲珑学院-ACM比赛1014 - Absolute Defeat
1014 - Absolute Defeat Time Limit:2s Memory Limit:64MByte Submissions:257Solved:73 DESCRIPTION Eric ...
随机推荐
- Spring中注解注入bean和配置文件注入bean
注解的方式确实比手动写xml文件注入要方便快捷很多,省去了很多不必要的时间去写xml文件 按以往要注入bean的时候,需要去配置一个xml,当然也可以直接扫描包体,用xml注入bean有以下方法: & ...
- 基于Redis的三种分布式爬虫策略
前言: 爬虫是偏IO型的任务,分布式爬虫的实现难度比分布式计算和分布式存储简单得多. 个人以为分布式爬虫需要考虑的点主要有以下几个: 爬虫任务的统一调度 爬虫任务的统一去重 存储问题 速度问题 足够“ ...
- 零基础入门学习Python(33)--异常处理:你不可能总是对的2
知识点 异常处理 捕捉异常可以使用try/except语句. try/except语句用来检测try语句块中的错误,从而让except语句捕获异常信息并处理. 如果你不想在异常发生时结束你的程序,只需 ...
- 搭建分布式yarn
1.在前一篇准备好Hadoop的基础上配置,链接 http://www.cnblogs.com/cici20166/p/6266367.html 2./etc/profile 配置环境变量 expor ...
- MySQL-----多对多
多对多: 示例1: 用户表和相亲记录表 用户表 用户id 用户名 性别 1 George 男 2 Elizabeth 女 3 Bruce 男 4 Catherine ...
- 在jupyter notebook 中编辑公式
jupyter notebook是一个python的交互式开发环境,广泛应用于数据分析的场景下. 在jupyter notebook中,还可以很方便的编辑数学公式. 1.Markdown状态 编辑公式 ...
- ROS 笔记 程序包/节点/topic
官方教程: wiki.ros.org/cn/ROS/tutorials 程序包打创建于编译 创建程序包 在工作空间的src底下,输入如下命令: $ catkin_create_pkg 要创建的包名 依 ...
- android framework navigationbar自定义
需要实现的目标:在navigationbar上显示录像预览,并且点击按钮可以显示/隐藏NavigationBar 参考文章: http://blog.csdn.net/yanlai20/article ...
- Linux下汇编语言学习笔记46 ---
这是17年暑假学习Linux汇编语言的笔记记录,参考书目为清华大学出版社 Jeff Duntemann著 梁晓辉译<汇编语言基于Linux环境>的书,喜欢看原版书的同学可以看<Ass ...
- 利用mysql分析小规模数据
1 获取数据 示例:(/home/work/data/1.data) 123457,chenli,70 123458,liuyang,71 2 create table CREATE TABLE sc ...