LIS最长上升子序列

Posted 你可以当我没名字

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LIS最长上升子序列相关的知识,希望对你有一定的参考价值。

LIS最长上升子序列

https://www.bilibili.com/video/BV137411B7BN?from=search&seid=10864404175908886859

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
#include<iostream>
#include<sstream>
#include<set>
#include<map>
#include<queue>
#include<bitset>
#include<vector>
#include<limits.h>
#include<assert.h>
#include<functional>
#include<numeric>
#include<ctime>
#define ini(a) memset(a,0,sizeof(a))
#define ini2(a,b) memset(a,b,sizeof(a))
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define lowbit(x) x&(-x)
#define DBG(x) \\
	(void)(cout << "L" << __LINE__ \\
	<< ": " << #x << " = " << (x) << '\\n')
#define TIE \\
	cin.tie(0);cout.tie(0)\\
	ios::sync_with_stdio(false);
#define sc ll T;cin>>T;while(T--)
#define IN freopen("in.txt","r",stdin);
#define OUT srand((unsigned)time(NULL)); \\
	freopen("out.txt","w",stdout);
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int INF = 0x3f3f3f3f;
const int maxn = 1000000005;
const int N = 100;
int a[100],dp[1000];

int main()
{
	int n;
	cin>>n;
	dp[0] = 0;
	int maxx = -INF;
	for(int i=1;i<=n;i++)
		cin>>a[i];
	for(int i=1;i<=n;i++){
		dp[i] = 1;
		for(int j=1;j<i;j++){
			if(a[j]<a[i])
				dp[i] = max(dp[i],dp[j]+1);
		}
		if(dp[i]>maxx)maxx = dp[i];
		printf("dp[%d]=%d\\n",i,dp[i]);
	}
	cout<<maxx<<"\\n";
	
}

例题:

https://leetcode-cn.com/problems/longest-increasing-subsequence/submissions/

class Solution {
public:
    int lengthOfLIS(vector<int>& nums) {
        int dp[2600];
        int maxx = -11111;
        for(int i=0;i<nums.size();i++){
            dp[i] = 1;
            for(int j=0;j<i;j++){
                if(nums[j]<nums[i])
                    dp[i] = max(dp[i],dp[j]+1);
            }
            if(dp[i]>maxx)maxx = dp[i];
        }
        return maxx;
    }
};

以上是关于LIS最长上升子序列的主要内容,如果未能解决你的问题,请参考以下文章

动态规划模板1|LIS最长上升子序列

算法练习--- DP 求解最长上升子序列(LIS)

LIS:最长上升子序列

Lintcode--010(最长上升子序列)

LIS(最长上升子序列)

最长上升子序列(LIS)动态规划