BZOJ 4300 绝世好题(位运算)

 

【题目链接】 http://www.lydsy.com/JudgeOnline/problem.php?id=4300

 

【题目大意】

  给出一个序列a,求一个子序列b,使得&和不为0

 

【题解】

  记录某个位置上为1的&序列长度的最长值,对于每个加入的数字, 更新每个数组。

  __builtin_ctz(x)函数用于求出x的二进制位数。

 

【代码】

#include <cstdio>  
#include <algorithm>  
int x,ans,res,n,a[40];  
using namespace std;  
int main(){  
    scanf("%d",&n);  
    for(int i=1;i<=n;i++){  
        scanf("%d",&x);res=0;  
        for(int j=x;j;j-=j&(-j))res=max(res,a[__builtin_ctz(j)]);
        ans=max(ans,++res);  
        for(int j=x;j;j-=j&(-j))a[__builtin_ctz(j)]=res;  
    }printf("%d\n",ans);
    return 0; 
}

  

posted @ 2016-11-16 12:33  forever97  阅读(289)  评论(0编辑  收藏  举报