[DP]最长递增子序列
#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]最长递增子序列的更多相关文章
- HDU-1160-FatMouse's Speed(DP, 最长递增子序列)
链接: https://vjudge.net/problem/HDU-1160 题意: FatMouse believes that the fatter a mouse is, the faster ...
- poj 1631 Bridging signals (二分||DP||最长递增子序列)
Bridging signals Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 9234 Accepted: 5037 ...
- HDU 1069 dp最长递增子序列
B - Monkey and Banana Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I6 ...
- dp之最长递增子序列模板poj3903
最长递增子序列,Longest Increasing Subsequence 下面我们简记为 LIS.排序+LCS算法 以及 DP算法就忽略了,这两个太容易理解了. 假设存在一个序列d[1..9] = ...
- 动态规划(DP),最长递增子序列(LIS)
题目链接:http://poj.org/problem?id=2533 解题报告: 状态转移方程: dp[i]表示以a[i]为结尾的LIS长度 状态转移方程: dp[0]=1; dp[i]=max(d ...
- Longest Increasing Subsequences(最长递增子序列)的两种DP实现
一.本文内容 最长递增子序列的两种动态规划算法实现,O(n^2)及O(nlogn). 二.问题描述 最长递增子序列:给定一个序列,从该序列找出最长的 升序/递增 子序列. 特点:1.子序列不要 ...
- 求解最长递增子序列(LIS) | 动态规划(DP)+ 二分法
1.题目描述 给定数组arr,返回arr的最长递增子序列. 2.举例 arr={2,1,5,3,6,4,8,9,7},返回的最长递增子序列为{1,3,4,8,9}. 3.解答 ...
- [程序员代码面试指南]最长递增子序列(二分,DP)
题目 例:arr=[2,1,5,3,6,4,8,9,7] ,最长递增子序列为1,3,4,8,9 题解 step1:找最长连续子序列长度 dp[]存以arr[i]结尾的情况下,arr[0..i]中的最长 ...
- 51nod-1134 最长递增子序列,用线段树将N^2的dp降到NlogN
题目链接 给出长度为N的数组,找出这个数组的最长递增子序列.(递增子序列是指,子序列的元素是递增的) 例如:5 1 6 8 2 4 5 10,最长递增子序列是1 2 4 5 10. Input 第1行 ...
随机推荐
- HTML5 第二章 列表和表格和媒体元素
列表: (1)什么是列表? 列表就是信息资源的一种展示形式. (2)无序列表: 语法: <ul> <li>第1项</li> <li>第2项</li ...
- IDED中配置SVN没有svn.exe解决办法
首先在idea中配置svn时
- 如何为 caddy 添写自定义插件
如何为 caddy 添写自定义插件 项目地址:https://github.com/yhyddr/quicksilver/tree/master/gosample/caddy-plugin 前言 Ca ...
- 转载 | 如何给网页标题添加icon小图标
打开某一个网页会在浏览器的标签栏处显示该网页的标题和图标,当网页被添加到收藏夹或者书签中时也会出现网页的图标,怎么在网页title左边显示网页的logo图标呢? 方法一(被动式): 制作一个ico格式 ...
- 转载 | CSS实现单行、多行文本溢出显示省略号(…)
本文引自:https://www.cnblogs.com/wyaocn/p/5830364.html 首先,要知道css的三条属性. overflow:hidden; //超出的文本隐藏 text-o ...
- Pipeline 模型
解决的问题 解决并发效率问题,将任务拆分成流水线,然后多线程并发执行,比之单线程执行快. 案例 CPU 流水线 Tomcat 容器 Structs
- mysql主从不同步处理过程分享
背景 8月7日15:58收到报障数据库出现不同步:数据库共四台,分别为10.255.70.11,10.255.70.12,10.255.70.13,10.255.70.14(ip为虚拟ip) 数据库 ...
- Java虚拟机详解(五)------JVM参数(持续更新)
JVM参数有很多,其实我们直接使用默认的JVM参数,不去修改都可以满足大多数情况.但是如果你想在有限的硬件资源下,部署的系统达到最大的运行效率,那么进行相关的JVM参数设置是必不可少的.下面我们就来对 ...
- 【译】为什么要了解HTTP
原文地址:Why should I care about HTTP? 原作信息:by Devon Campbell. Dec 15 '18 Originally published at raddev ...
- mybatis基础简介
1.mybatis的加载过程? 程序首先加载mybatis-config.xml文件,根据配置文件创建SQLSessionFactory对象: 然后通过SQLSessionFactory对象创建 ...