POJ 2533 最小上升子序列
D - POJ 2533 经典DP-最长上升子序列
A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given numeric sequence ( a1, a2, ..., aN) be any sequence ( ai1, ai2, ..., aiK), where 1 <= i1 < i2 < ... < iK <= N. For example, sequence (1, 7, 3, 5, 9, 4, 8) has ordered subsequences, e. g., (1, 7), (3, 4, 8) and many others. All longest ordered subsequences are of length 4, e. g., (1, 3, 5, 8).
Your program, when given the numeric sequence, must find the length of its longest ordered subsequence.
Input
The first line of input file contains the length of sequence N. The second line contains the elements of sequence - N integers in the range from 0 to 10000 each, separated by spaces. 1 <= N <= 1000
Output
Output file must contain a single integer - the length of the longest ordered subsequence of the given sequence.
Sample Input
7
1 7 3 5 9 4 8
Sample Outout
4
答
经典的DP,但是作为新手一开始想岔了,想着状态方程best(i)是表示前 i 个数字的最长子序列长度,然后用一个数组记录前 i 个数字的最长自序列的最后一项,则best(i)就等于将第i个数和组成前i - 1 个最长序列的最后一个一项进行比较, 若大于最后一项,则长度+1, 否则保持原长度。最后从i = N,一步步递归到i = 1。
但很明显这种想法错了,因为并不能满足最优子结构,一开始觉得自己一下就找到方法了就没细想....
正确答案是,best(i)表示以第i个数结尾的最长子序列,这样就需要两层循环,外层i = 1 ------>i = N, 内层 j = 1 ------>j = i - 1
AC代码如下
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#pragma warning ( disable : 4996 ) using namespace std; int N, map[], best[];
int max; int main()
{ while ( ~scanf( "%d", &N ) )
{
memset( map, , sizeof(map) );
memset( best, , sizeof(best) );
//memset( dp, 0, sizeof(map) ); ///////////////////////////////////////////////////////
for( int i = ; i <= N; i++ )
scanf( "%d", &map[i] );
///////////////////////////////////////////////////////
for ( int i = ; i <= N; i++ )
{
int max = ;
for ( int j = ; j < i; j++ )
if( map[i] > map[j] && max < best[j] )
max = best[j];
best[i] = max + ;
}
int max = ;
for( int i = ; i <= N; i++ )
if( max < best[i] )
max = best[i];
cout << max << endl;
}
return ;
}
POJ 2533 最小上升子序列的更多相关文章
- poj 2533 Longest Ordered Subsequence 最长递增子序列
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4098562.html 题目链接:poj 2533 Longest Ordered Subse ...
- nyoj 17-单调递增最长子序列 && poj 2533(动态规划,演算法)
17-单调递增最长子序列 内存限制:64MB 时间限制:3000ms Special Judge: No accepted:21 submit:49 题目描述: 求一个字符串的最长递增子序列的长度 如 ...
- POJ 2533 Longest Ordered Subsequence(LIS模版题)
Longest Ordered Subsequence Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 47465 Acc ...
- 最小正子序列(序列之和最小,同时满足和值要最小)(数据结构与算法分析——C语言描述第二章习题2.12第二问)
#include "stdio.h" #include "stdlib.h" #define random(x) (rand()%x) void creat_a ...
- OpenJudge 2757 最长上升子序列 / Poj 2533 Longest Ordered Subsequence
1.链接地址: http://poj.org/problem?id=2533 http://bailian.openjudge.cn/practice/2757 2.题目: 总Time Limit: ...
- POJ 2533 - Longest Ordered Subsequence - [最长递增子序列长度][LIS问题]
题目链接:http://poj.org/problem?id=2533 Time Limit: 2000MS Memory Limit: 65536K Description A numeric se ...
- POJ - 2533 Longest Ordered Subsequence(最长上升子序列)
d.最长上升子序列 s.注意是严格递增 c.O(nlogn) #include<iostream> #include<stdio.h> using namespace std; ...
- 题解报告:poj 2533 Longest Ordered Subsequence(最长上升子序列LIS)
Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence ...
- Longest Ordered Subsequence POJ - 2533 最长上升子序列dp
题意:最长上升子序列nlogn写法 #include<iostream> #include<cstdio> #include<cstring> #include&l ...
随机推荐
- python数据池,python3编码str转bytes,encode
一.python2 python3的区别 默认编码:2--ASCII码 3---UTF-8 print:python2 可以不需要加括号(),python3必须加括号 python2中有range, ...
- Neo4j-APOC使用总结(一)
一.安装APOC 1.下载jar包:https://github.com/neo4j-contrib/neo4j-apoc-procedures/releases 2.把jar包放在安装目录的plug ...
- [WPF自定义控件库] 让Form在加载后自动获得焦点
原文:[WPF自定义控件库] 让Form在加载后自动获得焦点 1. 需求 加载后让第一个输入框或者焦点是个很基本的功能,典型的如"登录"对话框.一般来说"登录" ...
- iOS逆向系列-脱壳
概述 通过iOS逆向系列-逆向App中使用class-dump工具导出App的Mach-O文件所有头文件.Hopper工具分析App的Mach-O文件代码大概实现.但是这些前体是App的Mach-O没 ...
- springboot整合shiro的使用
shiro的原理已经有博客了,自己写可以问度娘 参考https://www.cnblogs.com/liyinfeng/p/8033869.html 此处直接描述实际使用 一.pom.xml引包 &l ...
- java 压缩包
package com.gome.budget.common.utils; import org.apache.commons.compress.archivers.ArchiveEntry; imp ...
- opencv编译:opencv 3.4.1 编译 contrib模块,增加人脸识别
start cmake-gui select the opencv source code folder and the folder where binaries will be built (th ...
- eclipse中启动tomcat之后,项目一直重复部署导致内存报警!!!
项目环境:jdk1.8+tomcat8.0; 出现该问题的原因:目前还没有确定,网友有很多的说法. 但是我在部署其中一个项目时出现此情况,在同样的环境下部署另外一个项目未出现此情况. 其中的一种解决方 ...
- AM运行中的垃圾数据清理
1.下载 filetmpclear.bat 2.打开 这个批处理文件,修改参数 3.按下图 第一个红色框内, 地址: 可以在 AM8服务管理器- 文件服务 ,数据路径后再加 \__Temp__ 第 ...
- sort的cmp函数
sort的cmp函数只能写return a>b;或者return a<b;