绝世好题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

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

递推

f[x]表示相应二进制位为1时的最优解

每一次输入一个数,二进制分解,取为1的f值的最大长度

再将其加1,取代对应的f值

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 using namespace std;
 6 int f[100002],x,n,ans;
 7 int main()
 8 {int i,cnt,z;
 9     cin>>n;
10      for (i=1;i<=n;i++)
11      {
12         scanf("%d",&x);
13         cnt=0,z=0;
14         int xx=x;
15         while (x)
16         {
17             cnt++;
18             if (x%2==1)
19             {
20                 z=max(z,f[cnt]);
21             }
22             x/=2;
23         }
24         cnt=0;
25          while (xx)
26          {
27             cnt++;
28             if (xx%2==1)
29             {
30                 f[cnt]=max(f[cnt],z+1);
31                 ans=max(ans,f[cnt]);
32             }
33             xx/=2;
34          }
35      }
36      cout<<ans;
37 }

 

 

 

posted @ 2017-07-07 21:16  Z-Y-Y-S  阅读(184)  评论(0编辑  收藏  举报