HDU ACM 1025 Constructing Roads In JGShining's Kingdom->二分求解LIS+O(NlogN)
#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's Kingdom->二分求解LIS+O(NlogN)的更多相关文章
- HDU 1025 Constructing Roads In JGShining's Kingdom (DP)
Problem Description JGShining's kingdom consists of 2n(n is no more than 500,000) small cities which ...
- hdu1025 Constructing Roads In JGShining's Kingdom(二分+dp)
转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1025 Problem ...
- hdu1025 Constructing Roads In JGShining's Kingdom (nlogn的LIS)
题目链接 第一次写nlogn复杂度的LIS,纪念一下. 题目意思是说.有两条平行线.两条平行线都有n个城市,都是从左到右标记为1--n,一条线上是富有城市,一个是贫穷城市.输入n.接下来有n行,p,r ...
- HDOJ(HDU).1025 Constructing Roads In JGShining's Kingdom (DP)
HDOJ(HDU).1025 Constructing Roads In JGShining's Kingdom (DP) 点我挑战题目 题目分析 题目大意就是给出两两配对的poor city和ric ...
- [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 ...
- 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 ...
- 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 ...
- 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 ...
- HDU 1025 Constructing Roads In JGShining's Kingdom(求最长上升子序列nlogn算法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1025 解题报告:先把输入按照r从小到大的顺序排个序,然后就转化成了求p的最长上升子序列问题了,当然按p ...
随机推荐
- 学习Swift -- 协议(上)
协议(上) 协议是Swift非常重要的部分,协议规定了用来实现某一特定工作或者功能所必需的方法和属性.类,结构体或枚举类型都可以遵循协议,并提供具体实现来完成协议定义的方法和功能.任意能够满足协议要求 ...
- LeetCode(4) || Longest Palindromic Substring 与 Manacher 线性算法
LeetCode(4) || Longest Palindromic Substring 与 Manacher 线性算法 题记 本文是LeetCode题库的第五题,没想到做这些题的速度会这么慢,工作之 ...
- uva 1428 - Ping pong
树状数组,把他们的技能值作为轴: 首先按照编号从小到大插入值,这样就可以得到,技能值比当前小的人数: 然后按照编号从大到小再插一遍: 代码: #include<cstdio> #inclu ...
- 查看SQL Server数据库中各个表和视图的索引所占的空间大小
;with cte as ( (select t.name as TableName,i.name as IndexName, sum(row_count)as row_count, SUM (s.u ...
- C++中结构体与类的区别(结构不能被继承,默认是public,在堆栈中创建,是值类型,而类是引用类型)good
结构是一种用关键字struct声明的自定义数据类型.与类相似,也可以包含构造函数,常数,字段,方法,属性,索引器,运算符和嵌套类型等,不过,结构是值类型. 1.结构的构造函数和类的构造函数不同. a. ...
- android中保存一个ArrayList到SharedPreferences的方法
保存: public static boolean saveArray() { SharedPrefernces sp=SharedPrefernces.getDefaultSharedPrefern ...
- LeetCode 面试:Add Two Numbers
1 题目 You are given two linked lists representing two non-negative numbers. The digits are stored in ...
- 【POJ】1056 IMMEDIATE DECODABILITY
字典树水题. #include <cstdio> #include <cstring> #include <cstdlib> typedef struct Trie ...
- [LeetCode#212]Word Search II
Problem: Given a 2D board and a list of words from the dictionary, find all words in the board. Each ...
- NOIP2014酱油记
尘埃落定,来补一下酱油记吧... day-1 晚上老师说有xyz的noip模拟赛,于是果断请假来做(shou)题(nve),题目真是理(S)性(X)愉(B)悦(K),然后就爆零了!感觉noip要爆零滚 ...