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 ...
随机推荐
- 查出当前操作数据库的登入名SUSER_NAME()
select SUSER_NAME() 一般和触发器一起用来监控是谁对表做了操作
- jquery.artDialog.source.js学习
1 关键的对象关系art = jQuery = $function artDialog() {...}artDialog.fn = artDialog.prototype = artDialog.fn ...
- (转)nginx配置location总结及rewrite规则写法
注: rewrite 只能对域名后边的除去传递的参数外的字符串起作用,并且要写全域名后面的部分,如: http://i.com:9090/php/midou/admin.php/index/login ...
- Matlab AlexNet 识别花
1. 首先,你要又并行计算的工具箱,在插件选项里面找到,安装即可 2. 下载训练的数据集,采用matlab演示的材料即可 https://matlabacademy-content.mathworks ...
- Pascal的sin^-1函数实现
function unsin(t:real):real; var l,r,ans,mid:longint; function dsin(z:real):real; begin exit(sin(z*p ...
- php实现在不同国家显示网站的不同语言版本
首先,你的网站本身要拥有多个语言版本.不然的话你就只能用JS去转化了. 1.通过ip去定位,这个要引用到第三方的接口进行数据的完整返回,但是不知道是我的网速太慢还是什么原因,个人觉得这个方法会卡顿: ...
- [JZOJ3168] 【GDOI2013模拟3】踢足球
题目 描述 题目大意 有两个队伍,每个队伍各nnn人. 接到球的某个人会再下一刻随机地传给自己人.敌人和射门,射门有概率会中. 每次射门之后球权在对方111号选手. 某个队伍到了RRR分,或者总时间到 ...
- Python 变量与数据类型
1.变量命名规则: 变量名只能是字母,数字和下划线的任意组合 变量名第一个字符不能是数字 变量名区分大小写,大小写字母被认为是两个不同的字符 特殊关键字不能命名为变量名 2.数值的运算 print ( ...
- LinkedHashMap笔记
一.最基本元素存储单元 /** * HashMap.Node subclass for normal LinkedHashMap entries. */ static class Entry<K ...
- 阿里重磅开源首款自研科学计算引擎Mars,揭秘超大规模科学计算
日前,阿里巴巴正式对外发布了分布式科学计算引擎 Mars 的开源代码地址,开发者们可以在pypi上自主下载安装,或在Github上获取源代码并参与开发. 此前,早在2018年9月的杭州云栖大会上,阿里 ...