hdu1025 Constructing Roads In JGShining's Kingdom (nlogn的LIS)
第一次写nlogn复杂度的LIS,纪念一下。
题目意思是说。有两条平行线。两条平行线都有n个城市,都是从左到右标记为1--n,一条线上是富有城市,一个是贫穷城市。输入n。接下来有n行,p,r表示穷城市p和富有城市r
之间能够建一条路(p的顺序是1--n,一个贫穷城市仅仅相应一个富有城市(弱爆的语文描写叙述能力T_T)),公路不能交叉。
问最多能够建多少条公路。
在别处看到的对nlogn解法的解释吧算是:
时间复杂度:(NlogN):
除了算法一的定义之外,添加一个数组b,b[i]用以表示长度为i最长子序列的最后一个数最小能够是多少。易证:i<j时,b[i]<b[j]。
在二分查找时,一直更新b[]内容,设此时b的总长度为k,
若1. arr[i] >= b[k], 则b[k+1] = arr[i];
若2. arr[i] < b[k], 则在b[1..k]中用二分搜索大于arr[i]的最小值。返回其位置pos,然后更新b[pos]=arr[i]。
code例如以下(自己手写了一个二分,又用了下STL里面upper_bound,都能够):
//#pragma comment(linker,"/STACK:102400000,102400000")
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
//#define local
using namespace std;
const int maxn=500010;
int poor[maxn],ro[maxn];
int n;
int Binary_search(int x,int k)
{
int low=1,high=k;
while(low<=high)
{
int mid=(low+high)/2;
if(ro[mid]<=x)
low=mid+1;
else
high=mid-1;
}
return low;
}
int lis()
{
int k=1;
ro[k]=poor[1];
for(int i=2;i<=n;i++)
{
if(poor[i]>=ro[k])
ro[++k]=poor[i];
else
{
//int pos=Binary_search(poor[i],k);
int pos=upper_bound(ro+1,ro+k+1,poor[i])-(ro);
ro[pos]=poor[i];
}
}
return k;
}
int main()
{
#ifdef local
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif // local
int cnt=0;
while(~scanf("%d",&n))
{
int x,y;
for(int i=0;i<n;i++)
{
scanf("%d%d",&x,&y);
poor[x]=y;
}
int ans=lis();
printf("Case %d:\n",++cnt);
if(ans<=1)
printf("My king, at most %d road can be built.\n\n",ans);
else
printf("My king, at most %d roads can be built.\n\n",ans);
}
return 0;
}
hdu1025 Constructing Roads In JGShining's Kingdom (nlogn的LIS)的更多相关文章
- hdu1025 Constructing Roads In JGShining's Kingdom(二分+dp)
转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1025 Problem ...
- 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 ...
- 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]; ...
- hdu-1025 Constructing Roads In JGShining's Kingdom(二分查找)
题目链接: Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others) Memory Li ...
- DP 60题 -2 HDU1025 Constructing Roads In JGShining's Kingdom
Problem Description JGShining's kingdom consists of 2n(n is no more than 500,000) small cities which ...
- LIS问题---HDU1025 Constructing Roads In JGShining's Kingdom
发现这个说的比较通俗: 假设存在一个序列d[1..9] = 2 1 5 3 6 4 8 9 7,可以看出来它的LIS长度为5.下面一步一步试着找出它.我们定义一个序列B,然后令 i = 1 to 9 ...
- Constructing Roads In JGShining's Kingdom(HDU1025)(LCS序列的变行)
Constructing Roads In JGShining's Kingdom HDU1025 题目主要理解要用LCS进行求解! 并且一般的求法会超时!!要用二分!!! 最后蛋疼的是输出格式的注 ...
- [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 ...
随机推荐
- SetWindowLong
SetWindowLong函数介绍 收藏 SetWindowLong函数介绍 SetWindowLong Unicode 函数原型 LONG SetWindowLong(hwnd,nIndex,lNe ...
- JavaScript快速入门(四)——JavaScript函数
函数声明 之前说的三种函数声明中(参见JavaScript快速入门(二)——JavaScript变量),使用Function构造函数的声明方法比较少见,我们暂时不提.function func() { ...
- FOJ 1607 Greedy division 数学题
题目地址: http://acm.fzu.edu.cn/problem.php?pid=1607 给定一个n,将n平均分成m份,问有几种方法,每种方法中找出最大的数.思路:就是求n的因子数.先将每个数 ...
- DotNetBar.Bar控制Y顺序控制方向
DotNetBar.Bar控件Y方向上的顺序控制 老帅 控件DevComponents.DotNetBar.Bar是能够有多种用途的.能够作为容器,也能够作为工具条,不管做什么,在Y方向上 ...
- 打印org.eclipse.xsd.XSDSchema对象
由于网上关于Eclipse XSD的中文资料比較少,可是有的时候.我们须要使用Eclipse XSD的API去构造或者改动一个XSD文件. 那么当我们创建了org.eclipse.xsd.XSDSch ...
- java中遍历MAP,嵌套map的几种方法
java中遍历MAP的几种方法 Map<String,String> map=new HashMap<String,String>(); map.put("us ...
- LV在系统重启后不能自动激活(boot.lvm&after.loca)
同类相关文章:http://blog.csdn.net/laven54/article/details/9121661 最近发现suse11sp2的系统解决了异常死机的问题之后,又引入了另外的问题,比 ...
- Loser tree in Python | Christan Christens
Loser tree in Python | Christan Christens Loser tree in Python I am taking an Advanced Data Structur ...
- cocos2dx游戏开发学习笔记3-lua面向对象分析
在lua中,能够通过元表来实现类.对象.继承等.与元表相关的方法有setmetatable().__index.getmetatable().__newindex. 详细什么是元表在这里就不细说了,网 ...
- achieve aop through xml
The main way to achive AOP is deploying a xml file. Now a xml file is presented to be a explanation ...