Time limit : 2sec / Memory limit : 1024MB

Score : 700 points

Problem Statement

There is an integer sequence of length 2NA0,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

Copy
2
1 2 3 1

Sample Output 1

Copy
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

Copy
3
10 71 84 33 6 47 23 25

Sample Output 2

Copy
81
94
155
155
155
155
155

Sample Input 3

Copy
4
75 26 45 72 81 47 97 97 2 2 25 82 84 17 56 32

Sample Output 3

Copy
101
120
147
156
156
178
194
194
194
194
194
194
194
194
194
要求max(Ai+Aj) (0≤i<j≤2N−1 && i|j≤k),显然i <= k && j <= k,只要求max(Ai+Aj) (0≤i<j≤2N−1 && i|k=k && j|k=k),然后,就可以得出想要的结果,i|k=k说明i是在k的基础上二进制位中的0保持不变,改变1,把1变为0,则i < k,
这样求前缀,然后排着往后更新最大值即可。
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#define Max 1 << 19
using namespace std;
int n,e,s[Max];
int r[Max][];///记录最大值下标
bool cmp(int a,int b){
return s[a] < s[b];
}
int main(){
scanf("%d",&n);
e = << n;///总数个数
for (int i = ;i < e;i ++)
scanf("%d",&s[i]);
s[e] = -;
r[][] = ;///初始化
r[][] = e;
for (int i = ;i < e;i ++){
r[i][] = i;///初始化
r[i][] = e;
int x[];
for (int j = ;j < n;j ++)
if (i & ( << j)){///此位为1
int d = i ^ ( << j);///把这位变成0 对于d的情况前面已经确定过 这里直接用
x[] = r[i][];
x[] = r[i][];
x[] = r[d][];
x[] = r[d][];
sort(x,x + ,cmp);///按照对应位置值从小到大排列下标
r[i][] = x[];///存最大值下标
r[i][] = x[] == x[] ? x[] : x[];///存非重复下标
}
}
int ans = ;
for (int i = ;i < e;i ++){
ans = max(ans,s[r[i][]] + s[r[i][]]);///前缀更新i的最大值
printf("%d\n",ans);
}
return ;
}
 

AtCoder Regular Contest E - Or Plus Max的更多相关文章

  1. AtCoder Regular Contest 094 (ARC094) CDE题解

    原文链接http://www.cnblogs.com/zhouzhendong/p/8735114.html $AtCoder\ Regular\ Contest\ 094(ARC094)\ CDE$ ...

  2. AtCoder Regular Contest 094

    AtCoder Regular Contest 094 C - Same Integers 题意: 给定\(a,b,c\)三个数,可以进行两个操作:1.把一个数+2:2.把任意两个数+1.求最少需要几 ...

  3. AtCoder Regular Contest 095

    AtCoder Regular Contest 095 C - Many Medians 题意: 给出n个数,求出去掉第i个数之后所有数的中位数,保证n是偶数. \(n\le 200000\) 分析: ...

  4. AtCoder Regular Contest 102

    AtCoder Regular Contest 102 C - Triangular Relationship 题意: 给出n,k求有多少个不大于n的三元组,使其中两两数字的和都是k的倍数,数字可以重 ...

  5. AtCoder Regular Contest 096

    AtCoder Regular Contest 096 C - Many Medians 题意: 有A,B两种匹萨和三种购买方案,买一个A,买一个B,买半个A和半个B,花费分别为a,b,c. 求买X个 ...

  6. AtCoder Regular Contest 097

    AtCoder Regular Contest 097 C - K-th Substring 题意: 求一个长度小于等于5000的字符串的第K小子串,相同子串算一个. K<=5. 分析: 一眼看 ...

  7. AtCoder Regular Contest 098

    AtCoder Regular Contest 098 C - Attention 题意 给定一个只包含"E","W"字符串,可以花一的花费使他们互相转换.选定 ...

  8. AtCoder Regular Contest 099

    AtCoder Regular Contest 099 C - Minimization 题意 题意:给出一个n的排列.每次操作可以使一段长度为K的连续子序列变成该序列的最小数.求最少几次使得整个数列 ...

  9. Atcoder regular Contest 073(D - Simple Knapsack)

    Atcoder regular Contest 073(D - Simple Knapsack) 传送门 因为 w1≤wi≤w1+3 这个特殊条件,我们可以将每个重量离散化一下,同时多开一维记录选择的 ...

随机推荐

  1. QTreeWidget里嵌套表格QTableView

    InformationPositionSubTableView::InformationPositionSubTableView(QStringList& columnNameList,QLi ...

  2. python tkinter module的用法

    tkinter windows下从python3.2版本之后是自动安装的. python3.3之后的引入方式: >>> import _tkinter>>> imp ...

  3. CSS3 实现背景透明,文字不透明,兼容所有浏览器

    <!DOCTYPE html><html><head><meta charset="utf-8"><title>opac ...

  4. jQuery Validate(二)

    刚刚试了所谓的新版的用法.千万别问我是多新,因为我也不知道... <!DOCTYPE html> <html> <head> <script src=&quo ...

  5. 【翻译自mos文章】关于分区索引:Global, Local, Prefixed and Non-Prefixed

    来源于: Partitioned Indexes: Global, Local, Prefixed and Non-Prefixed (文档 ID 69374.1) APPLIES TO: Oracl ...

  6. 从epoll构建muduo-12 多线程入场

    mini-muduo版本号传送门 version 0.00 从epoll构建muduo-1 mini-muduo介绍 version 0.01 从epoll构建muduo-2 最简单的epoll ve ...

  7. 网页图表类框架(插件)——百度eCharts和Highcharts

    ECharts, 缩写来自Enterprise Charts,商业级数据图表,一个纯Javascript的图表库, 可以流畅的运行在PC和移动设备上,兼容当前绝大部分浏览器(IE6/7/8/9 /10 ...

  8. 如何运用spring将dao注入到servlet中?

    1.servlet的init方法 public void init(ServletConfig config) throws ServletException { super.init(config) ...

  9. 初探swift语言的学习笔记四-2(对上一节有些遗留进行处理)

    作者:fengsh998 原文地址:http://blog.csdn.net/fengsh998/article/details/30314359 转载请注明出处 假设认为文章对你有所帮助,请通过留言 ...

  10. Windows 驱动入门(二)代码结构

    windows驱动程序基础.转载标明出处:http://blog.csdn.net/ikerpeng/article/details/38777641 windows驱动程序结构: 我想说的是wind ...