hdu 1025 Constructing Roads In JGShining's Kingdom(最长上升序列n*logn算法)
2013-09-01 14:22
337 查看
用线段树实现求最长上升子序列,把复杂度降低到了n*log(n)*2。
再补充一个之前看到过的别人的算法,通过维护一个上升的数组,使用二分查找,将时间复杂度降低到了n*logn。
#include<stdio.h> #include<string.h> #define N 500005 struct node { int x,y; int max; } a[N*3]; int b ; 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 500005 int a ,dp ; 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(最长上升子序列的长度)
- DP_简单DP最长上升子序列[n*lg(n)](HDU_1025)
- 最长上升子序列 HDU 1025 Constructing Roads In JGShining's Kingdom
- hdu 1025 Constructing Roads In JGShining's Kingdom【即求最长上升子序列】
- hdu 1025 Constructing Roads In JGShining's Kingdom【即求最长上升子序列】
- 最长上升子序列(LIS)(HDU 1025)
- HDU 1025 DP+二分求解最长上升序列
- HDU 1025 高效最长上升子序列(二分查)
- 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 最长上升序列nlogn
- HDU 1025-Constructing Roads In JGShining's Kingdom(最长上升子序列 NlogN复杂度)
- hdu 1025 Constructing Roads In JGShining's Kingdom LIS最长上升序列
- hdu 4512 吉哥系列故事——完美队形I(最长上升公共子序列)
- hdu 1423 最长上升递增子序列
- hdu 1025 Constructing Roads In JGShining's Kingdom(树状数组求最长上升子序列)
- HDU-1257 最少拦截系统 贪心/DP 最长上升子序列的长度==最长不上升子序列的个数?
- 【最长上升子序列 && 输出路径】HDU - 1160 FatMouse's Speed
- HDU 1025-Constructing Roads In JGShining's Kingdom(最长不降子序列,线段树优化)
- HDU 1087 Super Jumping! Jumping! Jumping!(最长上升子序列,dp)