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 ...
随机推荐
- TEXT文本编辑框3 点击按钮添加文本至文本输入框
In this exercise a function that loads the texts of an internal table into the text window, is imple ...
- Android菜鸟的成长笔记(8)——Intent与Intent Filter(上)
原文:[置顶] Android菜鸟的成长笔记(8)——Intent与Intent Filter(上) Intent代表了Android应用的启动“意图”,Android应用将会根据Intent来启动指 ...
- 存储的几个LUN问题
存储的几个LUN问题 . ---整理自EMC论坛 1. Linux中如何识别LUN?(AIX是否也差不多) 当创建好LUN并建好storage group后,主机(linux)可以直接用fdisk - ...
- 启动网页时候自己主动载入servlet假设不使用strus最经常使用的两种方式
这是第一种使用的是onload方法当中的test是自己的servlet <html> <body onload = "test"> </body> ...
- Swift - 判断设备类型开发兼容的iOS应用(iPad使用分隔视图控制器)
1,分割视图控制器(UISplitViewController) 在iPhone应用中,使用导航控制器由上一层界面进入下一层界面. 但iPad屏幕较大,通常使用SplitViewController来 ...
- JQuery选择器操作
!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"><head runat="se ...
- Hibernate(六)——多对多关联映射
前面几篇文章已经较讲解了三大种关联映射,多对多映射就非常简单了,不过出于对关联映射完整性的考虑,本文还是会简要介绍下多对多关联映射. 1.单向多对多关联映射 情景:一个用户可以有多个角色,比如数据录入 ...
- ZeroClipboard插件:兼容各浏览器网页复制功能
常规利用JS编写的网页复制功能只对IE有效,无法做到兼容其它浏览器,代码如下: function copyToClipBoard(){ var clipBoardContent="" ...
- webmagic加上了注解支持
今天有个网友在博客回帖,能不能用注解来写一个爬虫?想了想,因为Javaer总习惯结果有个对象Model(我在自己用的时候也是这样),ResultItems的key-value形式难免会有点麻烦,何不将 ...
- C++ STL中Map的相关排序操作:按Key排序和按Value排序 - 编程小径 - 博客频道 - CSDN.NET
C++ STL中Map的相关排序操作:按Key排序和按Value排序 - 编程小径 - 博客频道 - CSDN.NET C++ STL中Map的相关排序操作:按Key排序和按Value排序 分类: C ...