最长非上升子序列的长度

  最长非上升子序列问题是一个经典的DP问题。如下给出完整的问题描述:

  给你一串序列 A1,A2,A3,A4,A5........An。让你找出它的某个最长子序列 S1,S2,S3,S4.........Sm。使得 S1<=S2<=S3<=S4.........<=Sm。

从问题描述中,我们可以把 <= 换成各种其他的关系符号从而变成最长非下降子序列等,他们都只是关系描述不同,其本质都是一样的。现在,我们来只需总结一下最长非上升子序列问题即可应用到其他类型。

  这既然是一个经典的DP问题,那么找出他的状态转移方程显然是必须的。首先,我们用dp[i]来表示 1--i 的最长非上升子序列的长度。那么dp[i]=Max(dp[j])+1,j∈[1, i-1]。显然dp[1]=1。那么去哦们只需从第二项开始遍历即可。

算法描述如下:

1.dp[1] = 1;

2.依此遍历整个序列,每次求出从第一项到当前项的最长子序列长度。

3.找dp数组里最大的那个就是整个序列的最长子序列长度。

算法复杂度为:O(n^2)

 1 int Num[N];
 2 int dp[N];
 3
 4 int Lis(int n)
 5 {
 6     memset(dp, 0, sizeof dp);
 7     int ans;
 8     dp[0] = 1;
 9     for(int i=1; i<n; i++)
10     {
11         ans = dp[i];
12         for(int j=0; j<i; j++)
13             if(Num[i] <= Num[j] && dp[j]>ans)
14                 ans = dp[j];
15         dp[i] = ans+1;
16     }
17     ans = 0;
18     for(int i=0; i<n; i++)
19         if(dp[i] > ans)
20             ans = dp[i];
21     return ans;
22 }
时间: 2024-07-29 00:29:21

最长非上升子序列的长度的相关文章

最长上升非降子序列的长度动态规划

第一种dp从后往前: dp[i]表示以a[i]为起点的最长上升非降子序列的长度 a[8]={10,2,2,4,12,23,34,2} dp[8]={4,6,5,4,3,2,1,1}; 代码实现: 1 #include<bits/stdc++.h> 2 using namespace std; 3 void logest_increase_sub(const int*a,int ssize) 4 { 5 int *dp=new int[ssize](); 6 int *p=new int[ssi

Codeforces Round #323 (Div. 2) D. Once Again... 暴力+最长非递减子序列

                                                                              D. Once Again... You are given an array of positive integers a1, a2, ..., an × T of length n × T. We know that for any i > n it is true that ai = ai - n. Find the length of

HDU 1025 Constructing Roads In JGShining&#39;s Kingdom[动态规划/nlogn求最长非递减子序列]

Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 27358    Accepted Submission(s): 7782 Problem Description JGShining's kingdom consists of 2n(n is no mor

[LeetCode] Longest Uncommon Subsequence I 最长非共同子序列之一

Given a group of two strings, you need to find the longest uncommon subsequence of this group of two strings. The longest uncommon subsequence is defined as the longest subsequence of one of these strings and this subsequence should not be any subseq

最长不下降子序列的长度

试题描述 求最长不下降子序列的长度. 设有由n个不相同的整数组成的数列,记为:a[1].a[2].…….a[n].例如:3,18,7,14,10,12,23,41,16,24.若存在0<i1<i2<i3< … < ie 且有a[i1]<=a[i2]<= … <=a[ie]则称为长度为e的不下降序列.如上例中3,18,23,24就是一个长度为4的不下降序列,同时也有3,7,10,12,16,24长度为6的不下降序列. 输入 第一行为n,表示序列中整数的个数,第

动态规划-最长非降子序列

有关概念: 最长上升子序列(LIS,Longest Increasing Subsequence),在一个序列中最长的单调递增的子序列 思路: 求LIS通常有O(n2)和O(nlogn)两种算法 (1)O(n2)算法 fi表示以第i个数结尾的LIS长度 对于序列中的一个数i,在i前面枚举数j,j满足比i小且fj最大,将i作为j的后继 伪代码&状态转移方程: f1=1 for i=2...n for j=1...i-1 if(aj<ai)fi=max(fi,fj+1) 最后结果在f1~fn中取

算法题-数组的最长非递减子序列

1 int binSearch(const vector<int> &tail, int len, int key)// 2 { 3 int left = 0, right = len - 1; 4 int mid; 5 6 while(left <= right) 7 { 8 mid = left + ((right - left) >> 1); 9 if(tail[mid] == key) 10 return mid; 11 else if(tail[mid] &

[LeetCode] Longest Uncommon Subsequence II 最长非共同子序列之二

Given a list of strings, you need to find the longest uncommon subsequence among them. The longest uncommon subsequence is defined as the longest subsequence of one of these strings and this subsequence should not be any subsequence of the other stri

【一维偏序】【最长上升子序列】【最长非降子序列】

两种方法,都是nlogn 树状数组型 #include<cstdio> #include<cstdlib> #include<cstring> #include<algorithm> using namespace std; const int N=100003,M=50003; int d[N];//原数组 int f[M]; int n,big; int lowbit(int x) { return x&(-x); } //用于求上升序列 int