HDU ACM 1025 Constructing Roads In JGShining's Kingdom->二分求解LIS+O(NlogN)
来源:互联网 发布:美国国父路易十六 知乎 编辑:程序博客网 时间:2024/06/18 11:43
#include<iostream>using namespace std; //BFS+优先队列(打印路径)#define N 500005int c[N];int dp[N]; //dp[i]保存的是长度为i的最长不降子序列的最小尾元素int BS(int n,int x) //二分查找下标,当x比所有元素小时下标为1,比所有元素大时下标为n+1.{int low,high,mid;low=1,high=n;while(low<=high){mid=(low+high)>>1;if(dp[mid]==x) return mid;else if(dp[mid]>x) high=mid-1;else if(dp[mid]<x) low=mid+1;}return low;}int main(){int t,n,a,b,i,len,pos;t=0;while(scanf("%d",&n)==1){for(i=1;i<=n;i++)c[scanf("%d%d",&a,&b),a]=b; //括号先算dp[0]=-1;dp[1]=c[1];len=1;for(i=1;i<=n;i++){pos=BS(len,c[i]);dp[pos]=c[i];if(pos>len) len++;}printf("Case %d:\n",++t);if(len==1)printf("My king, at most %d road can be built.\n\n",len);elseprintf("My king, at most %d roads can be built.\n\n",len);} return 0;}参考自:http://blog.csdn.net/ice_crazy/article/details/7536332
假设存在一个序列d[1..9] = 2 1 5 3 6 4 8 9 7,可以看出来它的LIS长度为5。
下面一步一步试着找出它。
我们定义一个序列B,然后令 i = 1 to 9 逐个考察这个序列。
此外,我们用一个变量Len来记录现在最长算到多少了
首先,把d[1]有序地放到B里,令B[1] = 2,就是说当只有1一个数字2的时候,长度为1的LIS的最小末尾是2。这时Len=1
然后,把d[2]有序地放到B里,令B[1] = 1,就是说长度为1的LIS的最小末尾是1,d[1]=2已经没用了,很容易理解吧。这时Len=1
接着,d[3] = 5,d[3]>B[1],所以令B[1+1]=B[2]=d[3]=5,就是说长度为2的LIS的最小末尾是5,很容易理解吧。这时候B[1..2] = 1, 5,Len=2
再来,d[4] = 3,它正好加在1,5之间,放在1的位置显然不合适,因为1小于3,长度为1的LIS最小末尾应该是1,这样很容易推知,长度为2的LIS最小末尾是3,于是可以把5淘汰掉,这时候B[1..2] = 1, 3,Len = 2
继续,d[5] = 6,它在3后面,因为B[2] = 3, 而6在3后面,于是很容易可以推知B[3] = 6, 这时B[1..3] = 1, 3, 6,还是很容易理解吧? Len = 3 了噢。
第6个, d[6] = 4,你看它在3和6之间,于是我们就可以把6替换掉,得到B[3] = 4。B[1..3] = 1, 3, 4, Len继续等于3
第7个, d[7] = 8,它很大,比4大,嗯。于是B[4] = 8。Len变成4了
第8个, d[8] = 9,得到B[5] = 9,嗯。Len继续增大,到5了。
最后一个, d[9] = 7,它在B[3] = 4和B[4] = 8之间,所以我们知道,最新的B[4] =7,B[1..5] = 1, 3, 4, 7, 9,Len = 5。
于是我们知道了LIS的长度为5。
!!!!! 注意。这个1,3,4,7,9不是LIS,它只是存储的对应长度LIS的最小末尾。有了这个末尾,我们就可以一个一个地插入数据。虽然最后一个d[9] = 7更新进去对于这组数据没有什么意义,但是如果后面再出现两个数字 8 和 9,那么就可以把8更新到d[5], 9更新到d[6],得出LIS的长度为6。
然后应该发现一件事情了:在B中插入数据是有序的,而且是进行替换而不需要挪动——也就是说,我们可以使用二分查找,将每一个数字的插入时间优化到O(logN)~~~~~于是算法的时间复杂度就降低到了O(NlogN)~
- HDU ACM 1025 Constructing Roads In JGShining's Kingdom->二分求解LIS+O(NlogN)
- HDU 1025 Constructing Roads In JGShining's Kingdom(LIS nlogn)
- HDU 1025 Constructing Roads In JGShining's Kingdom(LIS的O(nlogn)算法)
- HDU-1025-Constructing Roads In JGShining's Kingdom【LIS】【二分】
- HDOJ 1025 Constructing Roads In JGShining's Kingdom(LIS O(nlogn) )
- hdu 1025 Constructing Roads In JGShining's Kingdom(最长上升子序列(LIS)O(nlogn)算法)
- HDU 1025 Constructing Roads In JGShining's Kingdom (DP----LIS 的nlogn算法)
- hdu 1025Constructing Roads In JGShining's Kingdom(LIS)
- hdu 1025 Constructing Roads In JGShining's Kingdom(LIS)
- hdu 1025 Constructing Roads In JGShining's Kingdom (LIS)
- hdu 1025 Constructing Roads In JGShining's Kingdom (LIS)
- HDU 1025 Constructing Roads In JGShining's Kingdom LIS题解
- 【DP|LIS】HDU-1025 Constructing Roads In JGShining's Kingdom
- HDU 1025 Constructing Roads In JGShining's Kingdom(LIS)
- hdu 1025 Constructing Roads In JGShining's Kingdom(lis)
- HDU Problem 1025 Constructing Roads In JGShining's Kingdom 【LIS】
- HDU 1025Constructing Roads In JGShining's Kingdom(LIS)
- HDU 1025 Constructing Roads In JGShining's Kingdom(LIS)
- MyBatis学习总结(六)——调用存储过程
- Codeforces Round #306 (Div. 2) E - Brackets in Implications 构造
- hadoop的关键进程
- 线程简介
- android项目导入后,遇到gen already exists but is not a source folder. Convert to a source folder or rename
- HDU ACM 1025 Constructing Roads In JGShining's Kingdom->二分求解LIS+O(NlogN)
- java中基础技术学习笔记
- ListView流畅
- 浅析C++中this指针
- ArcGIS操作——object name is not a regular identifier for underlaying DBMS[ArcGIS企业级数据导入错误]
- 接口和抽象类的区别
- sqoop 1.4.4-cdh5.1.2快速入门
- Codeforces #306 (div2)
- 走马灯效果。注意需要获取焦点。