首页 > 代码库 > 【动态规划】【二分】CDOJ1006 最长上升子序列
【动态规划】【二分】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; }
【动态规划】【二分】CDOJ1006 最长上升子序列
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。