最长上升子序列(dp)

#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
using namespace std;
#define ll long long
#define lowbit(x) (x&(-x))
#define MT(a, b) memset(a,b,sizeof(a))
const int maxn = 2E5 + 15;
const int ONF = -0x3f3f3f3f;
const int INF = 0x3f3f3f3f;
int num[maxn];
int dp[maxn];
int main() {
    int n;
    while (~scanf ("%d",&n)){
        int len=0;
        MT(dp,-1);
        for (int i=1;i<=n;i++){
            scanf ("%d",&num[i]);
        }
        for (int i=1;i<=n;i++){
            if (dp[len]<num[i]) dp[++len]=num[i];
            else dp[lower_bound(dp+1,dp+len,num[i])-dp]=num[i];
        }
        printf("%d\n",len);
    }
    return 0;
}

相当于dp数组向后推,并且放进来的数字尽量小

发布了33 篇原创文章 · 获赞 15 · 访问量 910

猜你喜欢

转载自blog.csdn.net/weixin_43925900/article/details/97763642