AT4168 [ARC100C] Or Plus Max

https://www.luogu.com.cn/problem/AT4168

高维前缀和

这题每一位都是 0 / 1 0/1 0/1,一共 n n n

考虑一维一维的加入,然后把包含这一维的状态都更新一遍
这样就完成了高维前缀和

具体实现就是求出最大值和次大值

code:


#include<bits/stdc++.h>
using namespace std;
const int N = (1 << 18) + 5;
struct A {
    int x, y;
} a[N];
int b[12], n;
int main() {
    scanf("%d", &n);
    for(int i = 0; i < (1 << n); i ++) scanf("%d", &a[i].x);
    for(int i = 0; i < n; i ++)
    for(int S = 0; S < (1 << n); S ++)
         if((S >> i) & 1){
            int T = S ^ (1 << i);
            if(a[S].x > a[T].x) a[S].y = max(a[S].y, a[T].x);
            else a[S].y = max(a[S].x, a[T].y), a[S].x = a[T].x;
        }
    int ans = 0;
    for(int i = 1; i < (1 << n); i ++) printf("%d\n", ans = max(ans, a[i].x + a[i].y));
    return 0;
}
posted @ 2021-10-11 08:09  lahlah  阅读(27)  评论(0编辑  收藏  举报