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