#P2215. [HAOI2007] 上升序列

    ID: 1189 远端评测题 1000ms 125MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>动态规划,dp贪心递推2007河南各省省选

[HAOI2007] 上升序列

Description

Given a sequence S={a1,a2,a3,,an}S=\{a_1,a_2,a_3,…,a_n\}, if there exists P={ax1,ax2,ax3,,axm}P=\{a_{x_1},a_{x_2},a_{x_3},…,a_{x_m}\} such that (x1<x2<<xm)(x_1<x_2<…<x_m) and (ax1<ax2<<axm)(a_{x_1}<a_{x_2}<…<a_{x_m}), then PP is called an increasing subsequence of SS. If multiple PP satisfy the conditions, we want the lexicographically smallest one.

Task:

Given the sequence SS and several queries. For the ii-th query, find an increasing subsequence of length LiL_i. If there are multiple, choose the lexicographically smallest one (i.e., first minimize x1x_1; if it is not unique, then minimize x2x_2, and so on). If no increasing subsequence of length LiL_i exists, print Impossible.

Input Format

  • The first line contains an integer NN, the number of elements in the sequence.
  • The second line contains NN integers: a1,a2,,ana_1, a_2 , \cdots , a_n.
  • The third line contains an integer MM, the number of queries. Each of the next MM lines contains one integer LL, asking for an increasing subsequence of length LL.

Output Format

For each query:

  • If a valid subsequence exists, output the chosen subsequence’s elements ax1,ax2,,axLa_{x_1}, a_{x_2}, \dots, a_{x_L} on one line, separated by single spaces.
  • Otherwise, print Impossible.
6
3 4 1 2 3 6
3
6
4
5

Impossible
1 2 3 6
Impossible

6
6 7 1 2 3 4
1
2

6 7

Hint

Constraints: N10000N \le 10000, M1000M \le 1000. The testdata are random.

Translated by ChatGPT 5