1. 程式人生 > >[bzoj 4300]絕世好題

[bzoj 4300]絕世好題

math ret 什麽 序列 con 一個 思路 c++ its

題意:求一個最長子序列長度使得相鄰的按位與不是0.

思路:

(首先\(\%\)一波出題人)

感覺思路有點奇怪,考慮為什麽會\(\&\)成0,要是0就必須每一位都至少一個是0,那麽我們可得\(f[i]\)表示第 i 位是1的最長子序列的長度,隨便轉移一下就可以了。

#include <bits/stdc++.h>

using namespace std;
const int maxn = 100010;
int f[maxn];

int n;
int a[maxn];
int ans;

int main () {
    cin >> n;
    for(int i = 1;i <= n; ++i) {
        cin >> a[i];
    }
    for(int i = 1;i <= n; ++i) {
        int res = 0;
        for(int j = 0;j <= 40; ++j) {
            if(a[i] & (1 << j)) {
                res = max(res,f[j] + 1);
            }
        }
        for(int j = 0;j <= 40; ++j) {
            if(a[i] & (1 << j)) {
                f[j] = res;
            }
        }
        ans = max(ans,res);
    }
    printf("%d\n",ans);
    return 0;
}

[bzoj 4300]絕世好題