4300: 絕世好題
阿新 • • 發佈:2018-11-30
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
31 2 3
Sample Output
2HINT
n<=100000,ai<=2*10^9
子列是不連續的!!!
一開始以為是連續的,懵逼了半天
1 #include<iostream> 2 #include<cstdio> 3 usingnamespace 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 }