4300: 绝世好题

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 3267  Solved: 1761
[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

 

子列是不连续的!!!

一开始以为是连续的,懵逼了半天

 1 #include<iostream>
 2 #include<cstdio>
 3 using namespace std;
 4 
 5 const int MAXN=100005;
 6 int n,tmp,ans;
 7 int a[MAXN],f[MAXN];
 8 
 9 int main()
10 {
11     scanf("%d",&n);
12     for(int i=1;i<=n;i++)
13         scanf("%d",&a[i]);
14     for(int i=1;i<=n;i++)
15     {
16         tmp=0;
17         for(int j=0;j<=30;j++)
18             if(a[i]&(1<<j))
19                 tmp=max(tmp,f[j]+1);
20         for(int j=0;j<=30;j++)
21             if(a[i]&(1<<j))
22                 f[j]=tmp;
23         ans=max(ans,tmp);
24     }
25     printf("%d",ans);
26     return 0;
27 }

猜你喜欢

转载自www.cnblogs.com/InWILL/p/10046317.html