[HAOI 2007]上升序列

时间:2023-03-08 22:46:33

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

Sample Output

Impossible
1 2 3 6
Impossible

题解

比较暴力...

首先做一般的 $lis$ 都可以获得一个数组,如 $f_i$ 表示 $i$ 这个位置以前以 $a_i$ 结尾的最长上升子序列的长度。

我们考虑反着做,记 $f_i$ 表示 $i$ 这个位置之后以 $a_i$ 开头的最长上升子序列的长度。

然后处理询问 $len$ 的时候只需要从 $1$ 到 $n$ 扫一遍,记 $last$ 为上一个选出的数, $x$ 为待选序列长度。如果 $a_i > last$ 且 $f_i \geq x$ ,便选上,将 $x-1$ 。

 //It is made by Awson on 2018.1.4
#include <set>
#include <map>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define LD long double
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define Min(a, b) ((a) < (b) ? (a) : (b))
using namespace std;
const int N = ;
const int INF = ~0u>>; int n, m, a[N+], f[N+], w[N+], x, maxlen; void print(int x) {
int last = ;
for (int i = ; i <= n; i++) {
if (f[i] >= x && last < a[i] && x) {
if (last != ) printf(" ");
last = a[i];
printf("%d", a[i]);
x--;
}
}
printf("\n");
}
int dev(int l, int r, int val) {
int ans = ;
while (l <= r) {
int mid = (l+r)>>;
if (w[mid] > val) l = mid+, ans = mid;
else r = mid-;
}
return ans;
}
void work() {
scanf("%d", &n); w[] = INF;
for (int i = ; i <= n; i++) scanf("%d", &a[i]);
for (int i = n; i >= ; i--) {
int pos = dev(, maxlen, a[i]); maxlen = Max(maxlen, pos+);
f[i] = pos+;
if (f[i] == maxlen) w[maxlen] = a[i];
else w[f[i]] = Max(w[f[i]], a[i]);
}
scanf("%d", &m);
while (m--) {
scanf("%d", &x);
if (x > maxlen) printf("Impossible\n");
else print(x);
}
}
int main() {
work();
return ;
}