记忆化搜索 后三个点WA求助

B3637 最长上升子序列

Serend1pity @ 2023-11-25 17:27:07

#include<iostream>
#include<vector>
#include<cmath>
using namespace std;
vector<int>memo(5003,-1);
int dfs(int pos,vector<int>&nums){
    int n = nums.size() - 1;
    if(memo[pos] != -1) return memo[pos];
    if(pos == n) return memo[n] = 1;
    int tmax = 0;
    for( int j = pos + 1;j <= n;j++){
        if(nums[j] > nums[pos])
        tmax = max(tmax,dfs(j,nums));
    }
    return memo[pos] = tmax+1;
}
int main(){
    int n;
    cin >> n;
    vector<int>nums(n+1);
    for(int i = 1;i <= n;i++){
        cin >> nums[i];
    }
    cout << dfs(1,nums) << endl;
    return 0;
}

by OldDriverTree @ 2023-11-25 17:29:24

@defuzhu 不一定以第一个位置开头吧


by Serend1pity @ 2023-11-25 17:39:38

@OldDriverTree dfs返回值就是从nums数组的第pos个位置开始的最长上升子序列的长度呀,


by Serend1pity @ 2023-11-25 17:43:45

@OldDriverTree 哦哦哦,我悟了,确实不一定从第一个位置开头。 谢谢帮助!


|