[BZOJ 4300] 绝世好题
[题目链接]
https://www.lydsy.com/JudgeOnline/problem.php?id=4300
[算法]
记Fi表示二进制表示下第i位为1的最长序列
可以通过枚举二进制的每一位进行转移
时间复杂度 : O(NlogV)
[代码]
#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef unsigned long long ull; int f[30]; template <typename T> inline void chkmax(T &x,T y) { x = max(x,y); } template <typename T> inline void chkmin(T &x,T y) { x = min(x,y); } template <typename T> inline void read(T &x) { T f = 1; x = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) if (c == '-') f = -f; for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + c - '0'; x *= f; } int main() { int n; read(n); for (int i = 1; i <= n; i++) { int x; read(x); int value = 0; for (int j = 0; j < 31; j++) if (x & (1 << j)) chkmax(value , f[j] + 1); for (int j = 0; j < 31; j++) if (x & (1 << j)) chkmax(f[j] , value); } int ans = 0; for (int i = 0; i < 31; i++) chkmax(ans , f[i]); printf("%d\n" , ans); return 0; }