神奇的后往前处理

 

/**************************************************************
    Problem: 1046
    User: lxy8584099
    Language: C++
    Result: Accepted
    Time:2100 ms
    Memory:1992 kb
****************************************************************/
 
/*
    错误理解了题意 
    字典序最小 只是输出数对应的下标的字典序最小
    然后学会了新的最上上升序列的处理方法
    从后往前 用一个数组nxt记录已经处理过的 对应上升子序列的开头位置
    二分后面的最大的上升子序列 +1  更新nxt 这里更新保证最优 贪心
    之后就是一个n*m的输出了 
*/
#include<cstdio>
#include<vector>
using namespace std;
const int N=1e5+50;
int n,a[N],dp[N],nxt[N],m,maxn;
int find(int x)
{
    int l=1,r=maxn,mid,res=0;
    while(l<=r)
    {
        mid=(l+r)>>1;
        if(nxt[mid]>x) res=mid,l=mid+1;
        else r=mid-1;
    }
    return res;
}
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)  scanf("%d",&a[i]);
    for(int i=n;i>=1;i--)
    {
        dp[i]=find(a[i])+1;
        if(dp[i]>maxn) maxn=dp[i];
        if(nxt[dp[i]]<a[i]) nxt[dp[i]]=a[i]; 
        // 贪心 后面接的数坑定是越大就越能接上某些数 
    }
    scanf("%d",&m);
    while(m--)
    {
        int x; scanf("%d",&x);
        if(x>maxn) printf("Impossible\n");
        else
        {
            int now=0;
            for(int j=1;j<=n;j++)
                if(dp[j]>=x&&a[j]>now)
                {
                    printf("%d ",a[j]);
                    now=a[j]; x--;
                    if(x==0) break;
                }
            printf("\n");
        }
    }
    return 0;
}