您的位置:首页 > 其它

BZOJ 1046: [HAOI2007]上升序列 LIS

2017-08-20 10:31 387 查看

1046: [HAOI2007]上升序列

Time Limit: 10 Sec  Memory Limit: 162 MB
Submit: 5004  Solved: 1741

[Submit][Status][Discuss]

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的上升序列。N<=10000,M<=1000

Output

  对于每个询问,如果对应的序列存在,则输出,否则打印Impossible.

Sample Input

6

3 4 1 2 3 6

3

6

4

5

Sample Output

Impossible

1 2 3 6

Impossible

预处理出以每一个数为起点最长的LIS长度

询问直接扫就行了

#include<cmath>
#include<ctime>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<iomanip>
#include<vector>
#include<string>
#include<bitset>
#include<queue>
#include<map>
#include<set>
using namespace std;

inline int read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch<='9'&&ch>='0'){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
return x*f;
}
void print(int x)
{if(x<0)putchar('-'),x=-x;if(x>=10)print(x/10);putchar(x%10+'0');}

const int N=100100;

int a
,f
,st
,top,n;

void initial()
{
register int i,l,r,mid,res;
for(i=n;i;i--)
{
l=1;r=top;res=0;
while(l<=r)
{
mid=(l+r)>>1;
if(st[mid]>a[i])res=mid,l=mid+1;
else r=mid-1;
}
f[i]=res+1;
top=max(top,res+1);
st[res+1]=a[i];
}
}

void solve(int x)
{
register int i,last=0;
for(i=1;i<=n;i++)if(f[i]>=x&&a[i]>last)
{print(a[i]);if(x^1)putchar(' ');last=a[i];x--;if(!x)break;}
puts("");
}

int main()
{
n=read();
register int i,x;
for(i=1;i<=n;++i)a[i]=read();
initial();
int Q=read();
while(Q--){x=read();if(x<=top)solve(x);else puts("Impossible");}
return 0;
}
/*
6
3 4 1 2 3 6
3
6
4
5

Impossible
1 2 3 6
Impossible
*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: