AtCoder Regular Contest E - Or Plus Max
Time limit : 2sec / Memory limit : 1024MB
Score : 700 points
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
要求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的更多相关文章
- AtCoder Regular Contest 094 (ARC094) CDE题解
原文链接http://www.cnblogs.com/zhouzhendong/p/8735114.html $AtCoder\ Regular\ Contest\ 094(ARC094)\ CDE$ ...
- AtCoder Regular Contest 094
AtCoder Regular Contest 094 C - Same Integers 题意: 给定\(a,b,c\)三个数,可以进行两个操作:1.把一个数+2:2.把任意两个数+1.求最少需要几 ...
- AtCoder Regular Contest 095
AtCoder Regular Contest 095 C - Many Medians 题意: 给出n个数,求出去掉第i个数之后所有数的中位数,保证n是偶数. \(n\le 200000\) 分析: ...
- AtCoder Regular Contest 102
AtCoder Regular Contest 102 C - Triangular Relationship 题意: 给出n,k求有多少个不大于n的三元组,使其中两两数字的和都是k的倍数,数字可以重 ...
- AtCoder Regular Contest 096
AtCoder Regular Contest 096 C - Many Medians 题意: 有A,B两种匹萨和三种购买方案,买一个A,买一个B,买半个A和半个B,花费分别为a,b,c. 求买X个 ...
- AtCoder Regular Contest 097
AtCoder Regular Contest 097 C - K-th Substring 题意: 求一个长度小于等于5000的字符串的第K小子串,相同子串算一个. K<=5. 分析: 一眼看 ...
- AtCoder Regular Contest 098
AtCoder Regular Contest 098 C - Attention 题意 给定一个只包含"E","W"字符串,可以花一的花费使他们互相转换.选定 ...
- AtCoder Regular Contest 099
AtCoder Regular Contest 099 C - Minimization 题意 题意:给出一个n的排列.每次操作可以使一段长度为K的连续子序列变成该序列的最小数.求最少几次使得整个数列 ...
- Atcoder regular Contest 073(D - Simple Knapsack)
Atcoder regular Contest 073(D - Simple Knapsack) 传送门 因为 w1≤wi≤w1+3 这个特殊条件,我们可以将每个重量离散化一下,同时多开一维记录选择的 ...
随机推荐
- oracle学习之路(四) ---------PL/SQL 表,二维数组(TABLE)
LOB类型 ORACLE提供了LOB (Large OBject)类型.用于存储大的数据对象的类型.ORACLE眼下主要支持BFILE, BLOB, CLOB 及 NCLOB 类型. NCLOB 存储 ...
- 为备考二级C语言做的代码练习---辅导资料《C语言经典编程282例》--(1)
因为二级考试的时候用的C语言编译器是VC++6.0 真是日了狗了 用这个编译器 这是我第2个C编译器吧,第一个用的是啊哈C编译器..第二个是VS++6.0 然后在win下用VS2013感觉挺不错的 毕 ...
- MVC项目总结
View命名 View下有多个模块的文件夹,我们根据微软的规定,每个模块下的首页都为Index.cshtml命名 获得当前页面的控制器名称 var currentControllerName = th ...
- c# emit 动态实现接口
using System; using System.Linq; using System.Reflection; using System.Reflection.Emit; namespace Te ...
- WebStorm 常用功能
WebStorm 常用功能的使用技巧分享 WebStorm 是 JetBrain 公司开发的一款 JavaScript IDE,使用非常方便,可以使编写代码过程更加流畅. 本文在这里分享一些常用功能的 ...
- Thymeleaf框架
简单说, Thymeleaf 是一个跟 Velocity.FreeMarker 类似的模板引擎,它可以完全替代 JSP .相较与其他的模板引擎,它有如下三个极吸引人的特点: 1.Thymeleaf 在 ...
- Android setTag()与getTag(),与set多个setTag()
首先我们要知道setTag方法是干什么的,SDK解释为 Tags Unlike IDs, tags are not used to identify views. Tags are essential ...
- System.TypeLoadException: Could not load type 'System.IO.Compression.CompressionLevel' from assembly 'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.
1.提示错误信息: zipSystem.TypeLoadException: Could not load type 'System.IO.Compression.CompressionLevel' ...
- python中装饰器你真的理解吗?
def w1(func): print('装饰器1....') def w1_in(): print('w1_in.....') func() return w1_in def w2(func): p ...
- Linux Tomcat的安装
inux版本:CentOS 6.2 iso文件下载地址:http://mirrors.163.com/centos/6.2/isos/i386/CentOS-6.2-i386-bin-DVD1.iso ...