4300: 绝世好题
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 1328 Solved: 724
[ 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
对于条件bi & bi-1 != 0,,要满足它,只要满足!= 0即可,也就是说,bi & bi-1的值是多少,根本无关紧要
令fi为以位置i结尾的最长子序列长度
定义gi为以最后一个数字转换为二进制数码后第i位为1的最长子序列长度
这样转移和更新log就完成了
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<bitset>
#include<ext/pb_ds/priority_queue.hpp>
using namespace std;
const int maxn = 1E5 + 10;
int n,ans,a[maxn],f[maxn],g[31];
int main()
{
#ifdef DMC
freopen("DMC.txt","r",stdin);
#endif
cin >> n;
for (int i = 1; i <= n; i++) scanf("%d",&a[i]);
for (int i = 1; i <= n; i++)
{
for (int j = 0; j <= 30; j++)
if (a[i]&(1<<j)) f[i] = max(f[i],g[j]);
++f[i]; ans = max(ans,f[i]);
for (int j = 0; j <= 30; j++)
if (a[i]&(1<<j)) g[j] = max(g[j],f[i]);
}
cout << ans;
return 0;
}