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 ;
}

最新文章

  1. Android之卫星菜单的实现
  2. css li 不换行(布局,内容)
  3. 20145301&amp;20145321&amp;20145335实验二
  4. Linux Shell系列教程之(十五) Shell函数简介
  5. 【转】IOS中的release和nil
  6. oracle_partition sample_simple
  7. android 51 有序广播
  8. Linux高性能server编程——高级I/O函数
  9. Hadoop集群
  10. TypeScript入门(二)函数新特性
  11. redis-hash操作
  12. PowerScript数据类型及变量
  13. SQL 关联外键报错类型不匹配
  14. Gitee vs插件(Gitee Extension for Visual Studio)
  15. 【linux下查看文件路径--jdk】
  16. 利用Burp Suite攻击Web应用
  17. Android学习之基础知识十三 — 四大组件之服务详解第一讲
  18. eclipse中中文注释乱码怎么解决
  19. swift设计模式学习 - 代理模式
  20. linux安装oracle11g步骤

热门文章

  1. C作业--数据类型
  2. 201621123068 Week04-面向对象设计与继承
  3. python 单向链表实现
  4. JAVA线程概念
  5. ArcGIS地图打印那些事
  6. OO第一次阶段性总结
  7. 码农、黑客和2B程序员之间的区别
  8. 解决IE8下CSS3选择器 :nth-child() 不兼容的问题
  9. 新概念英语(1-119)who call out to the thieves in the dark?
  10. OAuth2.0学习(3-1)发布 spring-oauth-client 和 spring-oauth-server