位运算上的小技巧 - AtCoder

Problem Statement

There is an integer sequence of length 2N: A0,A1,…,A2N1. (Note that the sequence is 0-indexed.)

For every integer K satisfying 1K2N1, solve the following problem:

  • Let i and j be integers. Find the maximum value of Ai+Aj where 0i<j2N1 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  A2N1

Output

Print 2N1 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;
}

 

posted @ 2018-07-04 21:16  楼主好菜啊  阅读(250)  评论(0编辑  收藏  举报