首先这肯定是一道LIS的变形,这次求的是方案,还要求字典序最小
(注意这个字典序最小是指下标最小而不是数最小)
首先预处理以每个数为首,能组成多长的上升序列(这里我们用单调队列解决)
然后按照位置顺序扫一遍顺出可行即可
要注意每行末的空格
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 var f,a,q:array[0..10010] of longint; 2 n,k,i,x,j,t,l,r,m:longint; 3 begin 4 readln(n); 5 for i:=1 to n do 6 read(a[i]); 7 a[0]:=2147483647; 8 for i:=n downto 1 do 9 begin 10 if (a[q[t]]>a[i]) then 11 begin 12 inc(t); 13 q[t]:=i; 14 f[i]:=t; 15 end 16 else begin 17 l:=1; 18 r:=t; 19 while l<=r do 20 begin 21 m:=(l+r) shr 1; 22 if (a[q[m]]<=a[i]) and (a[q[m-1]]>a[i]) then break; 23 if (a[q[m]]<=a[i]) then r:=m-1 else l:=m+1; 24 end; 25 f[i]:=f[q[m]]; 26 q[m]:=i; 27 end; 28 end; 29 readln(k); 30 for i:=1 to k do 31 begin 32 readln(x); 33 j:=1; 34 t:=-2147483647; 35 while (j<=n) and (x>0) do 36 begin 37 if (f[j]>=x) and (a[j]>t) then 38 begin 39 write(a[j]); 40 if x<>1 then write(' '); 41 dec(x); 42 t:=a[j]; 43 end; 44 inc(j); 45 end; 46 if x>0 then writeln('Impossible') 47 else writeln; 48 end; 49 end.