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

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

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <queue>
  4. #include <cmath>
  5. #include <algorithm>
  6. using namespace std;
  7. typedef long long LL;
  8. const int N = 1e5 + ;
  9. const double eps = 1e-;
  10. const double INF = 1e12;
  11. int ch[N*][],cnt[N*],tot,a[N],n,T,op,kase;
  12. bool dp[(<<)+];
  13. inline int newnode(){
  14. ++tot;
  15. ch[tot][]=ch[tot][]=-;
  16. cnt[tot]=;
  17. return tot;
  18. }
  19. void insert(int x){
  20. int ptr = ;
  21. for(int i=;i>=;--i){
  22. int nx = (x&(<<i))?:;
  23. if(ch[ptr][nx]==-)
  24. ch[ptr][nx]=newnode();
  25. ptr = ch[ptr][nx];++cnt[ptr];
  26. }
  27. }
  28. void Union(int &x,int y){
  29. if(y==-)return;
  30. if(x==-)x = newnode();
  31. cnt[x]+=cnt[y];
  32. Union(ch[x][],ch[y][]);
  33. Union(ch[x][],ch[y][]);
  34. }
  35. int ask_xor(int x){
  36. int ret=,ptr=;
  37. for(int i=;i>=;--i){
  38. int nx = (x&(<<i))?:;
  39. if(ch[ptr][nx^]!=-){
  40. ret|=(<<i);
  41. ptr=ch[ptr][nx^];
  42. }
  43. else ptr=ch[ptr][nx];
  44. if(ptr==-)break;
  45. }
  46. return ret;
  47. }
  48. int ask_and(){
  49. int ret=,ptr=;
  50. for(int i=;i>=;--i){
  51. if(ch[ptr][]!=-&&cnt[ch[ptr][]]>=)
  52. ret|=(<<i);
  53. else Union(ch[ptr][],ch[ptr][]);
  54. ptr=ch[ptr][];
  55. }
  56. return ret;
  57. }
  58. void ask_or(){
  59. memset(dp,false,sizeof(dp));
  60. for(int i=;i<=n;++i)dp[a[i]]=true;
  61. for(int i=(<<);i>=;--i){
  62. for(int j=;j<=;++j)
  63. if(!(i&(<<j)))dp[i]|=dp[i|(<<j)];
  64. }
  65. int ret=,p=;
  66. int tmp[];
  67. for(int i=;i<=n;++i){
  68. p=;
  69. for(int j=;j>=;--j)
  70. if(!(a[i]&(<<j)))tmp[++p]=(<<j);
  71. int x=;
  72. for(int j=;j<=p;++j){
  73. if(dp[x|tmp[j]])x|=tmp[j];
  74. }
  75. ret=max(ret,x|a[i]);
  76. }
  77. printf("%d\n",ret);
  78. }
  79. int main(){
  80. scanf("%d",&T);
  81. while(T--){
  82. scanf("%d%d",&n,&op);
  83. tot=-;newnode();int ret_xor=;
  84. for(int i=;i<=n;++i){
  85. scanf("%d",&a[i]);
  86. if(op==)ret_xor=max(ret_xor,ask_xor(a[i]));
  87. insert(a[i]);
  88. }
  89. printf("Case #%d: ",++kase);
  90. if(op==)printf("%d\n",ret_xor);
  91. else if(op==)printf("%d\n",ask_and());
  92. else ask_or();
  93. }
  94. return ;
  95. }

玲珑学院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. 笔试算法题(01):字符串倒置 & 八皇后问题

    出题:将字符串“ABCD1234efgh”进行前后对调: 分析: 常见的考查指针使用的案例,知道字符串长度之后,依次交换位置i以及位置(length-1-i)上的内容,直到重叠: 注意不能直接修改指针 ...

  2. php与mysql事物处理

    PHP与MYSQL事务处理 mysql事物特性 (原子性,一致性,隔离性,持久性) /*MYSQL的事务处理主要有两种方法.1.用begin,rollback,commit来实现begin 开始一个事 ...

  3. 零基础入门学习Python(26)--字典:当索引不好用时2

    知识点 删除字典元素 能删单一的元素也能清空字典,清空只需一项操作. 显示删除一个字典用del命令,如下: >>> dict1 = {'a':1,'b':2,'c':3} >& ...

  4. Linux 搭建 squid 代理服务器 三种模式

    CentOS 6.7 squid 代理服务器 一般有两张或以上网卡,一张链接公网,访问外网资源,一张位于局域网. 代理服务器可以提供文件缓存.复制和地址过滤等服务,充分利用有限的出口带宽,加快内部主机 ...

  5. Not so Mobile (针对递归输入的函数)

      Before being an ubiquous communications gadget, a mobile was just a structure made of strings and ...

  6. Jmeter关联,正则表达式提取器使用2

    正则表达式的用处很多,最基础的用法 1,断言 2,传参(关联) 例子 1.http请求 2正则表达式提取,想要提取列表列中id,一遍打开列表页 如果是1,每次就会取相同的值!匹配数字的权限高于模板$0 ...

  7. JQuery_九大选择器

    JQuery_九大选择器-----https://blog.csdn.net/pseudonym_/article/details/76093261

  8. nyoj 96 n-1位数(处理前导 0 的情况)(string)

    n-1位数 时间限制:3000 ms  |            内存限制:65535 KB 难度:1   描述 已知w是一个大于10但不大于1000000的无符号整数,若w是n(n≥2)位的整数,则 ...

  9. 【HDOJ3341】Lost's revenge(AC自动机,DP)

    题意:给出一个n个模式串,一个目标串,问把目标串重新排位最多能产生多少个模式串,可以重叠且所有串只包含A C G T. n<=10,len[i]<=10 len(s)<=40 Cas ...

  10. mysql转oracle注意事项

    1.mysql中有自增长,oracle用新建sequence代替. 2.在实体类的id要指定相关的sequence @GeneratedValue(strategy=GenerationType.SE ...