Blog of RuSun

\begin {array}{c} \mathfrak {One Problem Is Difficult} \\\\ \mathfrak {Because You Don't Know} \\\\ \mathfrak {Why It Is Diffucult} \end {array}

ARC100C Or Plus Max

LuoGu: AT4168 [ARC100C] Or Plus Max

AtCoder: E - Or Plus Max

给你一个长度为 $2^n$ 的序列 $a$,每个$1\le K\le 2^n-1$,找出最大的 $a_i+a_j$($i \mathbin{\mathrm{or}} j \le K$,$0 \le i < j < 2^n$)并输出。 $\mathbin{\mathrm{or}}$ 表示按位或运算。

注意到 $or$ ,考虑高维前缀和相关。因为对于每个 $k$ 都要答案,所有考虑计算 $i | j = k$ 并做前缀最大值,发现仍然不好做,可以转化为 $i | j \subseteq k$ ,即对于每个 $k$ ,需要找到子集中最大值和次大值,上高维前缀和即可。

查看代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <cstdio>
#include <algorithm>
using namespace std;
typedef pair<int, int> PII;
template <class Type>
void read(Type &x)
{
char c;
bool flag = false;
while ((c = getchar()) < '0' || c > '9')
c == '-' && (flag = true);
x = c - '0';
while ((c = getchar()) >= '0' && c <= '9')
x = (x << 3) + (x << 1) + c - '0';
flag && (x = ~x + 1);
}
template <class Type>
void write(Type x)
{
x < 0 && (putchar('-'), x = ~x + 1);
x > 9 && (write(x / 10), 0);
putchar(x % 10 + '0');
}
const int N = 18, inf = 1e9;
int n;
PII w[1 << N];
PII operator+(const PII &x, const PII &y)
{
if (x.first < y.first)
return make_pair(y.first, max(y.second, x.first));
return make_pair(x.first, max(x.second, y.first));
}
int main()
{
read(n);
for (int i = 0; i < 1 << n; i++)
read(w[i].first), w[i].second = -inf;
for (int i = 0; i < n; i++)
for (int j = 0; j < 1 << n; j++)
(j >> i & 1) && (w[j] = w[j] + w[j ^ 1 << i], 0);
int res = w[0].first + w[0].second;
for (int i = 1; i < 1 << n; i++)
{
res = max(res, w[i].first + w[i].second);
write(res), puts("");
}
return 0;
}