bzoj1046: [HAOI2007]上升序列

多次询问最长上升序列的最小字典序。反过来求最长上升子序列。每次从左到右扫一边就可以了

#include<cstdio>
#include<cstring>
#include<cctype>
#include<algorithm>
using namespace std;
#define rep(i,s,t) for(register int i=s;i<=t;i++)
#define dwn(i,s,t) for(register int i=s;i>=t;i--)
#define clr(x,c) memset(x,c,sizeof(x))
int read(){
    int x=0,f=1;char c=getchar();
    while(!isdigit(c)){
        if(c=='-') f=-1;c=getchar();
    }
    while(isdigit(c)) x=x*10+c-'0',c=getchar();
    return x*f;
}
const int nmax=1e4+5;
const int inf=0x7f7f7f7f;
int a[nmax],b[nmax],f[nmax],n;
void mins(int &a,int b){
    if(a>b) a=b;
}
void maxs(int &a,int b){
    if(a<b) a=b;
}
int find(int x){
    int l=1,r=n,ans=0;
    while(l<=r){
        int mid=(l+r)>>1;
        if(b[mid]>x) ans=mid,l=mid+1;
        else r=mid-1;
    }
    return ans;
}
int main(){
    n=read();int mx=0;
    rep(i,1,n) a[i]=read();
    dwn(i,n,1){
        int t=find(a[i])+1;f[i]=t;
        maxs(mx,t);maxs(b[t],a[i]);
    }
    //rep(i,1,n) printf("%d ",f[i]);printf("
");
    int m=read(),u,v;
    rep(j,1,m){
        u=read();v=-inf;
        if(u>mx) printf("Impossible
");
        else {
            bool flag=0;
            rep(i,1,n) {
                if(f[i]>=u&&a[i]>v) {
                    if(!flag) printf("%d",a[i]),flag=1;
                    else printf(" %d",a[i]);
                    u--;v=a[i];
                    if(!u) break;
                }
            }
            printf("
");
        }
    }
    return 0;
}

  

1046: [HAOI2007]上升序列

Time Limit: 10 Sec  Memory Limit: 162 MB
Submit: 4166  Solved: 1421
[Submit][Status][Discuss]

Description

  对于一个给定的S={a1,a2,a3,…,an},若有P={ax1,ax2,ax3,…,axm},满足(x1 < x2 < … < xm)且( ax1 < ax
2 < … < 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

HINT

 

Source

 
[Submit][Status][Discuss]
原文地址:https://www.cnblogs.com/fighting-to-the-end/p/5859350.html