bzoj4300绝世好题

bzoj4300绝世好题

题意:

给定一个长度为n的数列ai,求ai的子序列bi的最长长度,满足bi&bi-1!=0。n≤100000,ai≤10^9。

题解:

用f[i]表示当前二进制i为1的最长子序列长度。每次求所有((1<<i)&bi)==1的f[i]最大值max,将所有((1<<i)&bi)==1的f[i]变为max+1。

代码:

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #define inc(i,j,k) for(int i=j;i<=k;i++)
 5 #define maxn 100010
 6 using namespace std;
 7 
 8 inline int read(){
 9     char ch=getchar(); int f=1,x=0;
10     while(ch<'0'||ch>'9'){if(ch=='-')f=-1; ch=getchar();}
11     while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
12     return f*x;
13 }
14 int n,a,f[40],tot;
15 int main(){
16     n=read();
17     inc(i,1,n){
18         a=read(); tot=0;
19         inc(j,0,30)if(a&(1<<j))tot=max(tot,f[j]); inc(j,0,30)if(a&(1<<j))f[j]=tot+1;
20     }
21     tot=0; inc(i,0,30)tot=max(tot,f[i]); printf("%d",tot); return 0;
22 }

 

20160812

posted @ 2016-08-15 21:50  YuanZiming  阅读(465)  评论(0编辑  收藏  举报