hdu 1025 Constructing Roads In JGShining's Kingdom(最长上升序列n*logn算法)
来源:互联网 发布:ansys14.5软件下载 编辑:程序博客网 时间:2024/06/05 08:50
用线段树实现求最长上升子序列,把复杂度降低到了n*log(n)*2。
#include<stdio.h>#include<string.h>#define N 500005struct node{ int x,y; int max;} a[N*3];int b[N];int Max(int x,int y){ if(x>y) return x; return y;}void CreatTree(int t,int x,int y){ a[t].x=x; a[t].y=y; a[t].max=0; if(x==y) return ; int mid=(x+y)/2; int temp=t*2; CreatTree(temp,x,mid); CreatTree(temp+1,mid+1,y); return ;}void InsertTree(int t,int x,int k){ if(a[t].x==a[t].y&&a[t].x==x) { a[t].max=k; return ; } int mid=(a[t].x+a[t].y)/2; int temp=t*2; if(x<=mid) InsertTree(temp,x,k); else InsertTree(temp+1,x,k); a[t].max=Max(a[temp].max,a[temp+1].max); return ;}int FindTree(int t,int x,int y){ if(a[t].x==x&&a[t].y==y) { return a[t].max; } int temp=t*2; int mid=(a[t].x+a[t].y)/2; if(y<=mid) return FindTree(temp,x,y); else if(x>mid) return FindTree(temp+1,x,y); else return Max(FindTree(temp,x,mid),FindTree(temp+1,mid+1,y));}int main(){ int n; int cnt=1; while(scanf("%d",&n)!=EOF) { int i,x,y; for(i=1; i<=n; i++) { scanf("%d%d",&x,&y); b[x]=y; } CreatTree(1,1,n); for(i=1; i<=n; i++) { int temp; temp=FindTree(1,1,b[i])+1; InsertTree(1,b[i],temp); } if(a[1].max==1) { printf("Case %d:\n",cnt++); printf("My king, at most %d road can be built.\n\n",a[1].max); } else { printf("Case %d:\n",cnt++); printf("My king, at most %d roads can be built.\n\n",a[1].max); } } return 0;}
再补充一个之前看到过的别人的算法,通过维护一个上升的数组,使用二分查找,将时间复杂度降低到了n*logn。
#include<stdio.h>#include<string.h>#define N 500005int a[N],dp[N];int main(){ int n,cnt; cnt=1; while(scanf("%d",&n)!=EOF) { int x,y; int i; for(i=1;i<=n;i++) { scanf("%d%d",&x,&y); a[x]=y; } int l,r,mid; int len; dp[1]=a[1];len=1; for(i=2;i<=n;i++) { l=1;r=len; while(l<=r) { mid=(l+r)/2; if(a[i]>dp[mid]) l=mid+1; else r=mid-1; } dp[l]=a[i]; if(l>len) len++; } if(len==1) { printf("Case %d:\n",cnt++); printf("My king, at most %d road can be built.\n\n",len); } else { printf("Case %d:\n",cnt++); printf("My king, at most %d roads can be built.\n\n",len); } } return 0;}
- hdu 1025 Constructing Roads In JGShining's Kingdom(最长上升序列n*logn算法)
- HDU 1025 Constructing Roads In JGShining's Kingdom(最长上升子序列+n*logn算法)
- HDU 1025.Constructing Roads In JGShining's Kingdom【最长上升子序列n×logn算法】【1月6】
- hdu 1025 Constructing Roads In JGShining's Kingdom(最长上升子序列nlogn算法)
- hdu 1025 Constructing Roads In JGShining's Kingdom(最长上升子序列的o(nlog(n))算法)
- 最长上升子序列 nlogn算法 hdu 1025 Constructing Roads In JGShining's Kingdom
- hdu 1025 Constructing Roads In JGShining's Kingdom(树状数组求最长上升子序列)
- HDU 1025 Constructing Roads In JGShining's Kingdom(DP,LIS最长上升子序列)
- HDU 1025 Constructing Roads In JGShining's Kingdom(LIS最长上升子序列)
- 【HDU 1025】Constructing Roads In JGShining's Kingdom(最长上升子序列LIS)
- HDU 1025 A - Constructing Roads In JGShining's Kingdom(最长上升子序列)
- HDU 1025 Constructing Roads In JGShining's Kingdom(最长上升子序列的长度)
- hdu 1025 Constructing Roads In JGShining's Kingdom【即求最长上升子序列】
- hdu 1025 Constructing Roads In JGShining's Kingdom 最长上升序列
- hdu 1025 Constructing Roads In JGShining's Kingdom (最长上升子序列)
- hdu 1025 Constructing Roads In JGShining's Kingdom 最长上升子序列(nlogn)
- HDU-1025 Constructing Roads In JGShining's Kingdom O(nlogn)的最长上升子序列
- hdu 1025 Constructing Roads In JGShining's Kingdom(二分法+最长上升子序列)
- 在u-boot中添加命令hello
- Struts2的Convention插件的好处与使用
- 分别使用递归和非递归实现二分查找算法
- android的窗口机制分析------事件处理
- 递归练习
- hdu 1025 Constructing Roads In JGShining's Kingdom(最长上升序列n*logn算法)
- POJ 2377 最大生成树
- ffmpeg的使用
- POJ3630-静态字典树
- Tree.Panel各项属性
- HDU 2227 Find the nondecreasing subsequences (线段树)
- U盘启动安装Ubuntu 13.04
- Windows文件读写速度
- OpenCV 2 学习笔记(15): 绘制图像直方图