【题目链接】
【算法】
先预处理 : 将序列反转,求最长下降子序列
对于每个询问,根据字典序性质,贪心即可
【代码】
#include<bits/stdc++.h>
using namespace std;
#define MAXN 10010
int i,j,len,n,q,mx,pre,l;
int a[MAXN],f[MAXN];
vector<int> res;
template <typename T> inline void read(T &x)
{
int f = 1; x = 0;
char c = getchar();
for (; !isdigit(c); c = getchar()) { if (c == '-') f = -f; }
for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + c - '0';
x *= f;
}
template <typename T> inline void write(T x)
{
if (x < 0)
{
putchar('-');
x = -x;
}
if (x > 9) write(x/10);
putchar(x%10+'0');
}
template <typename T> inline void writeln(T x)
{
write(x);
puts("");
}
int main() {
read(n);
for (i = 1; i <= n; i++) read(a[i]);
reverse(a+1,a+n+1);
for (i = 1; i <= n; i++)
{
f[i] = 1;
for (j = i - 1; j >= 1; j--)
{
if (a[i] < a[j])
f[i] = max(f[i],f[j]+1);
}
mx = max(mx,f[i]);
}
read(q);
while (q--)
{
read(l);
if (l > mx)
{
puts("Impossible");
continue;
} else
{
res.clear();
pre = 0;
for (i = n; i >= 1; i--)
{
if (f[i] >= l && a[i] > pre)
{
res.push_back(a[i]);
l--;
pre = a[i];
}
if (!l) break;
}
len = res.size();
for (i = 0; i < len - 1; i++)
{
write(res[i]);
putchar(' ');
}
writeln(res[len-1]);
}
}
return 0;
}