To output the longest ascending subsequence, you only need to add a pre array to record the first of the newly added sequence. You can traverse the array once.
The Code is as follows:
# Include <iostream>
# Include <cstdio>
# Include <cstring>
Using namespace STD;
Const int n= 1000;
Int num [N];
Int pre [N];
Int DP [N];
Int f [N];
Int main ()
{
Freopen ("data. In", "r", stdin );
Int N, I, j, ANS, flag;
While (~ Scanf ("% d", & n ))
{
For (I = 1; I <= N; I ++)
Scanf ("% d", & num [I]);
Memset (DP, 0, sizeof (DP ));
Memset (PRE, 0, sizeof (pre ));
Memset (F, 0, sizeof (f ));
DP [1] = 1;
For (I = 2; I <= N; I ++)
{
Ans = DP [I];
For (j = 1; j <I; j ++)
{
If (Num [I]> num [J] & Ans <DP [J])
{
Ans = DP [J];
If (! Pre [num [I] &! F [J])
{
Pre [num [I] = num [J]; // record
F [J] = 1;
Flag = num [I];
}
}
}
DP [I] = ans + 1;
}
Ans = 0;
I = flag;
While (I)
{
Printf ("% d", I );
I = pre [I];
}
For (I = 1; I <= N; I ++)
If (DP [I]> ans)
Ans = DP [I];
Printf ("\ n % d \ n", ANS );
}
Return 0;
}