BZOJ:4300 絕世好(sb)題

4300: 絕世好題

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


Source

[Submit][Status][Discuss]
題解:這個題一眼秒n²做法,然而有更加機智的做法,因爲是按位與,所以當二進制時此位爲1在進行dp就可以了。
貼上代碼:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define M 100100
using namespace std;
int n,ans;
int f[32];
int main()
{
    int i,j,x;
    cin>>n;
    for(i=1;i<=n;i++)
    {
        scanf("%d",&x);
        int temp=0;
        for(j=0;j<=30;j++)
            if(x&(1<<j))
                temp=max(temp,f[j]+1);
        for(j=0;j<=30;j++)
            if(x&(1<<j))
                f[j]=temp;
        ans=max(ans,temp);
    }
    cout<<ans<<endl;
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章