您的位置:首页 > 其它

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

2016-11-06 20:06 381 查看
额,一开始没有认真读题,我以为最小字典序输出是数字大小最小字典序,结果是位置,我靠水我呢。

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

#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: