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 ...
随机推荐
- e672. 缩放,剪取,移动,翻转缓冲图像
AffineTransform tx = new AffineTransform(); tx.scale(scalex, scaley); tx.shear(shiftx, shifty); tx.t ...
- iOS推送证书从申请到使用
关于这个话题,已经有非常多写的非常好的文章了.可是,在自己做的过程中,即使别人写的已经非常好了,还是会遇到这样那样的问题. 自己还是再写一遍吧. 本文记录了从无到有申请证书,到最后可以发出通知.当然, ...
- erlang的小知识,未分类。
erlang:module_loaded(module):检测模块是否已加载:
- asp.net 后端验证
using EntryRegistration.Filters; using EntryRegistration.Models.Entity; using System; using System.C ...
- <UIKit>关于剪贴板共享数据
http://blog.sina.com.cn/s/blog_45e2b66c010102h9.html 上面这篇文章将剪贴板的使用方法基本上已经讲清楚了,参考这篇文章,再加上一个使用剪贴板共享数据 ...
- mongodb select php操作 命令行操作
下面说一下,mongodb select的常用操作 测试数据 { "_id" : 1, "title" : "红楼梦", "aut ...
- [java] java 中Unsafe类学习
java不能直接访问操作系统底层,而是通过本地方法来访问.Unsafe类提供了硬件级别的原子操作,主要提供了以下功能: 1.通过Unsafe类可以分配内存,可以释放内存: 类中提供的3个本地方法all ...
- 第四章 Spring.Net 如何管理您的类___对象的初始化方式
在 Spring.Net 中对象初始化的方式分为两种: ① 急切实例化,也就是说 Spring.Net 容器初始化的时候将对象先实例化出来. ② 延迟实例化,也就是说我们在调用 GetObject 方 ...
- 延迟是AR/VR体验的基础
原文: http://blogs.valvesoftware.com/abrash/latency-the-sine-qua-non-of-ar-and-vr/ 译者注: 原文发表于2012年, 尽管 ...
- Java集合----Map集合
Map Map 用于保存具有映射关系的数据,因此 Map 集合里保存着两组值,一组值用于保存 Map 里的 Key,另外一组用于保存 Map 里的 Value Map 中的 key 和 value 都 ...