bzoj4300 绝世好题

传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=4300

【题解】

令f[i]表示二进制下上一个数第i位为1的最大长度

那么至少有一位这个数和上一个数都为1,就可以转移了。

# include <stdio.h>
# include <string.h>
# include <iostream>
# include <algorithm>
// # include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
const int M = 5e5 + 10, N = 30 + 10;
const int mod = 1e9+7;

# define RG register
# define ST static

int n, a[M];
int f[N];

int main() {
    cin >> n;
    for (int i=1; i<=n; ++i) scanf("%d", a+i);
    for (int i=1; i<=n; ++i) {
        int t = 0;
        for (int j=0; j<=30; ++j)
            if(a[i]&(1<<j)) t = max(t, f[j]);
        ++t;
        for (int j=0; j<=30; ++j)
            if(a[i]&(1<<j)) f[j] = t;
    }
    int ans = 0;
    for (int i=0; i<=30; ++i) ans = max(ans, f[i]);
    cout << ans;
    return 0;
}
View Code

 

posted @ 2017-06-06 20:51  Galaxies  阅读(243)  评论(0编辑  收藏  举报