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 ...
随机推荐
- Transformations 方块转换
题目是中文题,就不做什么解释了,纯模拟题,主要要搞清楚这几种装换方式下标的变化: 第一种:顺时针旋转90度: c[j][n-i+1]=a[i][j]; 第二种:旋转180度: c[n-i+1][n-j ...
- Hadoop--序列化
序列化: 对象的序列化用于将一个对象编码成字节流,以及从字节流中重新构建对象. 将一个对象编码成一个字节流称为序列化该对象. 序列化三种主要的用途: 1.作为一种持久化格式. 2.作为一种通信的数据格 ...
- Mahout-Pearson correlation的实现
计算公式: 并通过以下代码对Mahout in Action的结果进行了验证: 代码例如以下: ` package com.example.mahout; public class TestColl ...
- Oracle rank和dense_rank排名函数
1.rank函数 rank计算一组值的排名,返回数字类型.排名可能是不连续.如果有5人,其中有2个人排名第一,则rank返回的排名结果为:1 1 3 4 5. 作为一个聚合函数,返回虚拟行在样表中的排 ...
- Delphi/C#之父首次访华:55岁了 每天都写代码
Delphi.C#之父Anders Hejlsberg 近日首次访华,并在10月24日和27日参加了两场见面会,分享了他目前领导开发的TypeScript项目,并与国内前端开发者近距离交流.本文就为读 ...
- Bootstrap,Foundation和TypeScript
http://www.oschina.net/question/12_128155 http://www.oschina.net/news/72330/typescript-2-0 给自己提个醒,随时 ...
- jquery easyui Accordion的使用
<html> <head> <script src="jquery-easyui/jquery.min.js"></script> ...
- myBatis 基础测试 表关联关系配置 集合 测试
myBatis 基础测试 表关联关系配置 集合 测试 测试myelipse项目源码 sql 下载 http://download.csdn.net/detail/liangrui1988/599388 ...
- Php 解析XML文件
Php 解析XML文件 Php 解析XML文件,仅供学习參考!演示样例代码例如以下: <?php header("Content-type: text/html; charset=ut ...
- drupal THEME主要文件
**.info 文件** .info 文件是一个必需的文件:Drupal 必须包括它,才干看到主题. .info 文件告诉 Drupal 主题的内部名称.比如,假设这个文件的名称是 ibmtheme. ...