Submit: 1120 Solved: 609
[Submit][Status][Discuss]
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
1 2 3
Sample Output
2
HINT
n<=100000,ai<=2*10^9
Source
【题解】
设f[i],二进制中倒数第i位为1的数(其他位置不一定)所形成的数列的最长长度;
【代码】
#include <cstdio>
#include <algorithm>
using namespace std;
const int MAXN = 101000;
int n,ans = 0;
int x;
int f[31] = { 0 };
int main()
{
scanf("%d", &n);
for (int i = 1; i <= n; i++)
{
scanf("%d", &x);
int temp = 0;
for (int j = 0; j <= 30; j++)
if (x & (1 << j))
temp = max(temp, f[j] + 1);
for (int j = 0; j <= 30; j++)
if (x & (1 << j))
f[j] = temp;
ans = max(ans, temp);
}
printf("%d
", ans);
return 0;
}