#1040. [CSP2020 提高级] 第 39 题
[CSP2020 提高级] 第 39 题
(最优子序列)取 ,给出长度为 的整数序列 。对于一个二进制数 ,定义其分值 为 ,其中 表示 二进制表示中 的个数。对于一个子序列 ,定义其子序列分值 为 $w(b_1 \oplus b_2) + w(b_2 \oplus b_3) + w(b_3 \oplus b_4) + \cdots + w(b_{k-1} \oplus b_k)$。其中 表示按位异或。对于空子序列,规定其子序列分值为 求一个子序列使得其子序列分值最大,输出这个最大值。
输入第一行包含一个整数 接下来一行包含 个整数 。
提示:考虑优化朴素的动态规划算法,将前 位和后 位分开计算。
Max[x][y] 表示当前的子序列下一个位置的高 位是 、最后一个位置的低 位是 时的最大价值。
试补全程序。
#include <iostream>
using namespace std;
typedef long long LL;
const int MAXN = 40000, M = 16, B = M >> 1, MS = (1 << B) - 1;
const LL INF = 1000000000000000LL;
LL Max[MS + 4][MS + 4];
int w(int x)
{
int s = x;
while(x)
{
①;
s++;
}
return s;
}
void to_max(LL &x, LL y)
{
if(x < y)
x = y;
}
int main()
{
int n;
LL ans = 0;
cin >> n;
for(int x = 0; x <= MS; x++)
for(int y = 0; y <= MS; y++)
Max[x][y] = -INF;
for(int i = 1; i <= n ; i++)
{
LL a;
cin >> a;
int x = ② , y = a & MS;
LL v = ③;
for(int z = 0; z < = MS; z++)
to_max(v, ④);
for(int z = 0; z < = MS; z++)
⑤;
to_max(ans , v);
}
cout << ans << endl;
return 0;
}
{{ select(1) }}
x >>= 1x ^= x &(x ^ (x + 1))x -= x | -xx ^= x &(x ^ (x - 1))