【BZOJ】1046: [HAOI2007]上升序列(dp)

http://www.lydsy.com/JudgeOnline/problem.php?id=1046

一直看错题。。。。。。。。。。。。。。。。。。。。。。。

这是要求位置的字典序啊QQQAAAQQQ

。。

那么就lis后直接从前往后扫就行了。。

注意输出方案不要写错。。(wa了好多发。。。)

拓展:同时如果求答案的字典序最小,那么我们可以先对所有元素排序,然后一个个去试,即维护当前的lis长度,看当前的这个点是否能被接上(因为排序后是单调的,lis也是单调的)

#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
using namespace std;
typedef long long ll;
#define pii pair<int, int>
#define mkpii make_pair<int, int>
#define pdi pair<double, int>
#define mkpdi make_pair<double, int>
#define pli pair<ll, int>
#define mkpli make_pair<ll, int>
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << (#x) << " = " << (x) << endl
#define error(x) (!(x)?puts("error"):0)
#define printarr2(a, b, c) for1(_, 1, b) { for1(__, 1, c) cout << a[_][__]; cout << endl; }
#define printarr1(a, b) for1(_, 1, b) cout << a[_] << '\t'; cout << endl
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
inline const int max(const int &a, const int &b) { return a>b?a:b; }
inline const int min(const int &a, const int &b) { return a<b?a:b; }

const int N=10005, oo=~0u>>1;
int a[N], f[N], g[N], n, mx;
bool cmp(const int &a, const int &b) { return a>b; }
void lis() {
	CC(g, 128);
	for3(i, n, 1) {
		int t=lower_bound(g+1, g+1+(n-i+1), a[i], cmp)-g; dbg(t);
		f[i]=t;
		g[t]=a[i];
		mx=max(mx, t);
	}
}

int main() {
	read(n);
	for1(i, 1, n) read(a[i]);
	lis();
	int tot=getint();
	while(tot--) {
		int m=getint();
		if(m>mx) { puts("Impossible"); continue; }
		int now=-oo;
		for1(i, 1, n) if(m && f[i]>=m && a[i]>now) { printf("%d", a[i]); m==1?puts(""):printf(" "); --m; now=a[i]; } //注意特判m!=0
	}
	return 0;
}

  

 


 

 

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

 

Source

 

 
posted @ 2014-11-13 22:08  iwtwiioi  阅读(382)  评论(0编辑  收藏  举报