最长增长子序列 DP
#include<iostream>
using namespace std;
#define INF 0x7fffffff
#define N 10000 // O(n^2)
int len[N];
int dp(int *a, int n){
int mx = , mxlen = ;
for (int i = ; i < n; ++i)
len[i] = ;
for (int i = ; i < n; ++i){
mx = ;
for (int j = ; j < i; ++j) // 寻找len[0...i-1]最大值
if (a[j]<a[i] && len[j]>mx)
mx = len[j];
len[i] = mx + ;
if (mxlen < len[i]) mxlen = len[i]; // 更新最大长度
}
return mxlen;
} // O(nlogn)
int mv[N]; //mv[i]存放序列长度为 i+1 的最小元素
int binarySearch(int r, int val)
{
int left = , right = r, mid;
while (left < right){
mid = (left + right) / ;
if (mv[mid] < val) left = mid + ;
else right = mid;
}
return left;
}
int binaryLIS(int *a, int n)
{
int len = ;
mv[] = a[];
for (int i = ; i < n; ++i){
if (a[i]>mv[len]) mv[++len] = a[i];
else mv[binarySearch(len, a[i])] = a[i];
for (int i = ; i < len; ++i)
cout << mv[i] << ' ';
cout << mv[len] << endl;
}
return len + ;
} int main()
{
int a[] = { , , , , , , };
cout << dp(a, ) << endl;
cout << binaryLIS(a, ) << endl;
}
最长增长子序列 DP的更多相关文章
- 最长增长子序列(LIS)
给定一个无序的整数数组,找到其中最长上升子序列的长度. 示例: 输入: [10,9,2,5,3,7,101,18] 输出: 4 解释: 最长的上升子序列是 [2,3,7,101],它的长度是 4. 说 ...
- POJ-2533最长上升子序列(DP+二分)(优化版)
Longest Ordered Subsequence Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 41944 Acc ...
- LCS最长公共子序列~dp学习~4
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1513 Palindrome Time Limit: 4000/2000 MS (Java/Others ...
- Longest Ordered Subsequence POJ - 2533 最长上升子序列dp
题意:最长上升子序列nlogn写法 #include<iostream> #include<cstdio> #include<cstring> #include&l ...
- POJ 1458 最长公共子序列(dp)
POJ 1458 最长公共子序列 题目大意:给出两个字符串,求出这样的一 个最长的公共子序列的长度:子序列 中的每个字符都能在两个原串中找到, 而且每个字符的先后顺序和原串中的 先后顺序一致. Sam ...
- 【BZOJ2423】[HAOI2010]最长公共子序列 DP
[BZOJ2423][HAOI2010]最长公共子序列 Description 字符序列的子序列是指从给定字符序列中随意地(不一定连续)去掉若干个字符(可能一个也不去掉)后所形成的字符序列.令给定的字 ...
- hdu 1159 Common Subsequence(最长公共子序列 DP)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1159 Common Subsequence Time Limit: 2000/1000 MS (Jav ...
- 最长公共子序列 DP
class Solution: def LCS(self,A,B): if not A or not B: #边界处理 return 0 dp = [[0 for _ in range(len(B)+ ...
- 65.Longest Increasing Subsequence(最长增长子序列)
Level: Medium 题目描述: Given an unsorted array of integers, find the length of longest increasing sub ...
随机推荐
- Pet--hdu4707
Pet Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- Avoid The Lakes--poj3620
Avoid The Lakes Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7023 Accepted: 3735 D ...
- C# 几种常见的排序方法
1.冒泡排序 //冒泡排序 public void BubbleSort(int[] list) { int i, j, temp; bool done = false; j = ; while (( ...
- 深入浅出畅谈Zigbee
ZigBee采用802.15.4标准作为其对等通信的基础.该标准由ZigBee联盟(ZigBee Alliance)开发并管理.ZigBee Alliance是一家投资于该标准并在无线领域进行推广的联 ...
- Qt-4.6动画Animation快速入门三字决
Qt-4.6动画Animation快速入门三字决 Qt-4.6新增了Animation Framework(动画框架),让我们能够方便的写一些生动的程序.不必像以前的版本一样,所有的控件都枯燥的呆在伟 ...
- cdecl、pascal、stdcall、fastcall
Directive Parameter order Clean-up Passes parameters in registers?register Left-to-right ...
- 【POJ 1679 The Unique MST】最小生成树
无向连通图(无重边),判断最小生成树是否唯一,若唯一求边权和. 分析生成树的生成过程,只有一个圈内出现权值相同的边才会出现权值和相等但“异构”的生成树.(并不一定是最小生成树) 分析贪心策略求最小生成 ...
- tty/pts 相关指令
http://unix.stackexchange.com/questions/136662/how-can-we-know-whos-at-the-other-end-of-a-pseudo-ter ...
- 【LeetCode练习题】Scramble String
Scramble String Given a string s1, we may represent it as a binary tree by partitioning it to two no ...
- Linux磁盘及文件系统管理 3---- 文件系统
1 文件系统 1 操作系统通过文件系统来管理文件及数据,磁盘或分区需要创建文件系统之后才能为操作系统使用,创建文件系统的过程称为格式化 2 没有文件系统的设备称为裸设备 3 常见的文件系统有fat32 ...