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;
}
Atcoder Tenka1 Programmer Contest D: IntegerotS 【思维题,位运算】的更多相关文章
- Atcoder Tenka1 Programmer Contest C C - 4/N
http://tenka1-2017.contest.atcoder.jp/tasks/tenka1_2017_c 我怀疑我是不是智障.... 本来一直的想法是能不能构造出答案,把N按奇偶分,偶数好办 ...
- Tenka1 Programmer Contest D - IntegerotS
Problem Statement Seisu-ya, a store specializing in non-negative integers, sells N non-negative inte ...
- Atcoder Tenka1 Programmer Contest 2019
C 签到题,f[i][0/1]表示以i结尾最后一个为白/黑的最小值,转移显然. #include<bits/stdc++.h> using namespace std; ; ]; char ...
- Atcoder Tenka1 Programmer Contest 2019 题解
link 题面真简洁 qaq C Stones 最终一定是连续一段 . 加上连续一段 # .直接枚举断点记录前缀和统计即可. #include<bits/stdc++.h> #define ...
- Atcoder Tenka1 Programmer Contest 2019 E - Polynomial Divisors
题意: 给出一个多项式,问有多少个质数\(p\)使得\(p\;|\;f(x)\),不管\(x\)取何值 思路: 首先所有系数的\(gcd\)的质因子都是可以的. 再考虑一个结论,如果在\(\bmod ...
- Atcoder Tenka1 Programmer Contest 2019 D Three Colors
题意: 有\(n\)个石头,每个石头有权值,可以给它们染'R', 'G', 'B'三种颜色,如下定义一种染色方案为合法方案: 所有石头都染上了一种颜色 令\(R, G, B\)为染了'R', 染了'G ...
- Atcoder Tenka1 Programmer Contest 2019题解
传送门 \(C\ Stones\) 最后肯定形如左边一段白+右边一段黑,枚举一下中间的断点,预处理一下前缀和就可以了 int main(){ // freopen("testdata.in& ...
- 【AtCoder】Tenka1 Programmer Contest 2019
Tenka1 Programmer Contest 2019 C - Stones 题面大意:有一个01序列,改变一个位置上的值花费1,问变成没有0在1右边的序列花费最少多少 直接枚举前i个都变成0即 ...
- Tenka1 Programmer Contest D - Crossing
链接 Tenka1 Programmer Contest D - Crossing 给定\(n\),要求构造\(k\)个集合\({S_k}\),使得\(1\)到\(n\)中每个元素均在集合中出现两次, ...
随机推荐
- Linux 定时任务执行 php artisan
*/ * * * * php /www/wwwroot/project/artisan command:exec postNews 5分钟执行一次
- Web应用托管服务(Web+)隐藏的十个上云最佳姿势
随着云计算浪潮的推进,技术架构云化已经成为大势所趋.特别是最近由CNCF推动的云原生概念,将符合云原生标准的各种开源技术方案推向了前所未有的高度.在这一波浪潮的推动下,越来越多的企业开始了自身的数字化 ...
- 配置android studio环境
配置java jdk 1.1运行exe 程序 1.2配置jdk 环境变量 添加环境变量 ;%JAVA_HOME%\bin;%JAVA_HOME%\jre\bin .;%JAVA_HOME%\lib;% ...
- 在C#应用中使用Common Logging日志接口
我在C#应用中一般使用log4net来记录日志,但如果项目中有个多个工程,那么没有工程都需要引用log4neg,感觉很不爽.不过今日在开spring.net的时候,看到了有个通用日志接口Common ...
- 关于python的元组操作
关于元组: 元组和列表是类似的,但是元组中的数据是不可以修改的. 元组是一对 () 元组操作: 元组是不可以修改的所以对元组的操作极少 定义空元组(因为元组一旦创建,数据不可被修改,所以极少创建空元组 ...
- 洛谷P1474 [USACO 2.3]货币系统 Money Systems [2017年4月计划 动态规划04]
P1474 货币系统 Money Systems 题目描述 母牛们不但创建了它们自己的政府而且选择了建立了自己的货币系统.由于它们特殊的思考方式,它们对货币的数值感到好奇. 传统地,一个货币系统是由1 ...
- winform应用程序异常处理
对于winform应用程序补抓异常信息,我们经常用到得try catch. 如果代码中在某个地方执行异常,但是没有加try catch,这个时候就需要做一些全局异常捕捉. 怎么做到全局异常捕捉.win ...
- 在MaxCompute中利用bitmap进行数据处理
很多数据开发者使用bitmap技术对用户数据进行编码和压缩,然后利用bitmap的与/或/非的极速处理速度,实现类似用户画像标签的人群筛选.运营分析的7日活跃等分析.本文给出了一个使用MaxCompu ...
- hdu 1358 Period(KMP入门题)
Period Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- 编码之Base64编码
Base64编码 是一种基于 64 个可打印字符来表示二进制数据的方法.目前 Base64 已经成为网络上常见的传输 8 位二进制字节代码的编码方式之一. 为什么会有 Base64 编码呢?因为有些网 ...