提交时间:2023-11-25 10:23:35

运行 ID: 112103

#include <bits/stdc++.h> using namespace std; #define N 1005 int a[N] , dp[N] , p[N]; void show(int i) { if(i == 0) return; show(p[i]) cout << a[i] << " "; } int main() { int n , maxn = 1; cin >> n; for(int i = 1;i <= n;i++) cin >> a[i]; for(int i = 1;i <= n;i++) { dp[i] = 1; p[i] = 0; for(int j = 1;j < i;j++) { if(a[i] >= a[j] && dp[i] < dp[j] + 1) { dp[i] = dp[j] + 1; p[i] = j; } } } for(int i = 1;i <= n;i++) { if(dp[i] > dp[maxn]) maxn = i; } cout << "max=" << dp[maxn] << endl; show(maxn); return 0; }