51nod 最长单增子序列(动态规划)
(LIS Longest Increasing Subsequence)给定一个数列,从中删掉任意若干项剩余的序列叫做它的一个子序列,求它的最长的子序列,满足子序列中的元素是单调递增的。
第1行:1个数N,N为序列的长度(2 <= N <= 50000)
第2 - N + 1行:每行1个数,对应序列的元素(-10^9 <= S[i] <= 10^9)
输出最长递增子序列的长度。
8
5
1
6
8
2
4
5
10
5
其实我们可以加一项dp[i – 1][x + 1] = +∞
们考虑a[i]这一项有什么用。我们需要找到dp[i – 1][y] < a[i]把它接到长度为y的子序列后面,形成一个长度为y +
1的子序列。如果dp[i – 1][y + 1] < a[i], 这说明不属于a[i]这一项,我们考虑前个数也可以形成长度为y +
1的单增子序列,且最后一项更小,所以我们不应该更新它。事实上我们需要找到dp[i – 1][y] < a[i] && dp[i – 1][y + 1] >= a[i], 这样把a[i]接在长度为y的子序列后面形成一个长度为(y + 1)的子序列,同时结尾更小。
于是我们有递推关系:
dp[i][y + 1] = a[i]
dp[i][y + 2..] = dp[i – 1][y + 2…]
因为每次只更新一个值,我们dp数组只存第二维就可以了。最终的结果,其实是max {x| dp[x] < +∞}的x。
时间复杂度,二分是O(logn),所以总时间复杂度是O(nlogn)。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <time.h>
#include <string>
#include <stack>
#include <vector>
#include <set>
#include <queue>
using namespace std;
int n,a[],d[],i,j,len;
int binsearch(int x)
{
int l = , r = len, mid;
while (l <= r)
{
mid = (l + r) >> ;
if (d[mid-] <= x && x < d[mid]) return mid;
else if (x > d[mid]) l = mid + ;
else r = mid - ;
}
}
int main()
{
scanf ("%d", &n);
for (i = ; i<= n; i++)
scanf ("%d", &a[i]);
memset (d, , sizeof (d));
d[] = a[];
len = ;
for (i = ; i <= n; i++)
{
if (a[i] < d[]) j = ;
else if (a[i] > d[len]) j = ++len;
else j = binsearch (a[i]);
d[j] = a[i];
}
printf ("%d\n", len);
return ;
}
下面的代码只能过四分之一的数据,因为超时。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <time.h>
#include <string>
#include <stack>
#include <vector>
#include <set>
#include <queue>
using namespace std;
int main()
{
int n,len,i,j,dp[];
int a[];
cin>>n;
for(i=; i<n; i++)cin>>a[i];
int maxn=;dp[]=;
for(int i=; i<n; i++)
{
dp[i]=;
for(int j=; j<i; j++)
{
if(a[i]>a[j])
{
dp[i]=max(dp[i],dp[j]+);
}
}
maxn=max(maxn,dp[i]);
}
cout<<maxn<<endl;
return ;
}
51nod 最长单增子序列(动态规划)的更多相关文章
- 51nod 最长公共子序列问题(动态规划)(LCS)(递归)
最长公共子序列问题 输入 第1行:字符串A 第2行:字符串B (A,B的长度 <= 1000) 输出 输出最长的子序列,如果有多个,随意输出1个. 输入示例 abcicba abdkscab 输 ...
- - > 动规讲解基础讲解七——最长单增子序列
(LIS Longest Increasing Subsequence)给定一个数列,从中删掉任意若干项剩余的序列叫做它的一个子序列,求它的最长的子序列,满足子序列中的元素是单调递增的. 例如给定序列 ...
- 【ACM】最长公共子序列 - 动态规划
最长公共子序列 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 咱们就不拐弯抹角了,如题,需要你做的就是写一个程序,得出最长公共子序列.tip:最长公共子序列也称作最 ...
- 最长上升子序列(动态规划递推,LIS)
1759:最长上升子序列 题目: 总时间限制: 2000ms 内存限制: 65536kB 描述 一个数的序列bi,当b1 < b2 < ... < bS的时候,我们称这个序列是上升的 ...
- C++求解汉字字符串的最长公共子序列 动态规划
近期,我在网上看了一些动态规划求字符串最长公共子序列的代码.可是无一例外都是处理英文字符串,当处理汉字字符串时.常常会出现乱码或者不对的情况. 我对代码进行了改动.使用wchar_t类型存储字 ...
- [LeetCode] 300. 最长上升子序列 ☆☆☆(动态规划 二分)
https://leetcode-cn.com/problems/longest-increasing-subsequence/solution/dong-tai-gui-hua-she-ji-fan ...
- 51nod 最长递增子序列
nlogn版最长递增子序列.线段树.(其实常数蛮大的....) #include<iostream> #include<cstring> #include<algorit ...
- 九度OJ 1533 最长上升子序列 -- 动态规划
题目地址:http://ac.jobdu.com/problem.php?pid=1533 题目描述: 给定一个整型数组, 求这个数组的最长严格递增子序列的长度. 譬如序列1 2 2 4 3 的最长严 ...
- 300. Longest Increasing Subsequence(LIS最长递增子序列 动态规划)
Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...
随机推荐
- Conjugate 解题报告
Conjugate 问题描述 在不存在的 \(\text{noip day3}\) 中,小 \(\text{w}\) 见到了一堆堆的谜题. 比如这题为什么会叫共轭? 他并不知道答案. 有 \(n\) ...
- 如何加快JavaScript的加载与执行
JS 有个很无语的阻塞特性,就是当浏览器在执行JS 代码时,不能同时做其他任何事情,无论其代码是内嵌的还是外部的. 浏览器在碰到一个引入外部JS 文件的<script>标签时会停下所有工作 ...
- echarts中图表过于靠左或靠右的情况解决办法。
经过好多次尝试,终于在http://blog.csdn.net/ainuser/article/details/76641963中得到解决. grid: { x: '30%', //相当于距离左边效果 ...
- Backup and Restore MySQL Database using mysqlhotcopy
mysqlhotcopy is a perl script that comes with MySQL installation. This locks the table, flush the ta ...
- centos安装net-speeder
以前介绍过VPS上安装锐速对VPS的加速效果,但是这货对 Linux 内核有要求,一般就只能在XEN或者KVM的机子上安装.不过还好锐速有一个免费的代替品:net-speeder,所以这里介绍一下 D ...
- hbase vs mongodb
1.HBase依赖于HDFS,HBase按照列族将数据存储在不同的hdfs文件中:MongoDB直接存储在本地磁盘中,MongoDB不分列,整个文档都存储在一个(或者说一组)文件中 (存储) 2.Mo ...
- WebForm 在 Global.asax 中捕获全局异常
/// <summary> /// 捕获全局异常 /// </summary> /// <param name="sender">sender& ...
- 拉格朗日乘数法 和 KTT条件
预备知识 令 \(X\) 表示一个变量组(向量) \((x_1, x_2, \cdots, x_n)\) 考虑一个处处可导的函数 \(f(X)\), 为了方便描述, 这里以二元函数为例 对于微分, 考 ...
- Ubuntu下hadoop集群搭建
--修改IP地址(克隆镜像后可修改可不修改) http://jingyan.baidu.com/article/e5c39bf5bbe0e739d7603396.html -------------- ...
- 01-UIDynamic简单演练demo源代码
源代码下载: 01-简单演练.zip72.0 KB // // ViewController.m // 01.简单演练 // // Created by apple on 13-12-24. / ...