位运算上的小技巧 - AtCoder

Posted ccut-ry

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了位运算上的小技巧 - AtCoder相关的知识,希望对你有一定的参考价值。

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
", ans);
    } 
    
    return 0;
}

 



以上是关于位运算上的小技巧 - AtCoder的主要内容,如果未能解决你的问题,请参考以下文章

关于取模运算的小技巧

关于取模运算的小技巧

关于取模运算的小技巧

AtCoder - arc098_b Xor Sum 2(尺取+位运算)

Android课程---Android Studio使用小技巧:提取方法代码片段

开发人员应该了解哪些有用的按位运算符代码技巧? [关闭]