Atcoder Tenka1 Programmer Contest D: IntegerotS 【思维题,位运算】
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;
}
最新文章
- spring源码解析——spring源码导入eclipse
- mysql 触发器示例和注解
- Oracle转MySQL
- python requests 模块初探
- Android缓存学习入门(二)
- poj1700
- Struts2+Spring3+Mybatis3开发环境搭建
- InnoTop
- ibatis实战之基础环境搭建
- dos 命令集
- 8. Andr&#233;nalin ★ Serial
- 关于Spring总结
- Ubuntu 发行版的 Linux 操作系统
- Cocos2d-X使用CCAnimation创建动画
- 五十七、linux 编程——UDP 编程 域名解析
- JVM方法调用过程
- ES6学习路上的小学生,promise处理异步操作,简易原始起步之用。先能用,再深究!
- position inherit 定位
- c# LINQ 使用
- Arbitrage HDU1217
热门文章
- Django项目:CRM(客户关系管理系统)--29--21PerfectCRM实现King_admin查看页面美化
- SSM7-nginx的反向代理和负载均衡
- PhpExcel参考网址
- JavaScript中this的指向2(转载)
- HDU 4584
- poj 3468 A Simple Problem with Integers (线段树区间更新求和lazy思想)
- 【linux配置】Linux同步网络时间
- fastjson 对象和json互转
- Leetcode16.3Sum Closest最接近的三数之和
- python枚举详解