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. 关于ueditor 文本框

    遇到一个问题,需要将从ueditor中的获得的带格式的文本,从数据库中取出,在放回到 ueditor中去,但是 文本中\n总是截断字符串,出现 这种情况,后面的字符就不能算到里面去了,程序就报错了. ...

  2. php中heredoc使用方法

    Heredoc技术,在正规的PHP文档中和技术书籍中一般没有详细讲述,只是提到了这是一种Perl风格的字符串输出技术.但是现在的一些论坛程序,和部分文章系统,都巧妙的使用heredoc技术,来部分的实 ...

  3. Python全栈开发:协程代码实例

    协程代码1 #!/usr/bin/env python # -*- coding;utf-8 -*- # 导入协程模块 """ 协程工作原理 ""&q ...

  4. [JZOJ5355] 【NOIP2017提高A组模拟9.9】保命

    题目 描述 题目已经足够清晰了,所以不再赘述题目大意. 思考历程 一眼看下去,好像是一道大水题! 然而,再看几眼,感觉又不是一道水题! 然后想了半天,感觉它特别难转移! 最终打了一个暴力,然后发现样例 ...

  5. C++指针类型间强制转换

    深入理解指针类型间的转换 C++中指针的强制转换 强制类型转换(int).(int&)和(int*)的区别 内存中的地址 地址的本质就是一串0和1的机器代码,内存中的地址没有明确数据类型,但地 ...

  6. springcluoud入门

    概念: Spring Cloud是一个分布式的整体解决方案. Spring Cloud 为开发者提供了在分布式系统(配置管理,服务发现,熔断,路由,微代理,控制总线,一次性token,全局琐,lead ...

  7. Laravel移除Cache-Control

    碰到一个问题,网站上线后,需要移除Cache-Control,就是下面这个东西 方案1 失败 参考网址:https://stackoverflow.com/questions/51821563/lar ...

  8. 表单复选框input[type="checkbox"]

    <!DOCTYPE html> <html lang="zh"> <head> <title></title> < ...

  9. 一个四五年的Java开发程序员,该准备哪些去面试?

    上周面试了一周,感触颇深,总结一下. 面试了公司大概有阿里,携程,爱奇艺,唯品会,途牛,bilibili,大众点评,阿里和爱奇艺是电话面试,其他现场面试. 首先,五年左右,应该算高级开发工程师,大部分 ...

  10. kafka comsumer

    kafka的顺序消费只保证在同一个partition中而已