pta 习题集 5-5 最长连续递增子序列 (dp)
给定一个顺序存储的线性表,请设计一个算法查找该线性表中最长的连续递增子序列。例如,(1,9,2,5,7,3,4,6,8,0)中最长的递增子序列为(3,4,6,8)。
输入格式:
输入第1行给出正整数nn(≤105≤105);第2行给出nn个整数,其间以空格分隔。
输出格式:
在一行中输出第一次出现的最长连续递增子序列,数字之间用空格分隔,序列结尾不能有多余空格。
输入样例:
15
1 9 2 5 7 3 4 6 8 0 11 15 17 17 10
输出样例:
3 4 6 8
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string>
#include <map> using namespace std;
const int maxn=1e5;
int n;
int a[maxn+5];
int dp[maxn+5];
int search(int num,int l,int r)
{
int mid;
while(l<=r)
{
mid=(l+r)/2;
if(num>dp[mid])
l=mid+1;
else
r=mid-1;
}
return l;
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
memset(dp,0,sizeof(dp));
dp[1]=1;
for(int i=2;i<=n;i++)
{
if(a[i]>a[i-1])
dp[i]=dp[i-1]+1;
else
dp[i]=1;
}
int max=0;
int pos=0;
for(int i=n;i>=1;i--)
{
if(max<=dp[i])
{
max=dp[i];
pos=i;
}
}
for(int i=pos-dp[pos]+1;i<=pos;i++)
{
if(i!=pos)
printf("%d ",a[i]);
else
printf("%d\n",a[i]);
}
return 0; }
pta 习题集 5-5 最长连续递增子序列 (dp)的更多相关文章
- 二维动态规划&&二分查找的动态规划&&最长递增子序列&&最长连续递增子序列
题目描述与背景介绍 背景题目: [674. 最长连续递增序列]https://leetcode-cn.com/problems/longest-continuous-increasing-subseq ...
- 题目:[NOIP1999]拦截导弹(最长非递增子序列DP) O(n^2)和O(n*log(n))的两种做法
题目:[NOIP1999]拦截导弹 问题编号:217 题目描述 某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发 ...
- 动态规划-最长单调递增子序列(dp)
最长单调递增子序列 解题思想:动态规划 1.解法1(n2) 状态:d[i] = 长度为i+1的递增子序列的长度 状态转移方程:dp[i] = max(dp[j]+1, dp[i]); 分析:最开始把d ...
- [LintCode] Longest Increasing Continuous Subsequence 最长连续递增子序列
Give an integer array,find the longest increasing continuous subsequence in this array. An increasin ...
- LeetCode 最长连续递增序列
给定一个未经排序的整数数组,找到最长且连续的的递增序列. 示例 1: 输入: [1,3,5,4,7] 输出: 3 解释: 最长连续递增序列是 [1,3,5], 长度为3. 尽管 [1,3,5,7] 也 ...
- LeetCode 674. Longest Continuous Increasing Subsequence (最长连续递增序列)
Given an unsorted array of integers, find the length of longest continuous increasing subsequence. E ...
- [LeetCode] Longest Continuous Increasing Subsequence 最长连续递增序列
Given an unsorted array of integers, find the length of longest continuous increasing subsequence. E ...
- [Swift]LeetCode674. 最长连续递增序列 | Longest Continuous Increasing Subsequence
Given an unsorted array of integers, find the length of longest continuous increasing subsequence (s ...
- [LeetCode] 674. Longest Continuous Increasing Subsequence 最长连续递增序列
Given an unsorted array of integers, find the length of longest continuous increasing subsequence. E ...
随机推荐
- zebra/quagga
参考:http://blog.chinaunix.net/uid-25513153-id-212328.html 一.zebra安装 .编译安装 vim ./lib/zebra.h + 增加: #if ...
- udhcpc
/********************************************* * dhcpc * dhcpc是dhcp的客户端,在busybox中实现.今天正好了解一下. * Tony ...
- 使用Ultra Librarian转换芯片的Altium Designer封装格式
第一步:找到对应芯片的CAD文件,以OPA350为例: http://www.ti.com/product/opa350 RE: 使用Ultra Librarian转换TI芯片的Altium De ...
- 前台的js对象数组传到后台处理。在前台把js对象数组转化为json字符串,在后台把json字符串解析为List<>
前台的js对象数组传到后台处理.在前台把js对象数组转化为json字符串,在后台把json字符串解析为List<>
- 根据IP定位城市
根据IP定位城市:http://www.sucaihuo.com/js/35.html 示例:http://www.sucaihuo.com/jquery/0/35/demo/
- Spring-Resource接口
4.1.1 概述 在日常程序开发中,处理外部资源是很繁琐的事情,我们可能需要处理URL资源.File资源资源.ClassPath相关资源.服务器相关资源(JBoss AS 5.x上的VFS资源)等等很 ...
- 使用GitHub和Eclipse进行javaEE开发步骤
下载Git客户端:链接:http://pan.baidu.com/s/1jIueUEy 密码:7gef; 下载Eclipse javaee客户端:http://www.eclipse.org/down ...
- c++ 用构造函数
1.c++ 用构造函数创建一个对象时,对象里的属性应该是全新的不存在把之前的属性留下的问题.之所以debug里会存在上一个变量的信息,应该是debug里的一个bug,与程序无光.
- 第八章 示例代码(MyBatis)
Sample Code JPetStore 6 is a full web application built on top of MyBatis 3, Spring 3 and Stripes. I ...
- ASP.NET自带对象JSON字符串与实体类的转换
关于JSON的更多介绍,请各位自行google了解!如果要我写的话,我也是去Google后copy!嘿嘿,一直以来很想学习json,大量的找资料和写demo,总算有点了解! 切入正题! 还是先封装一个 ...