BZOJ4300 绝世好题(DP)

4300: 绝世好题

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 1636  Solved: 831
[Submit][Status][Discuss]

Description

给定一个长度为$n$的数列$ai$,求$ai$的子序列$bi$的最长长度,满足bi&bi-1!=0(2<=i<=len)

Input

输入文件共2行。
第一行包括一个整数$n$
第二行包括$n$个整数,第$i$个整数表示$ai$。

Output

输出文件共一行。
包括一个整数,表示子序列$bi$的最长长度。

Sample Input

3
1 2 3

Sample Output

2

HINT

n<=100000,ai<=2*10^9


Source

By Oxer

想到了就很简单了~

#include <bits/stdc++.h>

using namespace std;


int f[32];
int n;
int x, now;

int main(){

    scanf("%d", &n);
    for (int i = 1; i <= n; ++i){
        scanf("%d", &x);
        now = 0;
        for (int j = 0; j <= 30; ++j) if (x & (1 << j)) now = max(now, f[j] + 1);
        for (int j = 0; j <= 30; ++j) if (x & (1 << j)) f[j] = max(f[j], now);
    }
    
    int ret = 0;
    for (int i = 0; i <= 30; ++i) ret = max(ret, f[i]);
    printf("%d\n", ret);
    return 0;

}

 

 

posted @ 2017-02-16 16:49  cxhscst2  阅读(173)  评论(0编辑  收藏  举报