Luogu P2215 [HAOI2007] Ascending Sequence - Problem Solving


The main idea of ​​the title portal
is to ask for the smallest ascending subsequence of length L in lexicographic order. If it does not exist, output Impossible


Thought Process:
Memories n l O g n The longest ascending subsequence of What is the length of , so we can find the longest descending subsequence in reverse. (Note that the title requires the smallest lexicographical order, not the smallest numerical value!)


Specific methods:
1. Find the longest descending subsequence from the back to the front (the dp above is stored in the best array, where f represents the length of the longest ascending subsequence starting with this element)
2. Output the answer When looking at the best array first, there is no such long ascending subsequence to directly output Impossible, otherwise it starts from the first element and scans backwards. If the f value of the current element is greater than or equal to the required sequence length, the element is output. The required sequence length is reduced to 0


Code:

#include <bits/stdc++.h>
using namespace std;

const int maxn=1e4+100,maxm=1e3+100;
int cnt,n,m;
int best[maxn],f[maxn],a[maxn];

int find(int x)
{
    int l=1,r=cnt,nowans;
    while(l<=r)
    {
        int mid=(l+r)>>1;
        if(x<best[mid]) { nowans=mid;l=mid+1; }
        else r=mid-1;   
    }
    return nowans;
}

void put_out(int x)
{
    int now=1,last=0;
    while(x)
    {
        if(f[now]<x||a[now]<=last) { now++;continue; }
        printf("%d ",a[now]);
        last=a[now];
        now++;x--;
    }
    printf("\n");
}

int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    for(int i=n;i>=1;i--)
    {
        int t=find(a[i]);
        f[i]=t+1;cnt=max(cnt,f[i]);
        best[t+1]=max(best[t+1],a[i]);
    }
    scanf("%d",&m);
    for(int i=1;i<=m;i++)
    {
        int x;
        scanf("%d",&x);
        if(x>cnt) { printf("Impossible\n");continue; }
        put_out(x); 
    }
    return 0;   
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324814362&siteId=291194637