LIS(正向)输出路径(n * logn版)
原作者:Lj_三日小先森
原文:https://blog.csdn.net/Lj_victor/article/details/81603657
我复制了其代码,并略加改动
/// -7 10 9 2 3 8 8 1
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = (int)1000 + 10;
int a[maxn], dp[maxn];
int pos[maxn];///记录位置
int list[maxn];///记录路径
int n;
int main()
{
scanf("%d", &n);
memset(a, 0, sizeof(a));
memset(dp, 0, sizeof(dp));
for(int i = 1; i <= n; ++i)
scanf("%d", &a[i]);
int len = 1;
int po = 1;
dp[1] = a[1], pos[1] = 1;
for(int i = 2; i <= n; ++i)
{
if(a[i] > dp[len])///上升此处为 >
{
dp[++len] = a[i];
pos[++po] = len;///用来记录从a[i]中1到n每个位置的在dp中的位置
} ///a[i] -7 10 9 2 3 8 8 1
else ///pos[j] 1 2 2 2 3 4 4 2
{
*lower_bound(dp + 1,dp + len + 1,a[i]) = a[i];
pos[++po] = lower_bound(dp + 1,dp + len + 1,a[i]) - dp;
}
}
///for(int i = 1;i<=n;i++)
/// printf("%d",pos[i]);
int t = len;
for(int i = n; i >= 1; --i)
{
if(pos[i] == t)
{
list[t--] = a[i];
}
if (t < 1)
break;
}
for(int i = 1; i <= len; i++)
printf("%d\n", list[i]);
return 0;
}