求助

B3637 最长上升子序列

chenhany @ 2024-11-25 19:59:05

#include<bits/stdc++.h>
using namespace std;
int a[100000], n;
int f(int x, int k, int maxa) {
    if (a[x] > maxa && x < n) {
        return max(f(x + 1, k+1, a[x]), f(x + 1, k, maxa));
    } else {
        return k;
    }
}
int main() {
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    cout << f(0, 0, 0);

    return 0;
}

by WE_TRT @ 2024-11-28 15:35:22

建议使用动态规划

#include<bits/stdc++.h>
using namespace std;
int n,a[5005],ans,f[5005];
int main(){
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>a[i];
    }
    f[1]=1;
    for(int i=2;i<=n;i++){
        f[i]=1;
        for(int j=1;j<=i-1;j++){
            if(a[j]<a[i]&&f[j]+1>f[i]){
                f[i]=f[j]+1;
            }
            if(f[i]>ans){
                ans=f[i];
            }
        }
    }
    cout<<ans;
    return 0;
}

|