【BZOJ 1046】[HAOI2007]上升序列 lis

来源:互联网 发布:h3c路由器端口镜像 编辑:程序博客网 时间:2024/05/14 02:26

额,一开始没有认真读题,我以为最小字典序输出是数字大小最小字典序,结果是位置,我靠水我呢。

倒过来做一次最长下降子序列(手顺一下子写成最长不上升子序列,一个元素有重复,一个没有重复),得到重每一个位置开始的最长上升子序列(就是倒过来的最长下降),输出的时候贪心,能输出就直接输出

#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;int n,m,a[20200],cnt[20300],c[20200],tot;int find(int x){int l=1,r=tot;while(l<r){int mid=l+r>>1;if(c[mid]>x)l=mid+1;else r=mid;}return l;}void make(){for(int x,i=n;i>=1;i--){if(tot==0||a[i]<c[tot])c[++tot]=a[i],cnt[i]=tot;else {x=find(a[i]);c[x]=a[i];cnt[i]=x;}}}void Print(int x){int last=-1;for(int i=1;i<=n;i++){if(cnt[i]>=x&&a[i]>last){x--;printf("%d%c",a[i],x?' ':'\n'),last=a[i];}if(x==0)break;}}int main(){scanf("%d",&n);for(int i=1;i<=n;i++)scanf("%d",a+i);make();int x;scanf("%d",&m);while(m--){scanf("%d",&x);if(x>tot)puts("Impossible");else Print(x);}return 0;}


0 0
原创粉丝点击