#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. Hadoop 系列(二)—— 集群资源管理器 YARN

    一.hadoop yarn 简介 Apache YARN (Yet Another Resource Negotiator) 是 hadoop 2.0 引入的集群资源管理系统.用户可以将各种服务框架部 ...

  2. 国内CDH的MAVEN代理

    在编译CDH版本的各个开源软件时,需要从cdh-repo下载对应的jar包,但发现下载速度非常慢,甚至有时候出现下载异常的情况. 下面是国内可用的.速度非常快的一个maven代理仓库,亲测可用: ht ...

  3. React 如何搭建脚手架

    React 如何搭建脚手架   npm install -g create-react-app    //安装 create-react-app react-demo    // react-demo ...

  4. Spring aop 拦截自定义注解+分组验证参数

    import com.hsq.common.enums.ResponseState;import com.hsq.common.response.ResponseVO;import org.aspec ...

  5. JAVA jobs

    Java岗位1, SpringMVC, spring, mybaits2, 高并发编程3, mysql或者oracle SQL调优及函数,存储过程,JOB调度

  6. UML类图(1.3)

    UML:Unified modeling Language 统一建模语言 UML类图:用来描述系统所包含的类以及类之间的关系. 画图工具:https://www.processon.com 类之间的6 ...

  7. jvisualvm/Jconsole监控WAS中间件

    1.登录was控制台https://196.168.119.18:9043/ibm/console/,找到自己的应用程序服务器---java和进程管理---进程定义--JAVA虚拟机,然后配置 通用J ...

  8. maven3实战之仓库

    maven3实战之仓库(maven仓库分类) maven3实战之仓库(maven仓库分类) ---------- 对于maven来说,仓库只分为两类:本地仓库和远程仓库.当maven根据坐标寻找构件的 ...

  9. 《机器学习技法》---对偶SVM

    1.对偶问题的推导 为什么要求解对偶问题?一是对偶问题往往更容易求解,二是可以自然的引入核函数. 1.1 用拉格朗日函数将原问题转化为“无约束”等价问题 原问题是: 写出它的拉格朗日函数: 然后我们的 ...

  10. Spring Cloud Gateway 服务网关快速上手

    Spring Cloud Gateway 服务网关 API 主流网关有NGINX.ZUUL.Spring Cloud Gateway.Linkerd等:Spring Cloud Gateway构建于 ...