要保证长度为L的序列下标字典序最小,当然要尽量选前面的数。
如何判断前面的数是否满足条件?,只需要知道这个数开头的递增序列的最长长度是多少,如果不小于L,那么必然可以加入这个数。还需判断一下它是否大于前面的那个数就行了。
LIS用nlogn。
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
# include <cstdio> # include <cstring> # include <cstdlib> # include <iostream> # include <vector> # include <queue> # include <stack> # include <map> # include <set> # include <cmath> # include <algorithm> using namespace std; # define lowbit(x) ((x)&(-x)) # define pi acos(-1.0) # define eps 1e-9 # define MOD 1000000007 # define INF 1000000000 # define mem(a,b) memset(a,b,sizeof(a)) # define FOR(i,a,n) for(int i=a; i<=n; ++i) # define FO(i,a,n) for(int i=a; i<n; ++i) # define bug puts("H"); # define lch p<<1,l,mid # define rch p<<1|1,mid+1,r # define mp make_pair # define pb push_back typedef pair<int,int> PII; typedef vector<int> VI; # pragma comment(linker, "/STACK:1024000000,1024000000") typedef long long LL; int Scan() { int res=0, flag=0; char ch; if((ch=getchar())=='-') flag=1; else if(ch>='0'&&ch<='9') res=ch-'0'; while((ch=getchar())>='0'&&ch<='9') res=res*10+(ch-'0'); return flag?-res:res; } void Out(int a) { if(a<0) {putchar('-'); a=-a;} if(a>=10) Out(a/10); putchar(a%10+'0'); } const int N=10005; //Code begin... int a[N], dp[N], len[N], num=0; int bin_sea(int x) { int l=1, r=num+1, mid; while (l<r) { mid=(l+r)>>1; if (len[mid]<=x) r=mid; else l=mid+1; } if (r==num+1) ++num; len[r]=x; return r; } int main () { int n, m, l, ma=0; scanf("%d",&n); FOR(i,1,n) scanf("%d",a+i); for (int i=n; i>=1; --i) dp[i]=bin_sea(a[i]), ma=max(ma,dp[i]); scanf("%d",&m); while (m--) { scanf("%d",&l); if (l>ma) {puts("Impossible"); continue;} int pre=0; FOR(i,1,n) { if (dp[i]>=l&&a[i]>pre) printf(l==1?"%d":"%d ",a[i]), --l, pre=a[i]; if (!l) break; } putchar('\n'); } return 0; }