1046: [HAOI2007]上升序列

Description

对于一个给定的S={a1,a2,a3,…,an},若有P={ax1,ax2,ax3,…,axm},满足(x1 < x2 < … < xm)且( ax1 < ax2 < … < axm)。那么就称P为S的一个上升序列。如果有多个P满足条件,那么我们想求字典序最小的那个。任务给出S序列,给出若干询问。对于第i个询问,求出长度为Li的上升序列,如有多个,求出字典序最小的那个(即首先x1最小,如果不唯一,再看x2最小……),如果不存在长度为Li的上升序列,则打印Impossible.
Input

第一行一个N,表示序列一共有N个元素第二行N个数,为a1,a2,…,an 第三行一个M,表示询问次数。下面接M行每行一个数L,表示要询问长度为L的上升序列。
Output

对于每个询问,如果对应的序列存在,则输出,否则打印Impossible.
Sample Input
6
3 4 1 2 3 6
3
6
4
5
Sample Output
Impossible
1 2 3 6
Impossible
数据范围
N<=10000
M<=1000

先nlogn倒着做完最长上升,然后用一个数组b存以i为开头的最长上升子序列的长度

每个询问,从前往后扫一遍,能用就用,就是字典序最小的了(MD,一开始以为是数字最小,还排了一个序,原来是下标)

 1 const
 2     maxn=10010;
 3 var
 4     f,a,b:array[0..maxn]of longint;
 5     n,m,max:longint;
 6  
 7 procedure find(x:longint);
 8 var
 9     l,r,mid:longint;
10 begin
11     l:=1;
12     r:=max+1;
13     while l<>r do
14       begin
15         mid:=(l+r)>>1;
16         if f[mid]>a[x] then l:=mid+1
17         else r:=mid;
18       end;
19     if l>max then max:=l;
20     if f[l]<a[x] then f[l]:=a[x];
21     b[x]:=l;
22 end;
23  
24 procedure init;
25 var
26     i:longint;
27 begin
28     read(n);
29     for i:=1 to n do
30       begin
31         f[i]:=-100000000;
32         read(a[i]);
33       end;
34     for i:=n downto 1 do
35       find(i);
36 end;
37  
38 procedure work;
39 var
40     i,j,x,lasta:longint;
41 begin
42     read(m);
43     for i:=1 to m do
44       begin
45         read(x);
46         lasta:=-1000;
47         if x>max then write('Impossible')
48         else
49           for j:=1 to n do
50             if (b[j]>=x) and (a[j]>lasta) then
51             begin
52               dec(x);
53               lasta:=a[j];
54               if x>0 then write(a[j],' ')
55               else write(a[j]);
56               if x=0 then break;
57             end;
58         writeln;
59       end;
60 end;
61  
62 begin
63     init;
64     work;
65 end.
View Code
原文地址:https://www.cnblogs.com/Randolph87/p/3651187.html