分析过程:
代码:
#include<bits/stdc++.h>
#include<unordered_map>
#include<unordered_set>
using namespace std;
#define int long long //可能会超时
#define PII pair<int,int>
const int INF = 0x3f3f3f3f, mod = 998244353;
const int N = 1005;
int a[N], dp[N], n, ans;
signed main()
{
ios::sync_with_stdio, cin.tie(0), cout.tie(0);
cin >> n;
for (int i = 1;i <= n;i++) cin >> a[i];
for (int i = 1;i <= n;i++) {
dp[i] = 1;
for (int j = 1;j < i;j++) {
if (a[j] < a[i]) {
dp[i] = max(dp[i], dp[j] + 1);
}
}
}
for (int i = 1;i <= n;i++) ans = max(ans, dp[i]);
cout << ans;
return 0;
}
以此类推,求最长下降子序列代码如下:
for (int i = n;i >= 1;i--) {
dp1[i] = 1;
for (int j = n;j > i;j--) {
if (a[j] < a[i]) {
dp1[i] = max(dp1[i], dp1[j] + 1);
}
}
}
《信奥一本通》相关题目:
Leetcode: