http://tenka1-2017.contest.atcoder.jp/tasks/tenka1_2017_d

给定N,K和A1...AN,B1...BN,选取若干个Ai使它们的或运算值小于等于K时,使得对应的ΣBi值最大,求最大值。

其实我不大理解为什么要这么弄。

一个数K,如果X小于它,那么K的二进制中第r位是1,X的第r位可以是0或1;但如果K的第r位是0,X的第r位一定是0。

我只能勉强这样想:

  可以先选定一个或运算值的上限tmp,如果(Ai|tmp==tmp),那么根据或运算性质当前这个Ai是肯等可以选的,由于Bi>0,自然能选的越多越好。但是要是一个一个枚举或运算上限显然不现实。所以要按照K的二进制来枚举,把K中位是1的变为0,前面位不变,后面位全变为1。

codeforce上有人这么写:

Let's constder about the pattern of K = 13:
All of buying things are 0 (0 or 1) (0 or 1) (0 or 1) in binary-representation, 0 — 7 in decimal
All of buying things are 1 0 (0 or 1) (0 or 1) in binary-representation, 8 — 11 in decimal
All of buying things are 1 1 0 (0 or 1) in binary-representation, 12 — 13 in decimal
You can choose any pattern to buying, of above patterns.

Let's constder about an another pattern, K = 22:
All of buying things are 0 (0 or 1) (0 or 1) (0 or 1) (0 or 1) in binary-representation, 0 — 15 in decimal
All of buying things are 1 0 0 (0 or 1) (0 or 1) in binary-representation, 16 — 19 in decimal
All of buying things are 1 1 0 0 (0 or 1) in binary-representation, 20 — 21 in decimal
All of buying things are 1 1 0 0 0 in binary-representation, 22 in decimal
You can choose any pattern to buying, of above patterns.

So, you can divide [1, K] into logK parts (maximum). The complexity is N * logK = O(NlogK).

官方题解:

 #include<iostream>
#include<cstdio>
using namespace std;
typedef long long ll;
const int maxn=1e5+;
int a[maxn],b[maxn];
int fac[]; int main()
{
int N,K;
scanf("%d%d",&N,&K);
ll res=;
for(int i=;i<N;i++){
scanf("%d%d",&a[i],&b[i]);
if((a[i]|K)==K) res+=b[i];
}
for(int i=;i<=;i++)
fac[i]=<<i;
for(int i=;i<=;i++){
if(K&fac[i]){
ll cnt=;
int tmp=(K^fac[i])|(fac[i]-);//把当前位置为0,前面位不变,后面位全为1
for(int j=;j<N;j++)
if((a[j]|tmp)==tmp) cnt+=b[j];
res=max(res,cnt);
}
}
cout<<res<<endl;
}

最新文章

  1. spring源码解析——spring源码导入eclipse
  2. mysql 触发器示例和注解
  3. Oracle转MySQL
  4. python requests 模块初探
  5. Android缓存学习入门(二)
  6. poj1700
  7. Struts2+Spring3+Mybatis3开发环境搭建
  8. InnoTop
  9. ibatis实战之基础环境搭建
  10. dos 命令集
  11. 8. Andr&#233;nalin ★ Serial
  12. 关于Spring总结
  13. Ubuntu 发行版的 Linux 操作系统
  14. Cocos2d-X使用CCAnimation创建动画
  15. 五十七、linux 编程——UDP 编程 域名解析
  16. JVM方法调用过程
  17. ES6学习路上的小学生,promise处理异步操作,简易原始起步之用。先能用,再深究!
  18. position inherit 定位
  19. c# LINQ 使用
  20. Arbitrage HDU1217

热门文章

  1. Django项目:CRM(客户关系管理系统)--29--21PerfectCRM实现King_admin查看页面美化
  2. SSM7-nginx的反向代理和负载均衡
  3. PhpExcel参考网址
  4. JavaScript中this的指向2(转载)
  5. HDU 4584
  6. poj 3468 A Simple Problem with Integers (线段树区间更新求和lazy思想)
  7. 【linux配置】Linux同步网络时间
  8. fastjson 对象和json互转
  9. Leetcode16.3Sum Closest最接近的三数之和
  10. python枚举详解