您的位置:首页 > 其它

bzoj 1046 : [HAOI2007]上升序列 dp

2016-02-29 11:39 218 查看
题目链接

1046: [HAOI2007]上升序列

Time Limit: 10 Sec Memory Limit: 162 MB
Submit: 3620 Solved: 1236
[Submit][Status][Discuss]

Description

对于一个给定的S={a1,a2,a3,…,an},若有P={ax1,ax2,ax3,…,axm},满足(x1 < x2 < … < xm)且( ax1 < ax2 < … < axm)。那么就称P为S的一个上升序列。如果有多个P满足条件,那么我们想求字典序最小的那个。任务给出S序列,给出若干询问。对于第i个询问,求出长度为Li的上升序列,如有多个,求出字典序最小的那个(即首先x1最小,如果不唯一,再看x2最小……),如果不存在长度为Li的上升序列,则打印Impossible.

Input

第一行一个N,表示序列一共有N个元素第二行N个数,为a1,a2,…,an 第三行一个M,表示询问次数。下面接M行每行一个数L,表示要询问长度为L的上升序列。

Output

对于每个询问,如果对应的序列存在,则输出,否则打印Impossible.

Sample Input

6

3 4 1 2 3 6

3

6

4

5

Sample Output

Impossible

1 2 3 6

Impossible

HINT

数据范围

N<=10000

M<=1000

我们不能知道从某个位置开始的最长上升序列的长度, 但是我们可以知道以某个位置结束的最长下降序列的长度, 所以倒过来求一下最长下降子序列的长度就可以了。
这里的字典序并不是开头的数字的大小而是开头数字的坐标大小。

#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <queue>
#include <stack>
#include <bitset>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, n, a) for(int i = a; i<n; i++)
#define fi first
#define se second
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int mod = 1e9+7;
const int inf = 1061109567;
const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
int a[100005], dp[100005], stk[100000];
int main()
{
int n, m, len = 0;
cin>>n;
for(int i = n; i>=1; i--) {
scanf("%d", &a[i]);
dp[i] = 1;
}
for(int i = 2; i<=n; i++) {
for(int j = 1; j<i; j++) {
if(a[j]>a[i]) {
dp[i] = max(dp[i], dp[j]+1);
}
}
len = max(len, dp[i]);
}
reverse(a+1, a+n+1);
reverse(dp+1, dp+1+n);
int x;
cin>>x;
while(x--) {
scanf("%d", &m);
if(len < m) {
puts("Impossible");
continue;
}
int p = 0, last = -inf;
for(int i = 1; i<=n&&m!=0; i++)
if(dp[i]>=m&&a[i]>last) {
stk[++p] = i;
m--;
last = a[i];
}
for(int i = 1; i<p; i++)
printf("%d ", a[stk[i]]);
printf("%d\n",a[stk[p]]);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: