COGS 渡轮问题 (LIS规定字典序输出方案数)

时间:2022-04-28 16:25:35
/*
下标字典序最小
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#define maxn 10010
using namespace std;
int n,a[maxn],f[maxn],pre[maxn],len=,k=,ans[maxn],size;
int main()
{
//freopen("maxxl.in","r",stdin);
//freopen("maxxl.out","w",stdout);
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
f[i]=;
}
for(int i=;i<=n;i++)
for(int j=;j<i;j++)
if(a[i]>=a[j]&&f[i]<f[j]+)
{
f[i]=f[j]+;pre[i]=j;
if(f[i]>len)
{
len=f[i];
k=i;
}
}
printf("%d\n",len);
while(k)
{
ans[++size]=a[k];
k=pre[k];
}
for(int i=size;i>=;i--)
printf("%d ",ans[i]);
return ;
}
/*
序列字典序最小
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#define maxn 10010
using namespace std;
int n,x,c[maxn],ans[maxn],len,pre[maxn];
int main()
{
//freopen("maxxl.in","r",stdin);
//freopen("maxxl.out","w",stdout);
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%d",&x);
if(x>c[len])
{
pre[x]=c[len];
c[++len]=x;
continue;
}
int l=,r=len,p=;
while(l<=r)
{
int mid=(l+r)/;
if(c[mid]<x)
{
p=mid;
l=mid+;
}
else r=mid-;
}
pre[x]=c[p];
c[p+]=x;
}
int k=c[len],size=;
printf("%d\n",len);
while(k)
{
ans[++size]=k;
k=pre[k];
}
for(int i=size;i>=;i--)
printf("%d ",ans[i]);
return ;
}