Longest Ordered Subsequence 最长有序子序列

动态规划系列二(最长有序子序列) 1、问题描述 一个数的序列bi,当b1 < b2 < ... < bS的时候,我们称这个序列是上升的。对于给定的一个序列(a1, a2, ..., aN),我们可以得到一些上升的子序列(ai1, ai2, ..., aiK),这里1 <= i1 < i2 < ... < iK <= N。比如,对于序列(1, 7, 3, 5, 9, 4, 8),有它的一些上升子序列,如(1, 7), (3, 4, 阅读详情

Longest Ordered Subsequence

Problem Description
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 Output
4

题目大意:求输入序列元素中,最长的有序子序列的长度。例:1 7 3 5 9 4 8 最长的有序子序列为 1 3 5 8,长度为4。

动态规划,在此题中动态规划计算的是截止到第i个的最长增序子序列,dp数组初始化为1,即截止到每个位置的最长增序子序列为1,然后遍历每个位置 i,及每个位置之前的元素 j ,如果之前的元素小于当前位置,满足状态方程:dp[i] = Max(dp[i], dp[j]+1) ,具体参见代码。

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define Max(a,b) (a)>(b)?(a):(b)

//动态规划
//在此题中动态规划计算的是截止到第i个的最长增序子序列,
int main()
{
    int n;
    scanf("%d",&n);
    int a[n],dp[n];//dp 数组记录第i个数之前的最长子序列
    for(int i = 0; i<n; i++)
    {
        scanf("%d",&a[i]);
        dp[i] = 1;//每个位置的最长子序列初始化为1
    }
    for(int i = 1; i<n; i++)//从 1 开始遍历每一个位置的数
    {
        for(int j = 0; j<i; j++)//遍历第i个位置之前的数
        {
            if(a[j]<a[i])//如果小于,将第j位置的最长子序列加一与第i位置的最长子序列比较
                dp[i] = Max(dp[i],dp[j]+1);
        }
    }
    int m = 0;
    for(int i = 0; i<n; i++)
        m = Max(m,dp[i]);
    printf("%d\n",m);
    return 0;
}

7-7 最长有序子序列 对于给定一个数字序列 (a1​,a2​,…,an​) ,如果满足a1​ 阅读详情

相关推荐

Vivado Combinatorial Loop Alert问题 [DRC LUTLP-1]

Vvado综合和布局布线都通过了但是无法生成bit文件,提示Combinatorial Loop Alert,错误如下图。在RTL图中可以看到LUT的输出连接到了输入上,这个就是。在代码中我并未找到导致这种情况发生的地方,如果遇到类似的问题先检查自己的代码是不是存在组合逻辑连接出现了问题,如果没问题再考虑通过设置去解决问题。

伊丽莎白的学习之路 6038

最长有序子序列

什么是最长有序子序列呢? 首先要明白子序列和子串不同,可以不连续,然后保证有序就是有序子序列了。 比如有如下数组: I 0 1 2 3 4 5 6 7 8 Num[I] 1 4 7 2 5 8

u014285517的专栏 3795

HIL(硬件在环)技术汇总梳理

HIL(Hardware-in-the-Loop)测试领域的知名公司有dSPACE、NI、Vector和speedgoat等,本文是针对这几家HIL技术的对比分析。

Mr.Cssust的博客 3708

最长有序子序列—动态规划算法

动态规划使用范围:(http://baike.baidu.com/view/28146.htm) 任何思想方法都有一定的局限性,超出了特定条件,它就失去了作用。同样,动态规划也并不是万能的。适用动态规划的问题必须满足最优化原理和无后效性。    1.最优化原理(最优子结构性质) 最优化原理可这样阐述:一个最优化策略具有这样的性质,不论过去状态和决策如何,对前面的决策所形成的状态而言,余下的诸决

lawrencesgj的专栏 9919

5G核心网Framed Routing功能解析:基于PFCP的帧路由机制与多终端互联实践

5G核心网Framed Routing功能解析:基于PFCP的帧路由机制与多终端互联实践

大家好,我是码农老gou,一名专注于编程技术的程序员。在这个博客中,我将与大家分享我在工作和学习中的经验、见解以及一些实用的技术干货。 1473

长短期记忆网络(Long Short-Term Memory networks, LSTM)

如何简单的理解LSTM——其实没有那么复杂:

weixin_39910711的博客 2万+

最长有序子序列(DP + 记录路径)

在这个题目中,上升序列,很容易

Change__的专栏 897

SAP中MRP运行计划行超限问题的分析与解决

SAP中基于生产性物料的采购特点,通常会采用采购计划协议的方式进行采购。其具体的采购计划通常来自于成品计划独立需求,然后通过物料清单,经由MRP跑出。 在采购计划协议中的计划行中,存在一个计划行号,随着MRP在不断跑出新的计划行,该行号资源会消耗完。当计划行号消耗完后,MRP不会再跑出新的计划行。且在MD04或MD02中看不到类似提示信息。此时可以通过MD05来查看是否出现类似下图的提示信息。 ...

专注SAP应用技术文章写作,致力于拉近用户与系统间的距离; 3849

Poj 2533-Longest Ordered Subsequence(最长有序子序列)

题意:就是求最长上升子序列。 分析:有朴素的O(N^2)的简单dp,也有O(nlogn)的二分思想。 方法一:dp,对应原序列的每个元素,dp数组记录包含这个数在内的与之前所有数一起能构成的最长有序子序列的长度,最后对dp数组遍历取最大值即可   #include #include #include #include using namespace std; int a[1005

我是一颗小星星~ 1196

Oracle PLM Agile936 单点登录配置方式

Oracle PLM Agile936 单点登录配置方式

Jay的个人博客 828

一个服务如何被找到、被调用、被看见:注册中心、发布注册、发现调用与服务监控的治理闭环

服务数量一多,“对方在哪儿”就成了时刻变化的变量。本文沿发布、注册、发现、调用、监控的治理闭环展开:注册中心的数据模型、健康检查与AP/CP取舍,自我保护、推空保护与本地快照三道自保机制;服务发布的启动器、动态代理、发布管理器、协议服务器与登记顺序;消费端的服务目录、请求ID配对、六种集群容错与三种发现模式;日志、指标、链路三支柱与熔断舱壁。配套五个Java演示程序实测:默认参数下宕机实例平均240秒才被摘除,优雅下线可降至45秒,统计窗口长于租约会让自我保护失效,尾部采样可捕获全部慢请求。

曾经“等你生日那天”都遥远得像未来,如今却可欢愉的挥手说“下个十年见” 169万+

最长有序子序列 Longest Ordered Subsequence POJ2533

Longest Ordered Subsequence Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 29968   Accepted: 13066 Description A numeric sequence of ai is ordered if a1 a2

u011455899的专栏 593

由ip地址和子网掩码求网络号、主机号、广播地址(超详细)

1、用二进制数表示IP地址   在进行计算之前,我们首先要学会十进制的IP地址怎么转换为二进制的IP地址。   给出一个例子: 十进制:172.16.25.3 二进制:10101100.00010000.00011001.00000011 1、如果两台主机的网络号相同,我们就说它们是同一网段 2、主机号的意义 - 1)主机号部分全变成1,即为该网段的广播地址 - 2)主机号部分全变成0,即为该网段的网路地址(网络号) 3、如果主机号部分为x位,则该网络能容纳最大主机数为: 2的x次方-2。减2

爱吃雪糕的小布丁的博客 9万+

Longest Ordered Subsequence-最长有序子序列-动态规划

xnk想要在家里造一个上楼的楼梯,众所周知,由于是上楼,而且xnk也不傻,一个楼梯的前一级肯定要小于下一级(即满足严格单调上升),由于xnk是x里村首富,当然要彰显自己的贵族气质,所以他希望楼梯越长越好。这个版本答案仍然时长时长很长,用二分好一些,用二分时长短一点,但我现在还不会,哈哈哈,会了再写个二分版本的。现在有n个木板(n

m0_74157667的博客 182

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

Longest Ordered Subsequence POJ - 2533 (最长有序子序列)

Longest Ordered SubsequenceTime Limit: 2000MS Memory Limit: 65536KTotal Submissions: 59701 Accepted: 26753DescriptionA numeric sequence of ai is ordered if a1 &lt; a2 &lt; ... &lt; aN. Let the subsequ...

howell 169

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

poj--2533 Longest Ordered Subsequence

Longest Ordered Subsequence Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 32721   Accepted: 14303 Description A numeric sequence of ai is ordered if a1 a2 

acm_1361677193的专栏 472

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

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

Longest Ordered Subsequence POJ - 2533 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, ...,

愿岁月如歌 423

ZOJ2136 Longest Ordered Subsequence【DP】

Longest Ordered Subsequence Time Limit: 2 Seconds      Memory Limit: 65536 KB A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given numeric sequence (a1,

海岛Blog 1060

POJ 2533 Longest Ordered Subsequence

POJ 2533 Longest Ordered Subsequence Description 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 (ai...

旺崽的博客 3015

Longest Ordered Subsequence

最长递增子序列 Description 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), ...

就和你作对!!! 192

Longest Ordered Subsequence(DP两种写法)

最长有序子序列 A numeric sequence ofaiis ordered ifa1<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 ...

m0_58245389的博客 772

poj 2533 Longest Ordered Subsequence

Longest Ordered Subsequence Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 31423   Accepted: 13727 Description A numeric sequence of ai is ordered if a1 a2 a

逸疏 1015

又是一篇题解:Longest Ordered Subsequence

关于动态规划的题解 写了一个01背包,而这个题却不是01背包… 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 exa

zhanggsyx的博客 278

HNU ACM OJ 10001 最长递增子序列

【代码】HNU ACM OJ 10001 最长递增子序列。

2401_83271720的博客 426

POJ2533:Longest Ordered Subsequence(最长上升子序列)

http://poj.org/problem?id=2533 Description 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 (ai...

189
上一篇: Palindrome 回文
下一篇: Goldbach‘s Conjecture 哥德巴赫猜想
大白菜MC
博客等级 码龄7年 10粉丝 28原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值