Mighty title

Link


Because it involves operations in place, so consider by-bit processing.

With \ (dp [i] [j ] \) represents the currently considered before use \ (I \) number of constituent sequences, the binary number of the last Dir \ (J \) bits are \ (1 \) is the longest sequence length.

Need to satisfy \ (B_i \ & B_ {I +. 1} \ NE 0 \) , is necessary to ensure \ ((. 1 << K) \ & a_i \ NE 0 \) , \ (K \) as the first number on the \ ( k \) bits.

It is easy to write equation \ [dp [i] [j ] = max \ {dp [i-1] [k] +1 | (1 << j) \ & a_i> 0, (1 << k) \ & a_i> 0 \} \]

Then roll out the first dimension can be directly written \ (dp [j] \)

Code:

#include<stdio.h>
#define N 100007
int n;
int a,f[35],maxx=0;
inline int max(int x,int y){return x>y? x:y;}
inline void read(int &x){
    x=0;char c=getchar();
    while(c<'0'||c>'9') c=getchar();
    while(c>='0'&&c<='9'){x=x*10+c-48;c=getchar();}
}
int main(){
    read(n);
    for(int i=1;i<=n;i++){
        read(a);
        int rest=1;
        for(int j=30;j>=0;j--)
            if(a&(1<<j)) rest=max(f[j]+1,rest);
        for(int j=30;j>=0;j--)
            if(a&(1<<j)) f[j]=max(rest,f[j]); 
    }
    for(int i=0;i<=30;i++)
        maxx=max(maxx,f[i]);
    printf("%d",maxx);
}

Guess you like

Origin www.cnblogs.com/wwlwQWQ/p/12407375.html