hdu,1025 dp求最长递增序列。
2011-04-04 22:12
537 查看
注意 把第一列的城市看成固定的,求出另一列数的最长递增序列
用a[p]保存rich city的标号r,dp[i]存放的是长度为i的序列的末尾最小的那个a[i]值
dp[]的长度就是最多的道路数目。因为数据500000 故
用二分查找的方法更新dp
用a[p]保存rich city的标号r,dp[i]存放的是长度为i的序列的末尾最小的那个a[i]值
dp[]的长度就是最多的道路数目。因为数据500000 故
用二分查找的方法更新dp
#include<stdio.h> #include<stdlib.h> #include<string.h> int a[500001],dp[5000001]; //dp[i]用来存储长度为i时的最小值 int find(int a,int*dp,int low,int height) //每次加入的数和dp中的数进行比较直到返回第一个比a[i]大的数,更新 { int mid; while(low<=height) { mid=(low+height)/2; if(a>=dp[mid])low=mid+1; else height=mid-1; } return low; } int main() { int ncase,i,n,len,m,r,p,f=0; while(scanf("%d",&ncase)!=EOF) { f++; for(n=1;n<=ncase;n++) { scanf("%d%d",&p,&r); a[p]=r; } dp[1]=a[1];len=1; for(i=2;i<=ncase;i++) { if(a[i]>dp[len]) { len++; dp[len]=a[i]; } else { m=find(a[i],dp,1,len); dp[m]=a[i]; } }//要注意1条路的时候没有(S)road 是单数......... if(len==1) printf("Case %d:\nMy king, at most %d road can be built.\n\n",f,len); else printf("Case %d:\nMy king, at most %d roads can be built.\n\n",f,len); } return 0; }
相关文章推荐
- HDU 1025 最长递增子序列 DP+二分法
- 最长公共子序列 最长递增子序列(和) 最长递增公共子序列 最长(连续)子序列乘积(HDU)--dp
- [ACM] hdu 1025 Constructing Roads In JGShining's Kingdom (最长递增子序列,lower_bound使用)
- [ACM] hdu 1025 Constructing Roads In JGShining's Kingdom (最长递增子序列,lower_bound使用)
- [ACM] hdu 1025 Constructing Roads In JGShining's Kingdom (最长递增子序列,lower_bound使用)
- hdu 1087 Super Jumping! Jumping! Jumping!(dp+最长递增序列)
- DP_简单DP最长上升子序列[n*lg(n)](HDU_1025)
- 最长递增公共子序列dp(hdu 1423 hdu 4512)
- hdu 1087 Super Jumping! Jumping! Jumping!(dp+最长递增序列)
- hdu 1025 dp+二分求最长递增子序列
- HDU 1025-Constructing Roads In JGShining's Kingdom(最长不降子序列,线段树优化)
- UVa 10534 Wavio Sequence ( DP 二分 最长递增子序列 )
- 51nod 1218 最长递增子序列 V2 [dp]
- HDU 1257 ( 最少拦截系统 ) 最长单调子序列 二分+dp
- 最长上升子序列 HDU 1025 Constructing Roads In JGShining's Kingdom
- hdu 1422 重温世界杯(DP 最长非负子序列)
- HDU 1160 FatMouse's Speed--dP--(元素1递增元素2递降的最长子序列)
- hdu 1257 最少拦截系统(最长单调递增序列)
- AOJ 497 最长递增子序列 【DP】
- hdu 4604 Deque(DP递增递减序列,4级)