← 查看本卷题目
组合题5 个小题
已作答 0 / 5

2.(最优子序列)取 m=16m = 16,给出长度为 nn 的整数序列 a1,a2,…,an(0≤ai<2m)a_1,a_2,\dots,a_n(0 \le a_i < 2^m)。对于一个二进制数 xx,定义其分值 w(x)w(x) 为 x+popcnt⁡(x)x + \operatorname {popcnt}(x),其中 popcnt⁡(x)\operatorname{popcnt}(x) 表示 xx 二进制表示中 11 的个数。对于一个子序列 b1,b2,…,bkb_1,b_2,\dots,b_k,定义其子序列分值 SS 为 w(b1⊕b2)+w(b2⊕b3)+w(b3⊕b4)+⋯+w(bk−1⊕bk)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)。其中 ⊕\oplus 表示按位异或。对于空子序列,规定其子序列分值为 00 求一个子序列使得其子序列分值最大,输出这个最大值。

输入第一行包含一个整数 n(1≤n≤40000)n(1 \le n \le 40000) 接下来一行包含 nn 个整数 a1,a2,⋯ ,ana_1,a_2,\cdots,a_n。

提示:考虑优化朴素的动态规划算法,将前 m2\dfrac{m}{2} 位和后 m2\dfrac{m}{2} 位分开计算。

Max[x][y] 表示当前的子序列下一个位置的高 88 位是 xx、最后一个位置的低 88 位是 yy 时的最大价值。

试补全程序。

#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;}
第42题单选题原卷 3 分

④处应填( )

填写答案