位運算上的小技巧 - 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 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
Copy2 1 2 3 1
Sample Output 1
Copy3 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)
Sample Input 2
Copy3 10 71 84 33 6 47 23 25
Sample Output 2
Copy81 94 155 155 155 155 155
Sample Input 3
Copy4 75 26 45 72 81 47 97 97 2 2 25 82 84 17 56 32
Sample Output 3
Copy101 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