[HDU] 1025 Constructing Roads In JGShining's Kingdom - 二分的求最大递增非连续子序列
2013-05-02 11:36
387 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1025
方法:求最大递增非连续子序列朴素的方法是o(n^2)的时间复杂度,而该题要超时,所以用二分的,具体实现参见链接http://www.programfan.com/blog/article.asp?id=13086
感想:熟悉上述链接的定理证明。
代码:
View Code
方法:求最大递增非连续子序列朴素的方法是o(n^2)的时间复杂度,而该题要超时,所以用二分的,具体实现参见链接http://www.programfan.com/blog/article.asp?id=13086
感想:熟悉上述链接的定理证明。
代码:
View Code
#include<iostream> #include<math.h> #include<algorithm> #include<stack> using namespace std; int const MAX =0x3f3f3f3f; struct Road { int p; int r; }; bool cmp(Road x,Road y) { if(x.p < y.p ) return true; return false; } Road roads[500001]; int counts[500001]; int main() { int n=0,tc=1; while(scanf("%d",&n)!=EOF) { memset(counts,0,sizeof(counts)); for(int i=0;i<n;i++) scanf("%d %d",&roads[i].p,&roads[i].r); sort(roads,roads+n,cmp); counts[1] = roads[0].r; counts[0] = 0; int len=1; for(int i=1;i<n;i++) { int p = 0,ed = len,mid; while(p<=ed) { mid = (p+ed)/2; if(counts[mid]>=roads[i].r) ed = mid-1; else p=mid+1; } counts[p]=roads[i].r; if(p>len)len++; } cout<<"Case "<<tc<<":"<<endl; if(len>1) cout<<"My king, at most "<<len<<" roads can be built."<<endl; else cout<<"My king, at most "<<len<<" road can be built."<<endl; cout<<endl; tc++; } return 0; }
相关文章推荐
- hdu杭电1950 Bridging signals【最大递增子序列(不连续)】
- HDU1087 Super Jumping! Jumping! Jumping! DP求非连续单调递增序列的最大和
- 最大连续递增/递减/非递增/非递减子序列的长度(二分优化)
- [ACM] hdu 1025 Constructing Roads In JGShining's Kingdom (最长递增子序列,lower_bound使用)
- hdu(1025)(最大单调递增序列)
- HDU1069 Monkey and Banana DP非连续单调递增序列的最大和的变种
- HDU 1231 最大连续子序列(最大连续子段和)
- HDU 1025 Constructing Roads In JGShining's Kingdom 最长子序列的nlogn算法
- 最大递增子序列--动态规划+二分
- Max Sum 最大连续和的子序列 HDU 1003 (一维序列DP)
- HDU 1069 最大子序列和(不连续)变形
- hdu 1025 dp+二分求最长递增子序列
- Max Sum 最大连续和的子序列 HDU 1003 (一维序列DP)
- hdu3308--LCIS 最大连续递增序列长度
- hdu 1025 Constructing Roads In JGShining's Kingdom【即求最长上升子序列】
- hdu 5748 Bellovin (最长递增子序列 二分查找)
- Max Sum 最大连续和的子序列 HDU 1003 (一维序列DP)
- HDU ACMA 1025 最小递增序列, 二分
- 1025 最大递增子序列+二分 nlogn
- HDU 1087 Super Jumping! Jumping! Jumping! 最大递增子序列