BZOJ 4300: 绝世好题

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

题解
这东西很像最长不下降,但是条件变成了& , 同样设dp[i]表示以i为结尾的符合要求的最长的序列的长度。在考虑 , 条件是&!=0 , 那什么时候才不等于0呢 , 显然之前要有与他对应位都是1的数 , 开一个数组 , t[i] , 表示当前所有数字中第i位(二进制)为1的数对应的dp的最大值 ,这样更新一下就行 , 时间复杂度$$O(NlogN)$$

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<vector>
#include<cstring>
#include<cmath>
using namespace std;
const int N = 101000;
inline int read()
{
    register int x = 0 , f = 0; register char c = getchar();
    while(c < '0' || c > '9') f |= c == '-' , c = getchar();
    while(c >= '0' && c <= '9') x = (x << 3) + (x << 1) + c - '0' , c = getchar();
    return f ? -x : x;
}
int n;
int a[N] , t[36] , dp[N];
int main()
{
	n = read(); for(int i = 1 ; i <= n ; ++i) a[i] = read();
    int ans = 0;
	for(int i = 1 , x; i <= n ; ++i)
    {
    	x = a[i];
    	for(int j = 0 ; j <= 30 ; ++j)
    		if((x >> j) & 1) dp[i] = max(dp[i] , t[j] + 1);
		for(int j = 0 ; j <= 30 ; ++j)
			if((x >> j) & 1) t[j] = max(t[j] , dp[i]);
		ans = max(ans , dp[i]);
	}
	cout << ans << '
';
	return 0;
}

原文地址:https://www.cnblogs.com/R-Q-R-Q/p/12249669.html