Description
给一个长度为n的序列,每一次求字典序最小的长度为L的严格上升子序列。字典序按照坐标排序。
Sample Input
6
3 4 1 2 3 6
3
6
4
5
Sample Output
Impossible
1 2 3 6
Impossible
一开始看错题以为是按权值为字典序排序,然后自信码完。。。
但其实原题也不难。
反着求一边LIS,每个询问就扫一遍n,如果他的LIS满足到达L个,即输出他,然后随便搞搞。。。
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int _max(int x, int y) {return x > y ? x : y;}
int read() {
int s = 0, f = 1; char ch = getchar();
while(ch < '0' || ch > '9') {if(ch == '-') f = -1; ch = getchar();}
while(ch >= '0' && ch <= '9') s = s * 10 + ch - '0', ch = getchar();
return s * f;
}
void put(int x) {
int num = 0; char c[15];
while(x) c[++num] = (x%10)+48, x /= 10;
while(num) putchar(c[num--]);
putchar(' ');
}
int tp, sta[11000];
int a[11000], b[11000], c[11000], f[11000];
int main() {
int n = read();
for(int i = 1; i <= n; i++) a[i] = read(), b[i] = a[i];
sort(b + 1, b + n + 1);
for(int i = 1; i <= n; i++) c[i] = a[i], a[i] = lower_bound(b + 1, b + n + 1, a[i]) - b;
int maxx = 0;
for(int i = n; i >= 1; i--) {
int hh = n + 1 - a[i];
if(sta[tp] < hh) sta[++tp] = hh, f[i] = tp;
else {
int pos = lower_bound(sta + 1, sta + tp + 1, hh) - sta;
sta[pos] = hh, f[i] = pos;
} maxx = _max(maxx, f[i]);
}
int m = read();
for(int i = 1; i <= m; i++) {
int hh = read();
if(hh > maxx) printf("Impossible\n");
else {
int last = 0;
for(int i = 1; i <= n; i++) {
if(hh == 0) break;
if(f[i] >= hh && c[i] > last) {
put(c[i]);
last = c[i], hh--;
}
} puts("");
}
}
return 0;
}