BZOJ 4300 绝世好题(位运算)

forever97 posted @ 2016年10月23日 22:30 in 算法-位运算 with tags 位运算 , 598 阅读

 

【题目链接】 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; 
}  

登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter