博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【BZOJ】1046: [HAOI2007]上升序列(dp)
阅读量:7105 次
发布时间:2019-06-28

本文共 2424 字,大约阅读时间需要 8 分钟。

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

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

。。

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

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

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

#include 
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;typedef long long ll;#define pii pair
#define mkpii make_pair
#define pdi pair
#define mkpdi make_pair
#define pli pair
#define mkpli make_pair
#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 << endlinline 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
>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

 

 

转载地址:http://xxphl.baihongyu.com/

你可能感兴趣的文章
WLS_Oracle Weblogic管理概述(概念)
查看>>
UML的基本关联
查看>>
没有找到MSVCR100.dll解决方法
查看>>
wordpress调用函数大全
查看>>
触发器四(学习笔记)
查看>>
[LeetCode] Excel Sheet Column Number 求Excel表列序号
查看>>
Javascript闭包简单理解
查看>>
Android ---paint类
查看>>
spin_lock &amp; mutex_lock的差别?
查看>>
多种方法求解八数码问题
查看>>
VS2008下直接安装使用Boost库1.46.1版本号
查看>>
curl命令具体解释
查看>>
mysql update常见实例
查看>>
MFC显示GIF动画图片
查看>>
【HDU】4336 Card Collector
查看>>
Java正則表達式入门
查看>>
Linux进程间通信——使用命名管道
查看>>
C++编译器默默编写并调用哪些函数
查看>>
LoaderManager使用详解(二)---了解LoaderManager
查看>>
LeetCode - Longest Common Prefix
查看>>