位运算上的小技巧 - AtCoder
Problem Statement
There is an integer sequence of length 2N: A0,A1,…,A2N−1. (Note that the sequence is 0-indexed.)
For every integer K satisfying 1≤K≤2N−1, solve the following problem:
- Let i and j be integers. Find the maximum value of Ai+Aj where 0≤i<j≤2N−1 and (i or j)≤K. Here, or denotes the bitwise OR.
Constraints
- 1≤N≤18
- 1≤Ai≤109
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
A0 A1 … A2N−1
Output
Print 2N−1 lines. In the i-th line, print the answer of the problem above for K=i.
Sample Input 1
2
1 2 3 1
Sample Output 1
3
4
5
For K=1, the only possible pair of i and j is (i,j)=(0,1), so the answer is A0+A1=1+2=3.
For K=2, the possible pairs of i and j are (i,j)=(0,1),(0,2). When (i,j)=(0,2), Ai+Aj=1+3=4. This is the maximum value, so the answer is 4.
For K=3, the possible pairs of i and j are (i,j)=(0,1),(0,2),(0,3),(1,2),(1,3),(2,3) . When (i,j)=(1,2), Ai+Aj=2+3=5. This is the maximum value, so the answer is 5.
Sample Input 2
3
10 71 84 33 6 47 23 25
Sample Output 2
81
94
155
155
155
155
155
Sample Input 3
4
75 26 45 72 81 47 97 97 2 2 25 82 84 17 56 32
Sample Output 3
101
120
147
156
156
178
194
194
194
194
194
194
194
194
194
题意 : 给你 一堆数,并设定一个变量 k,要求 k 是从 1 开始递增的,每次从不超过 k 的序列中位置上取两个数 i, j, 且要求 i or j <= k
思路分析:对于一个k, 寻找小于等于 k 中
代码示例:
const int maxn = 1e6+5;
int n;
int pre[maxn];
int main() {
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout); cin >> n;
for(int i =0; i < (1<<n); i++){
scanf("%d", &pre[i]);
} int ans = 0;
for(int i = 1; i < (1<<n); i++){
int s1 = pre[0], s2 = 0;
for(int j = i; j; j = (j-1)&i){
if (pre[j] > s1) {s2 = s1, s1 = pre[j];}
else if (pre[j] > s2) s2 = pre[j];
}
ans = max(ans, s1+s2);
printf("%d\n", ans);
} return 0;
}
位运算上的小技巧 - AtCoder的更多相关文章
- ( 译、持续更新 ) JavaScript 上分小技巧(二)
考虑到文章过长,不便于阅读,这里分出第二篇,如有后续,每15个知识点分为一篇... 第一篇地址:( 译.持续更新 ) JavaScript 上分小技巧(一) 第三篇地址:( 译.持续更新 ) Java ...
- ( 译、持续更新 ) JavaScript 上分小技巧(四)
后续如有内容,本篇将会照常更新并排满15个知识点,以下是其他几篇译文的地址: 第一篇地址:( 译.持续更新 ) JavaScript 上分小技巧(一) 第二篇地址:( 译.持续更新 ) JavaScr ...
- ( 译、持续更新 ) JavaScript 上分小技巧(一)
感谢好友破狼提供的这篇好文章,也感谢写这些知识点的作者们和将他们整理到一起的作者.这是github上的一篇文章,在这里本兽也就只做翻译,由于本兽英语水平和编程能力都不咋地,如有不好的地方也请多理解体谅 ...
- ( 译、持续更新 ) JavaScript 上分小技巧(三)
最近家里杂事较多,自学时间实在少的可怜,所以都在空闲时间看看老外写的内容,学习之外顺便翻译分享~等学习的时间充足些再写写自己的一些学习内容和知识点分析(最近有在接触的:复习(C#,SQL).(学习)T ...
- hdu3006 状态压缩+位运算+hash(小想法题)
题意: 给了n个集合,问你这n个集合可以组合出多少种集合,可以自己,也可以两个,也可以三个....也可以n个集合组在一起. 思路: 是个小想法题目,要用到二进制压缩,位运算, ...
- 文件上传小技巧/原生态【html篇】
引语:大家都知道,html中上传文件就一个input,type=file就搞定了.但是,这个标签的样式,实在不值得提点什么,要改动他的样式,恐怕也是较难的.但是其实挺简单,今天就来说说上传文件小技巧吧 ...
- 文件上传小技巧/后端处理【以php示例】
引语:在上一篇文章中说到,在页面中可以用隐藏的方式让你的上传页面看起来漂亮.但是这对于性能来说,并没有什么卵用,那么在后台的处理中,难道就没有一些处理技巧么?所谓后台的技巧,应该要包括上传得快一点,上 ...
- C# 委托的一些使用上的小技巧
1.委托是一种数据类型,我们可以在任何定义类的地方定义委托,在任何声明类的地方声明委托 2.初始化委托有两种方式,代码如下: (1).像类一样初始化委托 public delegate void Sa ...
- Python语言中的按位运算
(转)位操作是程序设计中对位模式或二进制数的一元和二元操作. 在许多古老的微处理器上, 位运算比加减运算略快, 通常位运算比乘除法运算要快很多. 在现代架构中, 情况并非如此:位运算的运算速度通常与加 ...
随机推荐
- 2018-8-10-WPF-好看的矢量图标
title author date CreateTime categories WPF 好看的矢量图标 lindexi 2018-08-10 19:16:53 +0800 2018-5-16 11:4 ...
- 【codeforces 761B】Dasha and friends
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 模版——KMP
#include <iostream> #include <cstdio> #include <cstring> ; int f[maxn]; char P[max ...
- requires php ~7.1 -> your PHP version (7.0.18) does not satisfy that requirement
一个大兄弟本地用了 PHP 7.1 进行开发,而我本地是 PHP 7.0, 于是悲剧发生了. composer install 之后报错 Loading composer repositories w ...
- win10 uwp 使用 Microsoft.Graph 发送邮件
在 2018 年 10 月 13 号参加了 张队长 的 Office 365 训练营 学习如何开发 Office 365 插件和 OAuth 2.0 开发,于是我就使用 UWP 尝试使用 Micros ...
- vue中 js获取图片尺寸 设置不同样式
1.JS: 请求到后端数据后 判断图片尺寸 2.HTML代码 根据设置的类型,给图片添加不同的样式 3.CSS代码 添加不同尺寸的样式
- Python3内置函数、各数据类型(int/str/list/dict/set/tuple)的内置方法快速一览表
Python3内置函数 https://www.runoob.com/python3/python3-built-in-functions.html int https://www.runoob.co ...
- Activiti工作流引擎学习(一)
1.部署对象和流程定义相关表:RepositoryService act_re_deployment: 部署对象表:一次部署的多个文件的信息,对于不需要的流程可以删除和修改 act_re_procde ...
- Trendalyzer is an information visualization software
Trendalyzer is an information visualization software for animation of statistics that was initially ...
- JDK源码那些事儿之浅析Thread上篇
JAVA中多线程的操作对于初学者而言是比较难理解的,其实联想到底层操作系统时我们可能会稍微明白些,对于程序而言最终都是硬件上运行二进制指令,然而,这些又太过底层,今天来看一下JAVA中的线程,浅析JD ...