Luogu 4310 绝世好题
BZOJ 4300
先把这堆东西丢到博客里,以后再复习。
首先考虑暴力的$dp$,设$f_i$表示以$i$结尾的满足条件的序列的最长长度,有:
$f_i = max(f_j) + 1$ $j < i $ $,$ $ a_j \& a_i \neq 0$
$ans = max(f_i)$ $1 \leq i \leq n$
这样是$n^2$的。
考虑二进制意义下的按位与,如果要使这个运算的结果不为$0$的话,必须要有一位两个数都是$1$,那么我们可以考虑拆位进行$dp$,设$g_j$表示第$j$位是$1$结尾的最长的满足条件的序列的长度。对于每一个$i$,有:
$f_i = max(g_j) + 1$ $0 \leq j \leq 32$ 并且$a_i$的第$j$位为$1$。
$g_j = max(f_i, g_j)$ $a_i$的第$j$位为$1$。
$ans = max(f_i)$ $1 \leq i \leq n$
事实上在写的时候并没有必要把$f$开出来。
时间复杂度$O(nlog(Maxn))$。
Code:
#include <cstdio>
#include <cstring>
using namespace std; const int N = 1e5 + ;
const int M = ; int n, a[N], f[M]; inline void read(int &X) {
X = ; char ch = ; int op = ;
for(; ch > '' || ch < ''; ch = getchar())
if(ch == '-') op = -;
for(; ch >= '' && ch <= ''; ch = getchar())
X = (X << ) + (X << ) + ch - ;
X *= op;
} inline void chkMax(int &x, int y) {
if(y > x) x = y;
} int main() {
read(n);
for(int i = ; i <= n; i++) read(a[i]); int ans = ;
for(int i = ; i <= n; i++) {
int now = ;
for(int j = ; j <= ; j++)
if((a[i] >> j) & ) chkMax(now, f[j]);
++now;
chkMax(ans, now);
for(int j = ; j <= ; j++)
if((a[i] >> j) & ) chkMax(f[j], now);
} printf("%d\n", ans);
return ;
}
Luogu 4310 绝世好题的更多相关文章
- 【洛谷】4310: 绝世好题【二进制DP】
P4310 绝世好题 题目描述 给定一个长度为n的数列ai,求ai的子序列bi的最长长度,满足bi&bi-1!=0(2<=i<=len). 输入输出格式 输入格式: 输入文件共2行 ...
- bzoj 4300: 绝世好题
4300: 绝世好题 Time Limit: 1 Sec Memory Limit: 128 MB Description 给定一个长度为n的数列ai,求ai的子序列bi的最长长度,满足bi& ...
- BZOJ 4300: 绝世好题 动态规划
4300: 绝世好题 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=4300 Description 给定一个长度为n的数列ai,求ai的 ...
- 【递推】BZOJ 4300:绝世好题
4300: 绝世好题 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 564 Solved: 289[Submit][Status][Discuss] ...
- bzoj 4300: 绝世好题 dp
4300: 绝世好题 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/problem.php ...
- bzoj千题计划190:bzoj4300: 绝世好题
http://www.lydsy.com/JudgeOnline/problem.php?id=4300 f[i] 表示第i位&为1的最长长度 #include<cstdio> # ...
- HYSBZ(BZOJ) 4300 绝世好题(位运算,递推)
HYSBZ(BZOJ) 4300 绝世好题(位运算,递推) Description 给定一个长度为n的数列ai,求ai的子序列bi的最长长度,满足bi&bi-1!=0(2<=i<= ...
- P4310 绝世好题
P4310 绝世好题 题目描述 给定一个长度为n的数列ai,求ai的子序列bi的最长长度,满足bi&bi-1!=0(2<=i<=len). 说明 对于100%的数据,1<=n ...
- 【BZOJ4300】绝世好题(动态规划)
[BZOJ4300]绝世好题(动态规划) 题面 BZOJ Description 给定一个长度为n的数列ai,求ai的子序列bi的最长长度,满足bi&bi-1!=0(2<=i<=l ...
随机推荐
- 剑指offer--6.数值的整数次方
时间限制:1秒 空间限制:32768K 热度指数:362909 题目描述 给定一个double类型的浮点数base和int类型的整数exponent.求base的exponent次方. class S ...
- pcre函数详解
PCRE是一个NFA正则引擎,不然不能提供完全与Perl一致的正则语法功能.但它同时也实现了DFA,只是满足数学意义上的正则. PCRE提供了19个接口函数,为了简单介绍,使用PCRE内带的测试程序( ...
- DedeCMS织梦模板标签调用大全
本文转载:http://www.mubanzhijia.com/jishujiaocheng/1.html 关键描述调用标签: <meta name="keywords" c ...
- AtCoder Petrozavodsk Contest 001 B - Two Arrays
Time limit : 2sec / Memory limit : 256MB Score : 300 points Problem Statement You are given two inte ...
- 团队队列(列和map结合的经典运用)
Queues and Priority Queues are data structures which are known to most computer scientists. The Team ...
- LeetCode Continuous Subarray Sum
原题链接在这里:https://leetcode.com/problems/continuous-subarray-sum/description/ 题目: Given a list of non-n ...
- jsp中引入JavaScript的方法
1:在页面中直接嵌入JavaScript <script language="javascript">..........</script> 2:链接外部J ...
- vertex shader must minimally write all four components of POSITION
Though the POSITION semantic must be written out by the vertex shader, it cannot be read in by the p ...
- 如何找回未保存过的 Excel 文件?
如何找回未保存过的 Excel 文件? 同事做了一个文件,未保存直接关闭,正常是找不回来的. 但是 Excel 有一个强大的自动保存功能,对没有保存的文件也可以找回.
- WCF svcutil工具
通过SvcUtil.exe生成客户端代码和配置 WCF服务调用通过两种常用的方式:一种是借助代码生成工具SvcUtil.exe或者添加服务引用的方式,一种是通过ChannelFactory直接创建服务 ...