#include<iostream>
using namespace std; //BFS+优先队列(打印路径) #define N 500005
int 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);
else
printf("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已经没用了。非常easy理解吧。这时Len=1

接着,d[3] = 5,d[3]>B[1],所以令B[1+1]=B[2]=d[3]=5,就是说长度为2的LIS的最小末尾是5,非常easy理解吧。

这时候B[1..2]
= 1, 5,Len=2

再来。d[4] = 3,它正好加在1,5之间,放在1的位置显然不合适,由于1小于3。长度为1的LIS最小末尾应该是1。这样非常easy推知。长度为2的LIS最小末尾是3。于是能够把5淘汰掉,这时候B[1..2]
= 1, 3,Len = 2

继续,d[5] = 6。它在3后面,由于B[2] = 3, 而6在3后面。于是非常easy能够推知B[3] = 6, 这时B[1..3]
= 1, 3, 6,还是非常easy理解吧? 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&#39;s Kingdom-&gt;二分求解LIS+O(NlogN)的更多相关文章

  1. HDU 1025 Constructing Roads In JGShining&#39;s Kingdom (DP)

    Problem Description JGShining's kingdom consists of 2n(n is no more than 500,000) small cities which ...

  2. hdu1025 Constructing Roads In JGShining&#39;s Kingdom(二分+dp)

    转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1025 Problem ...

  3. hdu1025 Constructing Roads In JGShining&#39;s Kingdom (nlogn的LIS)

    题目链接 第一次写nlogn复杂度的LIS,纪念一下. 题目意思是说.有两条平行线.两条平行线都有n个城市,都是从左到右标记为1--n,一条线上是富有城市,一个是贫穷城市.输入n.接下来有n行,p,r ...

  4. HDOJ(HDU).1025 Constructing Roads In JGShining's Kingdom (DP)

    HDOJ(HDU).1025 Constructing Roads In JGShining's Kingdom (DP) 点我挑战题目 题目分析 题目大意就是给出两两配对的poor city和ric ...

  5. [ACM] hdu 1025 Constructing Roads In JGShining's Kingdom (最长递增子序列,lower_bound使用)

    Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65 ...

  6. HDU 1025 Constructing Roads In JGShining's Kingdom(二维LIS)

    Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65 ...

  7. hdu 1025:Constructing Roads In JGShining's Kingdom(DP + 二分优化)

    Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65 ...

  8. HDU 1025 Constructing Roads In JGShining's Kingdom[动态规划/nlogn求最长非递减子序列]

    Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65 ...

  9. HDU 1025 Constructing Roads In JGShining's Kingdom(求最长上升子序列nlogn算法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1025 解题报告:先把输入按照r从小到大的顺序排个序,然后就转化成了求p的最长上升子序列问题了,当然按p ...

随机推荐

  1. 常用css表达式-最小宽度-上下居中

    /* IE6下最小宽度的CSS表达式 */ width:100%; min-width:1024px; _width:expression((document.documentElement.clie ...

  2. sublime text3-代码片段配置

    1.Tools->New Snippet-> <snippet>     <content><![CDATA[${1:public }function ${2 ...

  3. secureCRT使用小贴士

    (一)使用WIN键盘 在securecrt界面:工具→键映射编辑器,在弹出的键盘中点击“home”,会弹出一个窗口,在“发送字符串”中输入:\033[1~ 另存为securecrt安装根目录下的Key ...

  4. Xcode6插件开发

    工欲善其事必先利其器,Xcode是我们做iOS Dev必须掌握的一款开发工具. Xcode本身也是一门Cocoa程序,与其来说它是一个Cocoa程序,是不是意味着,我们可以去动态去让它做某件事,或者监 ...

  5. 非常好用的正则表达式"\\s+" - 匹配任意空白字符

    说起来,博主使用过的正则场景虽然不多,但是就是在这当中,我发现"\\s+"真好用! 详解 "\\s+" 正则表达式中\s匹配任何空白字符,包括空格.制表符.换页 ...

  6. 【Java】关于并发

    http://www.cnblogs.com/dolphin0520/p/3958019.html http://www.cnblogs.com/yank/p/3955322.html http:// ...

  7. ibatis 中isNull, isNotNull与isEmpty, isNotEmpty区别

    在iBATIS中isNull用于判断参数是否为Null,isNotNull相反 isEmpty判断参数是否为Null或者空,满足其中一个条件则其true isNotEmpty相反,当参数既不为Null ...

  8. 5种php加密工具zendGuard、ionCube、SourceCop、SourceGuardian、phpShield

    PHP做桌面应用的想法: 除去icudt55.dll,PHP7用7ZIP压缩后不足7MB,而PHP自带了SQLite和CLI HTTP Server,用户打开浏览器就能访问PHP开发的桌面应用.如果源 ...

  9. bzoj1212

    trie树最基本的应用了不难得到f[i]=f[j] if (s[j+1~i]∈dictionary);可以用trie树匹配 ..] of boolean; son:..,..] of longint; ...

  10. BZOJ2134: 单选错位

    题目:http://www.lydsy.com/JudgeOnline/problem.php?id=2134 题解:因为每个答案之间是互不影响的,所以我们可以挨个计算. 假设当前在做 i 题目,如果 ...