火影 发表于 2025-4-6 00:45:59

算法--最长上升子序列

1、最长上升子序列

https://i-blog.csdnimg.cn/direct/d69a42f232244949b9563ef00922ef88.png
https://i-blog.csdnimg.cn/direct/8e26b1fb55974342b7ba07d70e5c055f.png
https://i-blog.csdnimg.cn/direct/9f2d7eb6d49749969ed2146bd486ccea.png
#include<iostream>
#include<algorithm>
using namespace std;
int a;//原始数组
int dp;//以x结尾的数组
int main() {
        int n;//数组长度
        cin >> n;
        for (int i = 1; i <= n; i++) {
                cin >> a;//数组中的每个元素
        }
        for (int i = 1; i <= n; i++) {
                dp = 1;
                for (int j = 1; j < i; j++) {
                        if (a < a) {
                                dp = max(dp, dp + 1);
                        }
                }
        }
        int max_step = 0;
        for (int i = 1; i <= n; i++) {
                if (dp > max_step) {
                        max_step = dp;
                }
        }
        cout << max_step << endl;
}  https://i-blog.csdnimg.cn/direct/c0775aea016b49ebafd33cab6c1d11c4.png
 2、例题

 https://i-blog.csdnimg.cn/direct/c92035bf44724404b9afe2506d22b509.png
#include<bits/stdc++.h>
using namespace std;
const int N = 1e6;
int g,a;
int main()
{
    int n;
    cin>>n;
    int len=0;
    for(int i = 1; i <= n; i ++) {
      cin>>a;
    }
    for(int i = 1; i <= n; i ++) {
      if(a > g){
          g[++len] = a;
      }else {
            int pos = upper_bound(g+1,g+1+len,a) - g;
            g = a;
      }
    }
    cout<<len;
    return 0;
}

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: 算法--最长上升子序列