BZOJ4300: 绝世好题

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

 

对于100%的数据,1<=n<=100000,ai<=10^9。

 

设f[i][j]表示前i个数,选出的最后一个数的第j位为1,最多选多少数。

可以用个滚动数组。

#include<cstdio>
#include<cctype>
#include<queue>
#include<cmath>
#include<cstring>
#include<algorithm>
#define rep(i,s,t) for(int i=s;i<=t;i++)
#define dwn(i,s,t) for(int i=s;i>=t;i--)
#define ren for(int i=first[x];i!=-1;i=next[i])
using namespace std;
inline int read() {
    int x=0,f=1;char c=getchar();
    for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
    for(;isdigit(c);c=getchar()) x=x*10+c-'0';
    return x*f;
}
const int maxn=100010;
int n,A[maxn],ans,f[32];
int main() {
    n=read();
    rep(i,1,n) A[i]=read();
    rep(i,1,n) {
        int res=0;
        rep(j,0,31) if(A[i]>>j&1) res=max(res,f[j]);
        rep(j,0,31) if(A[i]>>j&1) f[j]=res+1;
        ans=max(ans,res+1);
    }
    printf("%d\n",ans);
    return 0;
}
View Code

 

posted @ 2015-11-18 18:45  wzj_is_a_juruo  阅读(176)  评论(0编辑  收藏  举报