码迷,mamicode.com
首页 > 其他好文 > 详细

[bzoj 4300]绝世好题

时间:2018-08-08 17:39:32      阅读:119      评论:0      收藏:0      [点我收藏+]

标签:res   amp   for   转移   name   zoj   turn   c++   span   

题意:求一个最长子序列长度使得相邻的按位与不是0.

思路:

(首先\(\%\)一波出题人)

感觉思路有点奇怪,考虑为什么会\(\&\)成0,要是0就必须每一位都至少一个是0,那么我们可得\(f[i]\)表示第 i 位是1的最长子序列的长度,随便转移一下就可以了。

#include <bits/stdc++.h>

using namespace std;
const int maxn = 100010;
int f[maxn];

int n;
int a[maxn];
int ans;

int main () {
    cin >> n;
    for(int i = 1;i <= n; ++i) {
        cin >> a[i];
    }
    for(int i = 1;i <= n; ++i) {
        int res = 0;
        for(int j = 0;j <= 40; ++j) {
            if(a[i] & (1 << j)) {
                res = max(res,f[j] + 1);
            }
        }
        for(int j = 0;j <= 40; ++j) {
            if(a[i] & (1 << j)) {
                f[j] = res;
            }
        }
        ans = max(ans,res);
    }
    printf("%d\n",ans);
    return 0;
}

[bzoj 4300]绝世好题

标签:res   amp   for   转移   name   zoj   turn   c++   span   

原文地址:https://www.cnblogs.com/akoasm/p/9443889.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!