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 ...
随机推荐
- shell脚本练习01
######################################################################### # File Name: 4.5.sh # Auth ...
- Ubuntu环境下使用Maven编译并打包Java项目
一.安装Maven 打开终端输入以下指令: $ mvn -v Apache Maven Maven home: /usr/share/maven Java version: 1.8.0_181, ve ...
- Python-面向对象之高级进阶
目录 classmethod与staticmethod 面向对象高级 isinstance.issubclass 反射 魔法方法(类内置方法) 单例模式 classmethod与staticmetho ...
- 记录一次dubbo不能正常抛出特定异常
BUG场景 今天同事的代码中出现一个问题,让我帮忙排查一下.原代码大致如下 dubbo服务消费者: @Resource private IPayWayService payWayService; @R ...
- codeforces 1100E-Andrew and Taxi
传送门:QAQQAQ 题意:给你一个图,每条边有边权,现在你可以对边进行反转,使图中不存在环,你需要使得反转的边的边权集合中的最大值最小,并输出任意一组解. 思路:二分+拓扑排序 使得最大值最小,自然 ...
- Struts2接受请求参数三种常用方法
一 接受请求参数主要有三种:属性驱动 对象驱动 模型驱动<model Driven> 方式1:在Action中接收请求参数不需要使用request对象,在Action中定义与请求参数相同名 ...
- vue表格之:formatter=fun
作用:对从数据库中取出的数据进行处理后展示. 示例1: partner1为从数据库中查询出的展示字段,值为数字id,需要转化成中文名称 partners为全量查询结果,包含了id与中文名称的一一对应关 ...
- 08-2-if的其他写法
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- NtQuerySystemInformation 枚举进程
函数原型: NTSTATUS WINAPI NtQuerySystemInformation( _In_ SYSTEM_INFORMATION_CLASS SystemInformat ...
- 3、mysql读写性能优化方法
1.当表格特别多的时候,所新建的表格一定注意索引,数据库内部对索引的处理能够很好的优化查询读写性能