Time Limit: 10 Sec  Memory Limit: 162 MB
Submit: 5740  Solved: 2025
[Submit][Status][Discuss]

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

HINT

Source

非常套路的一道题

它的字典序是按下标排的,因此我们需要预处理出$f[i]$表示从$i$开始最长上升子序列的长度

这个可以通过倒着求最长下降子序列来实现

然后询问的时候若当前的$f[i]>=x$那么可以更新答案,直接暴力找就行

// luogu-judger-enable-o2
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#define LL long long
using namespace std;
const int MAXN = , INF = 1e9 + ;
inline int read() {
char c = getchar(); int x = , f = ;
while(c < '' || c > '') {if(c == '-') f = -; c = getchar();}
while(c >= '' && c <= '') x = x * + c - '', c = getchar();
return x * f;
}
int N, a[MAXN], low[MAXN], len, bg[MAXN], pre[MAXN];
int Find(int x) {
int l = , r = len, ans = len;
while(l <= r) {
int mid = (l + r) >> ;
if(x >= low[mid]) r = mid - , ans = mid;
else l = mid + ;
}
return ans;
}
main() {
memset(low, 0x3f, sizeof(low));
N = read();
for(int i = ; i <= N; i++) a[i] = read();
for(int i = N; i >= ; i--) {
if(a[i] < low[len]) low[++len] = a[i], bg[i] = len;
else {
int pos = Find(a[i]); // mmp这儿不能用stl。。
low[pos] = max(low[pos], a[i]);
bg[i] = pos;
}
}
int M = read();
while(M--) {
int x = read();
if(x > len) {puts("Impossible"); continue;}
for(int i = , j; i <= N; i++) {
if(bg[i] >= x) {
int pre = a[i];
printf("%d ", a[i]);
if(x == ) {puts(""); break;}
for(int j = i + ; j <= N; j++) {
if(bg[j] >= x - && a[j] > a[i])
{x--, i = j - ; break;}
}
}
}
}
return ;
}
05-11 15:36
查看更多