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
自己写的一直WA。。。没心情写题解了。。。自己看吧。。。
http://hzwer.com/4065.html
upd:做1049的时候突然想到要调一下这道题的代码。。。于是就过了。。
1 #include<iostream> 2 #include<cstdlib> 3 #include<cmath> 4 #include<cstring> 5 #include<cstdio> 6 #include<algorithm> 7 #include<string> 8 #include<map> 9 #include<queue> 10 #include<vector> 11 #include<set> 12 #define inf 1000000000 13 #define maxn 10000+5 14 #define maxm 10000+5 15 #define eps 1e-10 16 #define ll long long 17 #define for0(i,n) for(int i=0;i<=(n);i++) 18 #define for1(i,n) for(int i=1;i<=(n);i++) 19 #define for2(i,x,y) for(int i=(x);i<=(y);i++) 20 #define for3(i,x,y) for(int i=(x);i>=(y);i--) 21 #define for4(i,x) for(int i=head[x],y=e[i].go;i;i=e[i].next,y=e[i].go) 22 using namespace std; 23 int read(){ 24 int x=0,f=1;char ch=getchar(); 25 while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} 26 while(ch>='0'&&ch<='9'){x=10*x+ch-'0';ch=getchar();} 27 return x*f; 28 } 29 int a[maxn],f[maxn],p[maxn],ans=0,ans1=0,n; 30 int main(){ 31 //freopen("input.txt","r",stdin); 32 //freopen("output.txt","w",stdout); 33 n=read();p[0]=inf; 34 for1(i,n)a[i]=read(); 35 for(int i=n;i>0;i--){ 36 int l=1,r=ans1;ans=0; 37 while(l<=r){ 38 int mid=(l+r)>>1; 39 if(a[i]<p[mid]){l=mid+1;ans=mid;} 40 else r=mid-1; 41 } 42 f[i]=ans+1; 43 ans1=max(ans1,ans+1); 44 p[ans+1]=a[i]; 45 } 46 int m=read(); 47 for1(i,m){ 48 int x=read(); 49 if(x>ans1)printf("Impossible\n"); 50 else{ 51 int last=0; 52 for1(j,n){ 53 if(f[j]>=x&&a[j]>last){ 54 if(x>1)printf("%d ",a[j]); 55 else {printf("%d\n",a[j]);break;} 56 last=a[j]; 57 x--; 58 } 59 } 60 } 61 } 62 return 0; 63 }