AtCoder Regular Contest 100 E - Or Plus Max

Posted basasuya

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了AtCoder Regular Contest 100 E - Or Plus Max相关的知识,希望对你有一定的参考价值。

一道很好的dp题
dp[K]存的是 i满足二进制1属于K二进制1位置 最大的两个Ai
这样dp[K]统计的两个数肯定满足(i | j) <= K
然后不断做 update(dp[i | (1<<j)], dp[I])

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <ctime>
using namespace std;
typedef long long ll;
const int N = 262200;
const ll INF = 1e18;

int A[N];
pair<int, int> dp[N];

void update(pair<int,int> &A, int x) {
    if(x > A.second) {
        A.second = x;
        if(A.second > A.first) {
            swap(A.second, A.first);
        }
    }
}
int main() {
    int n;
    while(~scanf("%d", &n)) {
        int nLen = (1<<n) - 1;
    
        for(int i = 0; i <= nLen; ++i) {
            scanf("%d", &A[i]);
            dp[i] = make_pair(A[i], -1);
        }

        for(int i = 0; i < n; ++i) {
            for(int j = 0; j <= nLen; ++j) {
                if( ((j >> i) & 1) == 0 ) {
                    int newI = j | (1 << i);
                    int t1 = dp[j].first;
                    int t2 = dp[j].second;
                    update(dp[newI], t1);
                    update(dp[newI], t2);
                }
            }
        }

    //  for(int i = 0; i <= nLen; ++i) printf("%d %d
", dp[i].first, dp[i].second);
        int ans = -1;
        for(int i = 1; i <= nLen; ++i) {
            ans = max(ans, dp[i].first + dp[i].second);
            printf("%d
", ans);
        } 
    }
    return 0;
}



以上是关于AtCoder Regular Contest 100 E - Or Plus Max的主要内容,如果未能解决你的问题,请参考以下文章

刷题AtCoder Regular Contest 001

AtCoder Regular Contest 094

[Atcoder Regular Contest 060] Tutorial

AtCoder Regular Contest 103

AtCoder Regular Contest 128

AtCoder Regular Contest 119 C