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 【思维题,位运算】的更多相关文章

  1. Atcoder Tenka1 Programmer Contest C C - 4/N

    http://tenka1-2017.contest.atcoder.jp/tasks/tenka1_2017_c 我怀疑我是不是智障.... 本来一直的想法是能不能构造出答案,把N按奇偶分,偶数好办 ...

  2. Tenka1 Programmer Contest D - IntegerotS

    Problem Statement Seisu-ya, a store specializing in non-negative integers, sells N non-negative inte ...

  3. Atcoder Tenka1 Programmer Contest 2019

    C 签到题,f[i][0/1]表示以i结尾最后一个为白/黑的最小值,转移显然. #include<bits/stdc++.h> using namespace std; ; ]; char ...

  4. Atcoder Tenka1 Programmer Contest 2019 题解

    link 题面真简洁 qaq C Stones 最终一定是连续一段 . 加上连续一段 # .直接枚举断点记录前缀和统计即可. #include<bits/stdc++.h> #define ...

  5. Atcoder Tenka1 Programmer Contest 2019 E - Polynomial Divisors

    题意: 给出一个多项式,问有多少个质数\(p\)使得\(p\;|\;f(x)\),不管\(x\)取何值 思路: 首先所有系数的\(gcd\)的质因子都是可以的. 再考虑一个结论,如果在\(\bmod ...

  6. Atcoder Tenka1 Programmer Contest 2019 D Three Colors

    题意: 有\(n\)个石头,每个石头有权值,可以给它们染'R', 'G', 'B'三种颜色,如下定义一种染色方案为合法方案: 所有石头都染上了一种颜色 令\(R, G, B\)为染了'R', 染了'G ...

  7. Atcoder Tenka1 Programmer Contest 2019题解

    传送门 \(C\ Stones\) 最后肯定形如左边一段白+右边一段黑,枚举一下中间的断点,预处理一下前缀和就可以了 int main(){ // freopen("testdata.in& ...

  8. 【AtCoder】Tenka1 Programmer Contest 2019

    Tenka1 Programmer Contest 2019 C - Stones 题面大意:有一个01序列,改变一个位置上的值花费1,问变成没有0在1右边的序列花费最少多少 直接枚举前i个都变成0即 ...

  9. Tenka1 Programmer Contest D - Crossing

    链接 Tenka1 Programmer Contest D - Crossing 给定\(n\),要求构造\(k\)个集合\({S_k}\),使得\(1\)到\(n\)中每个元素均在集合中出现两次, ...

随机推荐

  1. Leetcode485.Max Consecutive Ones最大连续1的个数

    给定一个二进制数组, 计算其中最大连续1的个数. 示例 1: 输入: [1,1,0,1,1,1] 输出: 3 解释: 开头的两位和最后的三位都是连续1,所以最大连续1的个数是 3. 注意: 输入的数组 ...

  2. 15分钟构建超低成本数据大屏:DataV + DLA

    第一步:准备低成本存储的业务数据和DLA表 OSS(https://www.aliyun.com/product/oss)是云上低成本数据存储的优选方案 DLA(https://www.aliyun. ...

  3. @ font-face 引入本地字体文件

    @font-face { font-family: DeliciousRoman; src: url('…/Delicious-Roman.otf'); font-stretch: condensed ...

  4. node 和npm环境安装

    node 安装 1.下载node二进制文件 [root@baolin-images#>> ~]#wget https://nodejs.org/dist/v10.16.0/node-v10 ...

  5. Wireless Network POJ - 2236 (并查集)

    #include<iostream> #include<vector> #include<string> #include<cmath> #includ ...

  6. hibernate 注解使用

    实体类声明,需要引用 import javax.persistence.Entity; import javax.persistence.Table; @Entity @Table(name=&quo ...

  7. 【python自动化学习笔记】

    [python自动化第一篇:python介绍与入门] [python自动化第二篇:python入门] [python自动化第三篇:python入门进阶]      [Python自动化第三篇(2):文 ...

  8. mysql创建数据库指定utf8编码

    CREATE DATABASE IF NOT EXISTS dbname DEFAULT CHARSET utf8;

  9. python实例 函数

    #! /usr/bin/python # -*- coding: utf8 -*- def sum(a,b):     return a+b func = sum r = func(5,6) prin ...

  10. go strcut 封装

    package model import "fmt" type person struct { Name string age int //其它包不能直接访问.. sal floa ...