【动态规划】【二分】CDOJ1006 最长上升子序列

最长上升子序列。

要求输出字典序最小解。

就在更新答案的时候记录一下前驱。容易发现记录的这个玩意实际上形成了一个森林。

#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int T,n,a[1010],b[1010],pre[1010],anss[1010],e,pos[1010];
int main(){
	int x;
	scanf("%d",&T);
	for(;T;--T){
		e=0;
//		memset(pre,0,sizeof(pre));
		scanf("%d",&n);
		int m=0;
		for(int i=1;i<=n;++i){
			scanf("%d",&a[i]);
			int* p=lower_bound(b+1,b+m+1,a[i]);
			if(p-b>m){
				++m;
			}
			*p=a[i];
			pos[p-b]=i;
			pre[i]=pos[p-b-1];
		}
		printf("%d ",m);
		int U=pos[m];
		while(U){
			anss[++e]=a[U];
			U=pre[U];
		}
		for(int i=m;i>=1;--i){
			printf("%d ",anss[i]);
		}
		puts("");
	}
	return 0;
}
原文地址:https://www.cnblogs.com/autsky-jadek/p/6935108.html