【BZOJ 1046】[HAOI2007]上升序列 lis

47 篇文章 0 订阅

额,一开始没有认真读题,我以为最小字典序输出是数字大小最小字典序,结果是位置,我靠水我呢。

倒过来做一次最长下降子序列(手顺一下子写成最长不上升子序列,一个元素有重复,一个没有重复),得到重每一个位置开始的最长上升子序列(就是倒过来的最长下降),输出的时候贪心,能输出就直接输出

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int n,m,a[20200],cnt[20300],c[20200],tot;

int find(int x){
	int l=1,r=tot;
	while(l<r){
		int mid=l+r>>1;
		if(c[mid]>x)l=mid+1;
		else r=mid;
	}
	return l;
}

void make(){
	for(int x,i=n;i>=1;i--){
		if(tot==0||a[i]<c[tot])c[++tot]=a[i],cnt[i]=tot;
		else {
			x=find(a[i]);
			c[x]=a[i];
			cnt[i]=x;
		}
	}
}
void Print(int x){
	int last=-1;
	for(int i=1;i<=n;i++){
		if(cnt[i]>=x&&a[i]>last){
			x--;
			printf("%d%c",a[i],x?' ':'\n'),last=a[i];
		}
		if(x==0)break;
	}
}

int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++)scanf("%d",a+i);
	make();int x;
	scanf("%d",&m);
	while(m--){
		scanf("%d",&x);
		if(x>tot)puts("Impossible");
		else Print(x);
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值