D - POJ 2533 经典DP-最长上升子序列

A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given numeric sequence ( a1a2, ..., aN) be any sequence ( ai1ai2, ..., 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 最小上升子序列的更多相关文章

  1. poj 2533 Longest Ordered Subsequence 最长递增子序列

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4098562.html 题目链接:poj 2533 Longest Ordered Subse ...

  2. nyoj 17-单调递增最长子序列 && poj 2533(动态规划,演算法)

    17-单调递增最长子序列 内存限制:64MB 时间限制:3000ms Special Judge: No accepted:21 submit:49 题目描述: 求一个字符串的最长递增子序列的长度 如 ...

  3. POJ 2533 Longest Ordered Subsequence(LIS模版题)

    Longest Ordered Subsequence Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 47465   Acc ...

  4. 最小正子序列(序列之和最小,同时满足和值要最小)(数据结构与算法分析——C语言描述第二章习题2.12第二问)

    #include "stdio.h" #include "stdlib.h" #define random(x) (rand()%x) void creat_a ...

  5. OpenJudge 2757 最长上升子序列 / Poj 2533 Longest Ordered Subsequence

    1.链接地址: http://poj.org/problem?id=2533 http://bailian.openjudge.cn/practice/2757 2.题目: 总Time Limit: ...

  6. POJ 2533 - Longest Ordered Subsequence - [最长递增子序列长度][LIS问题]

    题目链接:http://poj.org/problem?id=2533 Time Limit: 2000MS Memory Limit: 65536K Description A numeric se ...

  7. POJ - 2533 Longest Ordered Subsequence(最长上升子序列)

    d.最长上升子序列 s.注意是严格递增 c.O(nlogn) #include<iostream> #include<stdio.h> using namespace std; ...

  8. 题解报告:poj 2533 Longest Ordered Subsequence(最长上升子序列LIS)

    Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence ...

  9. Longest Ordered Subsequence POJ - 2533 最长上升子序列dp

    题意:最长上升子序列nlogn写法 #include<iostream> #include<cstdio> #include<cstring> #include&l ...

随机推荐

  1. Python操作三大主流数据库✍✍✍

    Python操作三大主流数据库 Python 标准数据库接口为 Python DB-API,Python DB-API为开发人员提供了数据库应用编程接口. Python 数据库接口支持非常多的数据库, ...

  2. 侧滑关闭Activity的解决方案——SwipeBackLayout

    项目地址:ikew0ng/SwipeBackLayout: An Android library that help you to build app with swipe back gesture. ...

  3. 面试系列16 redis的持久化

    1.RDB和AOF两种持久化机制的介绍 RDB持久化机制,对redis中的数据执行周期性的持久化 AOF机制对每条写入命令作为日志,以append-only的模式写入一个日志文件中,在redis重启的 ...

  4. wpf 获取image控件的图片并保存

    XMAL代码如下: <Image Name="ImageToSave" Source="Images/pic_bg.png" Grid.RowSpan=& ...

  5. 图片上传的ImageIO工具类

    ImageIO类说明 最近的项目中遇到ImageIO,因此记录下这个类的用法 一.ImageIO: 这个类中的方法都是静态方法,可以用来进行简单的图片IO操作 1.读入的三种方法 public sta ...

  6. Apache下更改.htaccess文件名称

    有时候我们需要更改.htaccess的名称以解决一些问题 比如:Eclipse下是不显示点开头的文件的 所以我们可以使用  Apache的AccessFileName来更改此配置文件的名称 Acces ...

  7. Oracle SQL性能优化【转】

    (1)      选择最有效率的表名顺序(只在基于规则的优化器中有效):ORACLE的解析器按照从右到左的顺序处理FROM子句中的表名,FROM子句中写在最后的表(基础表 driving table) ...

  8. Windows相关命令

    1.查看端口8080被哪个进程占用 netstat -ano | findstr "8080" 2.查看进程号为5768对应的进程 tasklist | findstr " ...

  9. 边缘节点服务ENS重磅升级 阿里云首次定义“边缘云计算”概念层层深入

    随着5G.物联网时代的到来以及云计算应用的逐渐增加,传统集中式的云计算技术已经无法满足终端侧“大连接,低时延,大带宽”的需求.结合边缘计算的概念,云计算将必然发展到下一个技术阶段,也就是将云计算的能力 ...

  10. Python-数据类型内置方法(2)

    目录 元组(tuple) 内置方法: 字典(dict) 内置方法: 优先掌握: 需要掌握 集合(set) 优先掌握 深浅拷贝 拷贝(赋值) 浅拷贝 深拷贝 总结 存值个数 有序 or 无序 可变 or ...