【u023】最长上升子序列(sequence)
Time Limit: 1 second
Memory Limit: 128 MB
【问题描述】
非常经典的问题,拿来给大家练手了。 序列 { 1,2,...,n } 的一个子序列是指序列 { i1, i2, ……, ik },其中 1<=i1 < i2 < …… < ik<=n, 序列 { a1, a2, ……, an } 的一个子序列是指序列 { ai1, ai2, ……, aik },其中 { i1, i2, ……, ik } 是 { 1, 2, ……, n } 的一个子序列.同时,称 k 为此子序列的长度. 如果 { ai1, ai2, ……, aik } 满足 ai1 ≤ ai2 ≤ …… ≤
aik,则称之为上升子序列.如果不等号都是严格成立的,则称之为严格上升子序列.同理,如果前面不等关系全部取相反方向,则称之为下降子序列和严格下降子序列. 长度最长的上升子序列称为最长上升子序列.本问题对于给定的整数序列,请求出其最长上升子序列的长度.
【输入格式】
第一行给出一个数字N,N < = 5000,表示给定数列的长度。第二行有N个整数, 表示数列中的元素
【输出格式】
输出K的极大值,即最长上升子序列的长度
【数据规模】
Sample Input1
5
9 3 6 2 7
Sample Output1
3
【样例说明】
最长上升子序列为3,6,7
【题解】
程序1,用f[i]表示以i作为最后一个元素的最长上升子序列长度,f[i] = max(f[i],f[j]+1),其中 a[i] >= a[j]; j ∈(1,i-1);
程序2,用f[i]表示长度为i的最长上升子序列的最后一个元素的最小值,在扫描输入的数组a时,如果a[i] > f[maxl],那么maxl++,f[maxl] = a[i],否则 从maxl-1 到 1扫描一下,
找到最大的j使得a[j] <= a[i] ,然后 f[j+1] = a[i];即更新长度为j+1的最长上升子序列的最后一个数的最小值。最后输出maxl就好。
程序2可以用二分查找来优化做到nlogn,但是二分查找写起来很麻烦、
【程序1】
#include <cstdio> int n,a[5002],f[5002],ans = 0; void input_data()
{
scanf("%d",&n);
for (int i = 1;i <= n;i++)
scanf("%d",&a[i]);
} int ma_x( int a,int b)
{
if (a > b)
return a;
else
return b;
} void get_ans()
{
for (int i = 1;i <= n;i++)
{
f[i] = 1;//f[i] == 1表示 从这个数字开始新的一个序列 长度为1
for (int j = 1;j <= i-1;j++) //或者从之前的序列中更新,让这个数字作为最后一个数
{
if (a[i] >= a[j]) //前提是这个数字要比前面的数字大 且长度加上1之后会比这个数字作为其他序列的最后一个数字来的更好(长度更长)
f[i] = ma_x(f[i],f[j] + 1);
}
if (f[i] > ans) // 如果比最优解更优 则更新
ans = f[i];
}
} void output_ans()
{
printf("%d\n",ans);
} int main()
{
input_data();
get_ans();
output_ans();
return 0;
}
【程序2】
#include <cstdio> int n,a[5002],f[5002],ans = 0,maxl = 0; void input_data()
{
scanf("%d",&n);
for (int i = 1;i <= n;i++)
scanf("%d",&a[i]);
for (int i = 1;i <= n;i++)
f[i] = 0;
f[0] = -200000000;//这个f[0]的边界 可以方便后面的求解
} void get_ans()
{
for (int i = 1;i <= n;i++)
{
if (a[i] >= f[maxl]) //如果比最长长度的序列的最后一个数字大 就可以更新长度了,这样是最优的
{
maxl++;
f[maxl] = a[i];
} //没有办法更新最大长度 就在前面的数字中找,更新一下最后一个数字的最小值
else
for (int j = maxl-1; j >=0; j--) //从大到小更新这样保证是最大的j
if (f[j] <= a[i])
{
f[j+1] = a[i];
break;
}
}
} void output_ans()
{
printf("%d\n",maxl);
} int main()
{
//freopen("F:\\rush.txt","r",stdin);
input_data();
get_ans();
output_ans();
return 0;
}
【u023】最长上升子序列(sequence)的更多相关文章
- HOJ 2985 Wavio Sequence(最长递增子序列以及其O(n*logn)算法)
Wavio Sequence My Tags (Edit) Source : UVA Time limit : 1 sec Memory limit : 32 M Submitted : 296, A ...
- 子序列 sub sequence问题,例:最长公共子序列,[LeetCode] Distinct Subsequences(求子序列个数)
引言 子序列和子字符串或者连续子集的不同之处在于,子序列不需要是原序列上连续的值. 对于子序列的题目,大多数需要用到DP的思想,因此,状态转移是关键. 这里摘录两个常见子序列问题及其解法. 例题1, ...
- HDU 3998 Sequence(经典问题,最长上升子序列)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3998 解题报告:求一个数列的最长上升子序列,并求像这样长的不相交的子序列最多有多少个. 我用的是最简单 ...
- HDU 3998 Sequence (最长上升子序列+最大流)
参考链接:http://www.cnblogs.com/gentleh/archive/2013/03/30/2989958.html 题意:求一个序列的最长上升子序列,及其个数(注意:两个最长上升子 ...
- 【最长下降子序列】【动态规划】【二分】XMU 1041 Sequence
题目链接: http://acm.xmu.edu.cn/JudgeOnline/problem.php?id=1041 题目大意: 一个二维平面,上面n(n<=1 000 000)个点.问至少选 ...
- uva111动态规划之最长公共子序列
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=74662#problem/C A B C D E C - Largest Rect ...
- UVa 111 - History Grading (by 最长公共子序列 )
History Grading Background Many problems in Computer Science involve maximizing some measure accor ...
- 2.16 最长递增子序列 LIS
[本文链接] http://www.cnblogs.com/hellogiser/p/dp-of-LIS.html [分析] 思路一:设序列为A,对序列进行排序后得到B,那么A的最长递增子序列LIS就 ...
- BUY LOW, BUY LOWER_最长下降子序列
Description The advice to "buy low" is half the formula to success in the bovine stock mar ...
随机推荐
- cron 简单任务调度 go
package main import ( "github.com/robfig/cron" "log" ) func main() { i := 0 c := ...
- AtomicInteger类
今天写代码.尝试使用了AtomicInteger这个类,感觉使用起来非常爽,特别适用于高并发訪问.能保证i++,++id等系列操作的原子性. ++i和i++操作并非线程安全的.非常多人会用到synch ...
- golang beego cache
package main import ( "fmt" "github.com/astaxie/beego/cache" "time" ) ...
- sass自定义滚动条样式
@mixin scrollBarStyle() { &::-webkit-scrollbar { width: 7px; height: 7px; } &::-webkit-scrol ...
- vue项目使用axios
使用: npm install axios --save-dev 在main.js中import: 使用: (1):POST方式 let data= [{receiveAdd:receiveAddVa ...
- Shiro学习总结(10)——Spring集成Shiro
1.引入Shiro的Maven依赖 [html] view plain copy <!-- Spring 整合Shiro需要的依赖 --> <dependency> <g ...
- diff命令具体解释
diff命令參数: diff - 找出两个文件的不同点 总览 diff [选项] 源文件 目标文件 以下是 GNU所接受的 diff 的全部选项的概要. 大多数的选项有两个同样的名字,一个是单个的跟在 ...
- 1.3 Quick Start中 Step 1: Download the code官网剖析(博主推荐)
不多说,直接上干货! 一切来源于官网 http://kafka.apache.org/documentation/ 不要局限于,这个版本,我只是以最新的版本,来做个引子,让大家对官网的各个kafka版 ...
- Flume Channel Selectors官网剖析(博主推荐)
不多说,直接上干货! Flume Sources官网剖析(博主推荐) Flume Channels官网剖析(博主推荐) 一切来源于flume官网 http://flume.apache.org/Flu ...
- 洛谷—— P1967 货车运输 || COGS——C 1439. [NOIP2013]货车运输
https://www.luogu.org/problem/show?pid=1967#sub || http://www.cogs.pro/cogs/problem/problem.php?pi ...