[HAOI2007]上升序列 DP+贪心

48 篇文章 0 订阅
9 篇文章 0 订阅

Description
给一个长度为n的序列,每一次求字典序最小的长度为L的严格上升子序列。字典序按照坐标排序。


Sample Input
6
3 4 1 2 3 6
3
6
4
5


Sample Output
Impossible
1 2 3 6
Impossible


一开始看错题以为是按权值为字典序排序,然后自信码完。。。
但其实原题也不难。
反着求一边LIS,每个询问就扫一遍n,如果他的LIS满足到达L个,即输出他,然后随便搞搞。。。


#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;
int _max(int x, int y) {return x > y ? x : y;}
int read() {
	int s = 0, f = 1; char ch = getchar();
	while(ch < '0' || ch > '9') {if(ch == '-') f = -1; ch = getchar();}
	while(ch >= '0' && ch <= '9') s = s * 10 + ch - '0', ch = getchar();
	return s * f;
}
void put(int x)   {  
    int num = 0; char c[15];
    while(x) c[++num] = (x%10)+48, x /= 10;
    while(num) putchar(c[num--]);
    putchar(' '); 
}

int tp, sta[11000];
int a[11000], b[11000], c[11000], f[11000];

int main() {
	int n = read();
	for(int i = 1; i <= n; i++) a[i] = read(), b[i] = a[i];
	sort(b + 1, b + n + 1);
	for(int i = 1; i <= n; i++) c[i] = a[i], a[i] = lower_bound(b + 1, b + n + 1, a[i]) - b;
	int maxx = 0;
	for(int i = n; i >= 1; i--) {
		int hh = n + 1 - a[i];
		if(sta[tp] < hh) sta[++tp] = hh, f[i] = tp;
		else {
			int pos = lower_bound(sta + 1, sta + tp + 1, hh) - sta;
			sta[pos] = hh, f[i] = pos;
		} maxx = _max(maxx, f[i]);
	}
	int m = read();
	for(int i = 1; i <= m; i++) {
		int hh = read();
		if(hh > maxx) printf("Impossible\n");
		else {
			int last = 0;
			for(int i = 1; i <= n; i++) {
				if(hh == 0) break;
				if(f[i] >= hh && c[i] > last) {
					put(c[i]);
					last = c[i], hh--;
				}
			} puts("");
		}
	}
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值