40分求助

B3637 最长上升子序列

duanzx @ 2023-07-21 20:33:33

这一题为什么错了,我试了好多样例都没发现错误。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define vct vector
#define que queue
#define stk stack
#define sct struct
#define str string
#define N 114514
vct <int> a(N);
vct <int> dp(N);
bool cmp(ll x, ll y) {
    return x > y;
}

void solve() {

}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin >> n;
    for (int i = 1; i <= n; i ++) {
        cin >> a[i];
    }
    int ans = 1;
    for (int i = 2; i <= n; i ++) { 
        dp[1] = 1;
        for (int j = 1; j < i; j ++) {
            if (a[i] > a[j])
            dp[i] = max(dp[i], dp[j] + 1);
        }
        ans = max(ans, dp[i]);      
    }
    cout << ans;
}

by pragma_GCC @ 2023-07-21 20:40:28

问题:

  1. 把dp[1]=1挪到循环外面
    2.在循环内部添加dp[i]=1;

by pragma_GCC @ 2023-07-21 20:42:57

你这个程序n=5,a={10,1,2,3,4}时会挂


by pragma_GCC @ 2023-07-21 20:50:29

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define vct vector
#define que queue
#define stk stack
#define sct struct
#define str string
#define N 114514
vct <int> a(N);
vct <int> dp(N);
bool cmp(ll x, ll y) {
    return x > y;
}

void solve() {

}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin >> n;
    for (int i = 1; i <= n; i ++) {
        cin >> a[i];
    }
    int ans = 1;
    for (int i = 1; i <= n; i ++) { 
        dp[i] = 1;
        for (int j = 1; j < i; j ++) {
            if (a[i] > a[j])
            dp[i] = max(dp[i], dp[j] + 1);
        }
        ans = max(ans, dp[i]);      
    }
    cout << ans;
}

by duanzx @ 2023-07-21 21:13:07

@NIEYIXUAN 太感谢了


|