BZOJ4300 绝世好题 dp

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

题解:

  一眼秒,DP。然而我开始只想出了O(N^2)的算法,其实我们并不需要去枚举从谁转移过来,

  毕竟是和位运算有关,那么我们肯定是要从位的角度出发。

  f[i]表示处理到当前数,第i位不为0的最优长度。转移很好转。(开始有一个误区:认为一定这个序列的所有数某一位都不为0,

  实际并不是,因为相邻两个数可以是不同位置使得&运算不为0)

 1 #include<cstring>
 2 #include<cmath>
 3 #include<iostream>
 4 #include<algorithm>
 5 #include<cstdio>
 6 #include<cstdlib>
 7 
 8 #define N 100007
 9 #define ll long long
10 using namespace std;
11 inline int read()
12 {
13     int x=0,f=1;char ch=getchar();
14     while(ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
15     while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
16     return x*f;
17 }
18 
19 int n,ans=-1;
20 int f[N];
21 
22 int main()
23 {
24     n=read();
25     for (int i=1;i<=n;i++)
26     {
27         int x=read(),now=0;
28         for (int j=0;j<=30;j++)
29             if (x&(1<<j)) now=max(f[j],now);
30         now++;
31         for (int j=0;j<=30;j++)
32             if (x&(1<<j)) f[j]=max(f[j],now);
33     }
34     for (int i=1;i<=n;i++) ans=max(ans,f[i]);
35     printf("%d
",ans);
36 }
原文地址:https://www.cnblogs.com/fengzhiyuan/p/8204234.html