1046: [HAOI2007]上升序列
Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 5222 Solved: 1815
[ Submit][ Status][ Discuss]
Description
对于一个给定的S={a1,a2,a3,…,an},若有P={ax1,ax2,ax3,…,axm},满足(x1 < x2 < … < xm)且( ax1 < ax
2 < … < axm)。那么就称P为S的一个上升序列。如果有多个P满足条件,那么我们想求字典序最小的那个。任务给
出S序列,给出若干询问。对于第i个询问,求出长度为Li的上升序列,如有多个,求出字典序最小的那个(即首先
x1最小,如果不唯一,再看x2最小……),如果不存在长度为Li的上升序列,则打印Impossible.
Input
第一行一个N,表示序列一共有N个元素第二行N个数,为a1,a2,…,an 第三行一个M,表示询问次数。下面接M
行每行一个数L,表示要询问长度为L的上升序列。N<=10000,M<=1000
Output
对于每个询问,如果对应的序列存在,则输出,否则打印Impossible.
Sample Input
6
3 4 1 2 3 6
3
6
4
5
3 4 1 2 3 6
3
6
4
5
Sample Output
Impossible
1 2 3 6
Impossible
1 2 3 6
Impossible
HINT
Source
code
LIS一开始求错了,尴尬了好久。我太弱了!!
求出LIS,询问>lis就impossible,否则二分求字典序最小的合法序列。
#include<bits/stdc++.h>
using namespace std;
inline void read(int &res){
static char ch;int flag=1;
while((ch=getchar())<'0'||ch>'9')if(ch=='-')flag=-1;res=ch-48;
while((ch=getchar())>='0'&&ch<='9')res=res*10+ch-48;res*=flag;
}
const int N = 100005;
int a[N],dp[N],n,m,cnt,mx[N];
int main(){
read(n);
for(register int i=1;i<=n;i++)read(a[i]);
for(register int l,r,tmp,i=n;i>=1;i--){
l=1,r=cnt,tmp=0;
while(l<=r){
register int mid=(l+r)>>1;
if(mx[mid]>a[i])tmp=mid,l=mid+1;
else r=mid-1;
}
dp[i]=tmp+1;
cnt=max(cnt,dp[i]);
mx[dp[i]]=max(mx[dp[i]],a[i]);
}
read(m);
while(m--){
int x;read(x);
if(x<=cnt){
int last=0;
for(register int j=1;j<=n;j++)
if(dp[j]>=x&&a[j]>last){
printf("%d",a[j]);
if(x!=1)printf(" ");
last=a[j];--x;
if(!x)break;
}
puts("");
}else puts("Impossible");
}
return 0;;
}