最长单调子序列

最长单调递增子序列——动态规划算法 题目简介 最长单调递增子序列是一个很经典的问题,他所需要的就是要在整个序列中(序列一般来讲应该是无序的)寻找出一个最长的升序子序列,这个子序列中的所有元素都必须来自这个原序列,它们可以在原序列中是不相邻的,但是它们之间的相对位置是不可以改动的。比如一个序列: (1, 7, 3, 5, 9, 4, 8) 它有一些上升子序列,如(1, 7), (3, 4, 8)等等。在这些子序列中,长度最长的子序列... 阅读详情

给定一个序列,求这个序列的最长上升子序列的长度,并输出这个最长上升子序列,题目保证,最长上升子序列只有一个。

第一行输出一个数,代表最长上升子序列的长度。
第二行打印这个子序列。

输入

8 5 2 8 6 3 6 5 7

输出

4 2 3 6 7

#include<stdio.h>
#include<stdlib.h> 
const int N=100;
int dp[N],s[N],arr[N],tt[N];
int main(){
	int n;
	scanf("%d",&n);
	int i,j,k;
	int max=1;
	for(i=1;i<=n;i++){
		scanf("%d",&arr[i]);
	}

	for(i=1;i<=n;i++){
		dp[i]=1;
		s[i]=i;
		for(j=1;j<i;j++){
			if(arr[i]>arr[j]&&dp[j]+1>dp[i]){
				dp[i]=dp[j]+1;
				s[i]=j;
			}
		}
		if(dp[i]>=max) {
		max=dp[i];
		k=i;
		}
		
	}
	int m=0;
	while(true){
		tt[m++]=arr[k];
		if(s[k]==k){
			break;
		}
		k=s[k];
	}
	for(i=m-1;i>0;i--){
		printf("%d ",tt[i]);
	}
	printf("%d",tt[0]); 
	
} 

夜深人静写算法(二十)- 最长单调子序列 一类简单动态规划问题 阅读详情

相关推荐

动态规划:最长单调递增子序列

动态规划:最长单调递增子序列 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.

经典动态规划-连续数组最大和问题

Description 求最大连续子段和,并输出此子段的起始位置和终止位置的值。 例如给定序列{ -2, 11, -4, 13, -5, -2 },其最大连续子序列为{ 11, -4, 13 },最大和为20。 Input 测试输入包含若干测试用例,每个测试用例占2行,第1行给出正整数K( < 10000 ),第2行给出K个整数,中间用空格分隔。当K为0时,输入结束,该用例不被处理。 Output 对每个测试用例,在1行里输出最大和、最大连续子序列的第一个和最后一个元 素,中间用空格分隔。如果最大连续

hhhertzzz的博客 1197

OpenEuler22.04 快速部署Docker:华为云镜像源配置指南

本文详细介绍了在OpenEuler 22.04系统上快速部署Docker的完整流程。针对系统默认源版本老旧的问题,重点讲解了如何配置华为云镜像源以加速安装,并涵盖Docker引擎、Docker Compose的安装、服务启动验证、权限配置、镜像加速等核心步骤与常见问题排查,帮助用户高效搭建稳定的容器化开发环境。

http9protocoller的博客 174

动态规划法 第3关:求序列-2 11 -4 13 -5 -2的最大子段和

任务描述 本关任务:编写用动态规划解决最大子段和问题。 相关知识 为了完成本关任务,你需要掌握:动态规划。 编程要求 给定由n个整数(可能为负数)组成的序列:a1,a2,……,an, 求该序列的最大子段和。当所有整数均为负数,定义其最大子段和为0。 解题思路: 定义b[j]=max(a[i]+a[i+1]+…+a[j]),其中1<=i<=j,并且1<=j<=n。那么所求的最大子段和可以表示为max b[j],1<=j<=n。 由b[j]的定义可知,当b[j−1]>0

Alfred_tfk的博客 4743

蓝桥杯算法:打印图形——六芒星

【输入格式】 输入一行包含一个整数 n。 【输出格式】 输出一个六芒星,表示满足题意的图形。 【样例输入】 3 4 【样例输出】 【评测用例规模与约定】 对于所有评测用例,1 ≤ n ≤ 20。 总结: 第一次做图形题,做了蛮久,图形题有很多的方式去完成,它考验的是你能不能找到更简单的那一种方式. 我初看到这个图形,第一种想法是把这个六芒星当做六个小三角形来做,然后以某种规则排列一下,就能做出来,但是尝试了一下感觉太麻烦了。 然后我的第二种想法是,这个图形是对称图形,怎么能写出一半吗? 这样另一半想办

KeepCodeing 3843

MTK Linux DRM分析(十一)- MTK KMS Panel显示屏驱动

面板扩展(Panel Extension)是指 MediaTek 在标准 DRM 面板驱动框架之上添加的定制化功能模块。

iTom1900 1690

poj 怪盗基德的滑翔翼

2019独角兽企业重金招聘Python工程师标准>>> ...

weixin_33991727的博客 767

AEJoy —— 表达式之仅限在某一维摆动【JS】

效果图 wiggle() 的作用仅限于水平方向 想法 wiggle() 确实是一个非常方便的工具,但有时您希望对它有更多的控制。在本例中,我们希望仅将摆动限制在水平方向。 设计 要理解 wiggle() 最重要的一点是,它生成的结果与应用它的属性具有相同的维度。例如,如果你将 wiggle() 应用于 3D 层的 position 属性,结果将是一个由三个元素组成的数组(x、y 和 z 各一个)。我们需要做的就是将 wiggle() 的结果捕捉到一个变量(我们称之为 “w” ),然后从这个变量中提取我们想

ShaderJoy 的兴趣技术杂货铺 2095

动态规划:最大子矩阵

描述 已知矩阵的大小定义为矩阵中所有元素的和。给定一个矩阵,你的任务是找到最大的非空(大小至少是1 * 1)子矩阵。 比如,如下4 * 4的矩阵 0 -2 -7 0 9 2 -6 2 -4 1 -4 1 -1 8 0 -2 的最大子矩阵是 9 2 -4 1 -1 8 这个子矩阵的大小是15。 输入 输入是一个N * N的矩阵。输入的第一行给出N (0

July_xunle的博客 678

mongodb在游戏开发领域的优势

目前游戏服务器使用mysql作为数据库的较多,然而,使用mongodb来作为存储层,在某些方面比mysql更为方便和适合。

littleschemer的博客 1701

Silvaco TCAD 半导体器件仿真全攻略:从入门到精通

本文介绍了Silvaco TCAD半导体器件仿真平台的核心功能与应用方法。主要内容包括: Silvaco TCAD的核心模块组成(ATHENA工艺仿真和ATLAS器件仿真)及其在半导体研发中的重要作用,能够通过数值仿真预测器件电学特性,显著降低研发成本。 环境配置要点,包括内存分配、文件目录管理和许可证验证,为仿真工作提供基础保障。 仿真核心概念:详细讲解了网格划分策略和物理模型选择原则,强调基于物理直觉的自适应加密网格方法。 以PN结二极管为例,提供了完整的仿真代码示例,涵盖从网格定义、掺杂设置到结果分析

huanghm88的专栏 210

最长单调子序列求解

本文给出了如下题目的参考解答,虽然方法并不是最优,但详细的分析具有很好的参考价值!题目:已知一个序列,由随机数构成,求其最长单调子序列。要求: 单调分严格和不严格两种情况,并分别求解并输出一个最长单调子序列和所有符合要求的子序列。

jin的专栏 3095

DaVinci配置DID之三:代码实现

本系列主要基于DaVinCi工具链来展开具体DID读写的配置以及最终实现。1,CDD准备2,工具链配置3,代码实现4,验证结果本文讲述流程3话不多说,走你~博主是AutoSar小白,如果哪里表达的有问题,还请大佬们指点指点哈。接下来,让我们在下章关于测试验证的博文再相会哈~

weixin_48498880的博客 5365

《最长单调子序列》基础概念

本文介绍了一种求解最长递增子序列的高效算法,采用动态规划思想结合贪心策略和二分查找进行优化。算法维护一个辅助数组存储递增子序列的最小末尾元素,通过二分查找确定每个元素的插入位置,时间复杂度为O(n log n),优于传统O(n²)的DP方法。文章详细讲解了算法思路、伪代码实现、处理流程和复杂度分析,并举例说明其正确性。该算法在生物信息学、数据压缩等领域有广泛应用,是处理大规模数据的有效解决方案。

英雄哪里出来 1005

063.CI/CD for ML:给YOLO项目装上自动化流水线

本文分享了为YOLO目标检测项目搭建CI/CD流水线的实践经验。作者通过手动调试模型导出问题的经历,意识到重复验证工作比模型改进更耗时,提出需要自动化解决方案。文章详细介绍了四阶段流水线设计:1)代码质量检查;2)快速训练验证;3)推理性能基准测试;4)模型注册部署。同时总结了环境不一致、GPU资源争抢等常见问题及解决方案,建议从简单规则开始逐步完善,强调模型版本管理的重要性。实施后不仅提高了效率,还促使团队形成更规范的开发习惯。

2603_95664987的博客 237

迈瑞BS系列全自动生化分析仪LIS协议接口手册(内含HL7和ASTM)

迈瑞BS系列全自动生化分析仪LIS协议接口手册,内含HL7和ASTM)

专题:最长单调子序列(LIS)问题

专题:最长单调子序列(LIS)问题 (4.27 记录) LIS问题是DP当中的一个很经典的问题。 LIS问题的核心在于状态的保护和转移。 一.LIS的长度 解法1:一般DP 时间复杂度O(n2) 很显然,对于某一个点i,1-i的最长单调子序列是唯一的,而以i结尾的最长单调子序列也是唯一的。这样就有两种思路: A dp[i]表示1-i中出现的最长单调子序列的长度; B dp[i]表示以i结尾的最长单调子序列的长度。 这两种思路唯一的区别在于dp是否要转移,或者更直观的来说,区别在于dp[n]是否就是最长单调子

eliyou的博客 1238

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

最长单调子序列问题(LIS,dp入门)

dp的一种很简单的题型。 小编便以两道题来介绍一下。

蒲公英殇的博客 794

android 蓝牙通讯demo

android 下蓝牙通讯demo,实现了发现蓝牙,使能蓝牙,通过蓝牙发送消息。

RU DOS版、EFI版32bit/64bit BIOS常用工具

DOS版、EFI版的RU工具,可用来查看IO、MEM、PCI、SMBIOS等资源。亲测在EFI SHELL下可用。

《最长单调子序列》题集

本文整理了一套关于LIS(最长上升子序列)的算法题集,包含模板题、课内题和课后题三类。其中模板题和课内题配有视频讲解,是必做题;课后题为选做题,用于提升竞赛能力。题集涵盖HDOJ、蓝桥云课、洛谷和力扣等多个OJ平台,兼顾算法竞赛和求职面试需求。作者为抖音课程《英雄编程》系列的主讲人,建议学员先尝试独立解题,再参考视频学习。这套题集旨在帮助学员掌握LIS算法及其变种应用,提高算法竞赛水平。

英雄哪里出来 763

最长单调子序列LIS的简单动态规划算法

    给定一个序列,要求求出该序列的最长单调子序列, 即 longest increasing subsequence    这是一个经典的动态规划求解问题。     设给定序列为 a[],大小为 n,如何求其最长单调子序列呢?    考虑将最长单调子序列的长度作为所求的最优值    最长单调子序列必定以序列a[]中的某一个元素结尾,这是废话。    设序列count[i

月淡如水 1613

C++ 最长单调子序列

代码练习 1 对应蓝桥云课 蓝桥骑士 代码见下。代码练习 2 对应蓝桥云课 合唱队形 代码见下。代码 3 对应蓝桥云课 拍照 代码见下。最长单调子序列 代码框架 代码见下。

qq_43355454的博客 218

HDU1160 最长单调子序列

<br /><br />FatMouse's Speed<br />Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)<br /> Total Submission(s): 2768    Accepted Submission(s): 1168<br />Special Judge<br />Problem Description<br />FatMouse believes that

ACM_DavidCN的专栏 1029

最长单调子序列问题

代码: #include<iostream> #include<vector> #include <algorithm> using namespace std; const int maxn = 1001; int dp[maxn]; //vector<int>nums = { 10,9,2,5,3,7,101,18 }; //vector<int>nums = { 0, 1, 0, 3, 2, 3 }; vector<int>nu

Aaron_Yang 349

最长单调子序列(DP)

问题描述:给定某一组无序的数,求其中最长的单调序列的长度(该单调序列不一定是连续的),如在 5 6 1 2 4 7 5 中最长上升增序列是1 2 4 7 或者1 2 4 5。 该题可以用动态规划的方法解决,时间复杂度为O(n^2): 设A[t]表示序列中的第t个数,F[t]表示从1到t这一段中以t结尾的最长上升子序列的长度。 动态规划方程:F[t]=max{1,F[j]+...

weixin_33953384的博客 211
上一篇: 最大子段和(分治算法)
下一篇: 最大子段和问题的求解(动态规划)
grbgit
博客等级 码龄6年 5粉丝 12原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值