玲珑学院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 ...
随机推荐
- 笔试算法题(01):字符串倒置 & 八皇后问题
出题:将字符串“ABCD1234efgh”进行前后对调: 分析: 常见的考查指针使用的案例,知道字符串长度之后,依次交换位置i以及位置(length-1-i)上的内容,直到重叠: 注意不能直接修改指针 ...
- php与mysql事物处理
PHP与MYSQL事务处理 mysql事物特性 (原子性,一致性,隔离性,持久性) /*MYSQL的事务处理主要有两种方法.1.用begin,rollback,commit来实现begin 开始一个事 ...
- 零基础入门学习Python(26)--字典:当索引不好用时2
知识点 删除字典元素 能删单一的元素也能清空字典,清空只需一项操作. 显示删除一个字典用del命令,如下: >>> dict1 = {'a':1,'b':2,'c':3} >& ...
- Linux 搭建 squid 代理服务器 三种模式
CentOS 6.7 squid 代理服务器 一般有两张或以上网卡,一张链接公网,访问外网资源,一张位于局域网. 代理服务器可以提供文件缓存.复制和地址过滤等服务,充分利用有限的出口带宽,加快内部主机 ...
- Not so Mobile (针对递归输入的函数)
Before being an ubiquous communications gadget, a mobile was just a structure made of strings and ...
- Jmeter关联,正则表达式提取器使用2
正则表达式的用处很多,最基础的用法 1,断言 2,传参(关联) 例子 1.http请求 2正则表达式提取,想要提取列表列中id,一遍打开列表页 如果是1,每次就会取相同的值!匹配数字的权限高于模板$0 ...
- JQuery_九大选择器
JQuery_九大选择器-----https://blog.csdn.net/pseudonym_/article/details/76093261
- nyoj 96 n-1位数(处理前导 0 的情况)(string)
n-1位数 时间限制:3000 ms | 内存限制:65535 KB 难度:1 描述 已知w是一个大于10但不大于1000000的无符号整数,若w是n(n≥2)位的整数,则 ...
- 【HDOJ3341】Lost's revenge(AC自动机,DP)
题意:给出一个n个模式串,一个目标串,问把目标串重新排位最多能产生多少个模式串,可以重叠且所有串只包含A C G T. n<=10,len[i]<=10 len(s)<=40 Cas ...
- mysql转oracle注意事项
1.mysql中有自增长,oracle用新建sequence代替. 2.在实体类的id要指定相关的sequence @GeneratedValue(strategy=GenerationType.SE ...