#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. mac安装ElasticSearch+head+node+一个例子~

    1.下载ElasticSearch 官网下载链接:https://www.elastic.co/cn/downloads/past-releases(进去的可能会比较慢,网络好的情况下会好一些) 我下 ...

  2. SpringBoot 2 HTTP转HTTPS

    @Bean public TomcatServletWebServerFactory servletContainer() { TomcatServletWebServerFactory tomcat ...

  3. java并发编程(二十四)----(JUC集合)ArrayBlockingQueue和LinkedBlockingQueue介绍

    这一节我们来了解阻塞队列(BlockingQueue),BlockingQueue接口定义了一种阻塞的FIFO queue,每一个BlockingQueue都有一个容量,当容量满时往BlockingQ ...

  4. h5微信分享

    h5分享的步骤(前端需要完成的部分) 1.绑定域名 登录微信公众平台进入“公众号设置”的“功能设置”里填写“JS接口安全域名”. 2.引入Js文件 在需要调用JS接口的页面引入如下JS文件,(支持ht ...

  5. 洛谷 P1903 [国家集训队]数颜色

    题意简述 给定一个数列,支持两个操作 1.询问l~r有多少不同数字 2.修改某个数字 题解思路 带修莫队 如果修改多了,撤销修改 如果修改少了,进行修改 代码 #include <cmath&g ...

  6. (十八)c#Winform自定义控件-提示框

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

  7. Tomcat源码分析 (十)----- 彻底理解 Session机制

    Tomcat Session 概述 首先 HTTP 是一个无状态的协议, 这意味着每次发起的HTTP请求, 都是一个全新的请求(与上个请求没有任何联系, 服务端不会保留上个请求的任何信息), 而 Se ...

  8. springboot启动慢解决方法

    jdk的配置文件中,使用securerandom.source设置了熵源: cat /usr/java/jdk1.8.0_121/jre/lib/security/java.security secu ...

  9. IIS配置后本地访问正常,但外网无法访问

    很久没有部署IIS网站项目了,都有些手生了,这不今天就遇到了问题.首先确定的是,我的网站配置没有问题,因为内网访问正常.内网访问情况如下: 但是外网访问时确是这样的: 怎么回事儿呢?我就想是不是防火墙 ...

  10. C#ORM中的对象映射

    使用Linq.Expressions来动态生成映射方法 1.我们先写个简单的类Test,包含一个ID和Name. public class Test { public int? ID { get; s ...