#include <iostream>
#include <limits.h>
#include <vector>
#include <algorithm> using namespace std; //获取最长递增子序列的递增数组
vector<int> getdp1(vector<int> arr) {
vector<int> dp(arr.size());
for (int i = 0; i < int(arr.size()); i ++) {
dp[i] = 1;
for (int j = 0; j < i; j ++) {
if (arr[i] > arr[j])
dp[i] = max(dp[i], dp[j] + 1);
}
}
return dp;
} vector<int> getdp2(vector<int> arr) {
int right = 0;
int arr_len = int(arr.size());
int ends[arr_len];
int dp[arr_len];
ends[0] = arr[0];
dp[0] = 1;
for (int i = 1; i < arr_len; i ++) {
//二分查找ends
int l = 0;
int r = right;
while (l <= r) {
int mid = (l + r) >> 1;
if (arr[i] > ends[mid])
l = mid + 1;
else
r = mid - 1;
}
}
} // 从dp数组中逆序还原出决策路径
vector<int> generateLIS(vector<int> dp, vector<int> arr) {
vector<int>::iterator maxPosition = max_element(dp.begin(), dp.end()); //algorithm中求vector最大值的函数
int maxIndex = maxPosition - dp.begin();
int maxNum = *maxPosition;
vector<int> res;
res.push_back(arr[maxIndex]);
int tmpNum = maxNum;
for (int i = maxIndex - 1; i >= 0; i --) {
if (dp[i] == tmpNum - 1) {
res.push_back(arr[i]);
tmpNum -= 1;
} else continue;
}
reverse(res.begin(), res.end());
return res;
} int main()
{
vector<int> test = {2, 1, 5, 3, 6, 4, 8, 9, 7};
vector<int> dp = getdp1(test);
vector<int> lis = generateLIS(dp, test);
// cout<<"LIS"<<lis.size()<<endl;
for (auto c: lis)
cout<<c<<endl; return 0;
}

[DP]最长递增子序列的更多相关文章

  1. HDU-1160-FatMouse's Speed(DP, 最长递增子序列)

    链接: https://vjudge.net/problem/HDU-1160 题意: FatMouse believes that the fatter a mouse is, the faster ...

  2. poj 1631 Bridging signals (二分||DP||最长递增子序列)

    Bridging signals Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 9234   Accepted: 5037 ...

  3. HDU 1069 dp最长递增子序列

    B - Monkey and Banana Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I6 ...

  4. dp之最长递增子序列模板poj3903

    最长递增子序列,Longest Increasing Subsequence 下面我们简记为 LIS.排序+LCS算法 以及 DP算法就忽略了,这两个太容易理解了. 假设存在一个序列d[1..9] = ...

  5. 动态规划(DP),最长递增子序列(LIS)

    题目链接:http://poj.org/problem?id=2533 解题报告: 状态转移方程: dp[i]表示以a[i]为结尾的LIS长度 状态转移方程: dp[0]=1; dp[i]=max(d ...

  6. Longest Increasing Subsequences(最长递增子序列)的两种DP实现

    一.本文内容 最长递增子序列的两种动态规划算法实现,O(n^2)及O(nlogn).     二.问题描述 最长递增子序列:给定一个序列,从该序列找出最长的 升序/递增 子序列. 特点:1.子序列不要 ...

  7. 求解最长递增子序列(LIS) | 动态规划(DP)+ 二分法

    1.题目描述     给定数组arr,返回arr的最长递增子序列. 2.举例     arr={2,1,5,3,6,4,8,9,7},返回的最长递增子序列为{1,3,4,8,9}. 3.解答      ...

  8. [程序员代码面试指南]最长递增子序列(二分,DP)

    题目 例:arr=[2,1,5,3,6,4,8,9,7] ,最长递增子序列为1,3,4,8,9 题解 step1:找最长连续子序列长度 dp[]存以arr[i]结尾的情况下,arr[0..i]中的最长 ...

  9. 51nod-1134 最长递增子序列,用线段树将N^2的dp降到NlogN

    题目链接 给出长度为N的数组,找出这个数组的最长递增子序列.(递增子序列是指,子序列的元素是递增的) 例如:5 1 6 8 2 4 5 10,最长递增子序列是1 2 4 5 10. Input 第1行 ...

随机推荐

  1. MapReduce 运行全过程解析

    关注公众号,大家可以在公众号后台回复“博客园”,免费获得作者 Java 知识体系/面试必看资料. 前言 前面我们讲了 MapReduce 的编程模型,我们知道他主要分成两大阶段来完成一项任务,一是 m ...

  2. python基础之循环与迭代器

    循环 python 循环语句有for循环和while循环. while循环while循环语法 while 判断条件: 语句 #while循环示例 i = 0 while i < 10: i += ...

  3. 用JavaScript带你体验V8引擎解析标识符过程

    上一篇讲了字符串的解析过程,这一篇来讲讲标识符(IDENTIFIER)的解析. 先上知识点,标识符的扫描分为快解析和慢解析,一旦出现Ascii值大于128的字符或者转义字符,会进入慢解析,略微影响性能 ...

  4. C# Quartz结合控制台实现定时任务

    前言: Quartz一个开源的作业调度框架,是OpenSymphony 的 Quartz API的.NET移植,基于C#写成,可应用于winform.asp.net.asp.net core应用中.提 ...

  5. hadoop学习(六)----HDFS的shell操作

    HDFS所有命令: [uploaduser@rickiyang ~]$ hadoop fs Usage: hadoop fs [generic options] [-appendToFile < ...

  6. 映射&集合

    哈希函数 通过哈希表可以实现 O(1) 复杂度的查找. 哈希函数构造方法:设计好的哈希函数的两个基本原则,计算简单+分布均匀 1. 直接定址法 用key自身的某个线性函数来定址,f(key) = a* ...

  7. 我的mybatis入门宝典

    **********************************************************************************************一:myba ...

  8. Asp.Net Core WebAPI+PostgreSQL部署在Docker中

     PostgreSQL是一个功能强大的开源数据库系统.它支持了大多数的SQL:2008标准的数据类型,包括整型.数值值.布尔型.字节型.字符型.日期型.时间间隔型和时间型,它也支持存储二进制的大对像, ...

  9. 论文解读2——Spatial Pyramid Pooling in Deep Convolutional Networks for Visual Recognition

    背景 用ConvNet方法解决图像分类.检测问题成为热潮,但这些方法都需要先把图片resize到固定的w*h,再丢进网络里,图片经过resize可能会丢失一些信息.论文作者发明了SPP pooling ...

  10. Go-json解码到结构体-踩坑

    package main import ( "encoding/json" "fmt" ) type User struct { Name string `js ...