BZOJ 1046: [HAOI2007]上升序列【动态规划】

f [ i ] f[i] f[i]表示以 a [ i ] a[i] a[i]为开头的最长上升子序列的长度。

显然可以暴力 O ( n 2 ) O(n^2) O(n2)转移得到 f f f

对于每一个询问,直接 O ( n ) O(n) O(n)扫一遍即可。

#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define db double
#define sg string
#define make make_pair
#define pint pair<int,int>
#define rel(i,x,y) for(int i=(x);i<(y);i++)
#define rep(i,x,y) for(int i=(x);i<=(y);i++)
#define red(i,x,y) for(int i=(x);i>=(y);i--)
#define res(i,x) for(int i=head[x];i;i=nxt[i])
using namespace std;

const int N=1e5+5;
const int Inf=1e18;
const db Eps=1e-10;

int ans[N];
int n,m,a[N],f[N];

inline int read() {
	int x=0;char ch=getchar();bool f=0;
	while(ch>'9'||ch<'0'){if(ch=='-')f=1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
	return f?-x:x;
}

inline void write(int x) {
	if(x<0)x=-x,putchar('-');
	if(x>9)write(x/10);putchar(x%10+48);
}

int main() {
	n=read();
	
	rep(i,1,n) a[i]=read();
	
	f[n]=1;
	
	red(i,n-1,1) rep(j,i+1,n) if(a[i]<a[j]) f[i]=max(f[i],f[j]+1);
	
	m=read();
	
	while(m--) {
		ans[0]=0;
		
		int pre=0,flag=0,len=read();
		
		rep(i,1,n) {
			if(f[i]>=len&&a[i]>pre) {
				pre=a[i];ans[++ans[0]]=a[i];
				if(--len==0) {
					flag=1;break ;
				}
			}
		}
		
		if(flag) rep(i,1,ans[0]) printf("%d ",ans[i]);
		else printf("Impossible");
		
		puts("");
	}

	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值