码迷,mamicode.com
首页 > 其他好文 > 详细

位运算上的小技巧 - AtCoder

时间:2018-07-04 22:48:47      阅读:198      评论:0      收藏:0      [点我收藏+]

标签:copy   find   一个   思路   only   The   pos   max   geo   

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 1K2N?1, solve the following problem:

  • Let i and j be integers. Find the maximum value of Ai+Aj where 0i<j2N?1 and (i or j)K. Here, or denotes the bitwise OR.

Constraints

  • 1N18
  • 1Ai109
  • 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

题意 : 给你 一堆数,并设定一个变量 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

标签:copy   find   一个   思路   only   The   pos   max   geo   

原文地址:https://www.cnblogs.com/ccut-ry/p/9265378.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!