传送门
文章目录
- 题意:
- 思路:
题意:
给你一个长度为2n2^n2n的数组,让你对于所有的1≤k≤2n−11\le k\le 2^n-11≤k≤2n−1求最大的ai+aj,0≤i<j≤2n−1,iorj≤ka_i+a_j,0\le i<j\le2^n-1,i\ \ or \ \ j\le kai+aj,0≤i<j≤2n−1,i or j≤k。
思路:
直接想不好想,考虑如何能转化一下条件。
可以发现iorj≤ki\ \ or \ \ j\le ki or j≤k里面的i,ji,ji,j都是kkk的子集,所以对于每个kkk我们如果能快速求出其所有子集的最大值和次大值,就可以维护一个前缀最大值直接输出答案了。这个显然可以用sosdpsos dpsosdp来解决,由子集向上推即可。
// Problem: E - Or Plus Max
// Contest: AtCoder - AtCoder Regular Contest 100
// URL: https://atcoder.jp/contests/arc100/tasks/arc100_c
// Memory Limit: 1024 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
//#pragma GCC optimize(2)
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<cmath>
#include<cctype>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<sstream>
#include<ctime>
#include<cstdlib>
#define X first
#define Y second
#define L (u<<1)
#define R (u<<1|1)
#define pb push_back
#define mk make_pair
#define Mid (tr[u].l+tr[u].r>>1)
#define Len(u) (tr[u].r-tr[u].l+1)
#define random(a,b) ((a)+rand()%((b)-(a)+1))
#define db puts("---")
using namespace std;//void rd_cre() { freopen("d://dp//data.txt","w",stdout); srand(time(NULL)); }
//void rd_ac() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//AC.txt","w",stdout); }
//void rd_wa() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//WA.txt","w",stdout); }typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;const int N=1000010,mod=1e9+7,INF=0x3f3f3f3f;
const double eps=1e-6;int n;
int a[N];
PII ans[N];void merge(int x,int y) {if(ans[x].X<y) ans[x].Y=ans[x].X,ans[x].X=y;else if(ans[x].Y<y) ans[x].Y=y;
}int main()
{
// ios::sync_with_stdio(false);
// cin.tie(0);scanf("%d",&n);for(int i=0;i<1<<n;i++) scanf("%d",&a[i]),ans[i].X=a[i],ans[i].Y=-INF;for(int i=0;i<n;i++)for(int j=0;j<1<<n;j++) {if(!(j>>i&1)) continue;merge(j,ans[j^(1<<i)].X);merge(j,ans[j^(1<<i)].Y);}int res=0;for(int i=1;i<1<<n;i++) printf("%d\n",res=max(res,ans[i].X+ans[i].Y));return 0;
}
/**/