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 这个特殊条件,我们可以将每个重量离散化一下,同时多开一维记录选择的 ...
随机推荐
- python windows package/module 安装
方法一: cmd中运行pip install xx 注意:1.pip添加至环境变量 2.系统自带这些安装模块 运行pip freeze > requirements.txt 可以在require ...
- jdbcTemplaate queryForObject的两个易混淆的方法
JdbcTemplate中有两个可能会混淆的queryForObject方法: 1. Object queryForObject(String sql, Object[] args, Class ...
- linux的MACHINE_START-MACHINE_END(转)
转自: http://blog.sina.com.cn/s/blog_753fd0b00100t8js.html 在友善mini2440提供的linux2.6.32.2内核中,有如下定义: MACHI ...
- 理解Linux系统负荷(WDCP系统后台参数之一)
一.查看系统负荷 如果你的网站很卡,可能是因为服务器很慢,,你或许想查看一下,它的工作量是否太大了. 在Linux系统中,我们一般使用uptime命令查看(w命令和top命令也行).(另外,它们在苹果 ...
- 【虚拟机】WIN8.1系统安装虚拟机win7环境
一.虚拟机的安装 1.准备 VMware Workstation 的软硬件支持,请查看 http://www.vmware.com/cn/products/workstation.html#techs ...
- 【LeetCode从零单排】No.135Candy(双向动态规划)
1.题目 There are N children standing in a line. Each child is assigned a rating value. You are giving ...
- Unity编辑器扩展之RequireComponent等详解
RequireComponent的使用: 当你添加的一个用了RequireComponent组件的脚本,需要的组件将会自动被添加到game object(游戏物体).这个可以有效的避免组装错误.举个例 ...
- sql 时间转换格式
convert(varchar(10),字段名,转换格式) CONVERT(nvarchar(10),count_time,121)CONVERT为日期转换函数,一般就是在时间类型(datetime, ...
- API自动化测试利器——Postman
自从开始做API开发之后,我就在寻找合适的API测试工具.一开始不是很想用Chrome扩展,用的WizTools的工具,后来试过一次Postman之后就停不下来了,还买了付费的Jetpacks.推出T ...
- 基于live555实现的RTSPServer对底层进行性能优化的方法
在博客<EasyIPCamera高性能摄像机RTSP服务器RTSPServer解决方案>我介绍了基于live555实现的一套RTSPServer功能组件,当时开发者经过几个月的调试,已经将 ...