ProgramAlgTrain/20240919/CSP常考算法模板/线性dp_求最长不下降序列_逆推.cpp

63 lines
769 B
C++
Raw Normal View History

2024-09-19 02:22:41 +00:00
#include<bits/stdc++.h>
using namespace std;
int a[100], dp[100], trace[100];
/*
14
13 7 9 16 38 24 37 18 44 19 21 22 63 15
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
max=8
7 9 16 18 19 21 22 63
*/
int main()
{
int n;
cin>>n;
for(int i=1; i<=n; i++)
{
cin>>a[i]; // ԭʼ<D4AD><CABC><EFBFBD><EFBFBD>
dp[i]=1; // LIS
trace[i]=0; // <20><>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD><CEBB>
}
// <20><>LIS
for(int i=n-1; i>=1; i--)
{
int maxlis=0;
int maxindex=0;
for(int j=i+1; j<=n; j++)
{
if(a[j]>a[i] && dp[j]>maxlis)
{
maxlis=dp[j];
maxindex=j;
}
}
if(maxlis>0)
{
dp[i]=maxlis+1;
trace[i]=maxindex;
}
}
int pos=1;
for(int i=2; i<=n; i++)
{
if(dp[i]>dp[pos])
pos=i;
}
cout<<"max="<<dp[pos]<<endl;
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EEB3A4><EFBFBD>½<EFBFBD><C2BD><EFBFBD><EFBFBD><EFBFBD>
while(pos!=0)
{
cout<<a[pos]<<" ";
pos=trace[pos];
}
return 0;
}