http://acm.hdu.edu.cn/showproblem.php?pid=1025

Constructing Roads In JGShining's Kingdom

Problem Description
 
JGShining's kingdom consists of 2n(n is no more than 500,000) small cities which are located in two parallel lines.
Half of these cities are rich in resource (we call them rich cities) while the others are short of resource (we call them poor cities). Each poor city is short of exactly one kind of resource and also each rich city is rich in exactly one kind of resource. You may assume no two poor cities are short of one same kind of resource and no two rich cities are rich in one same kind of resource. 
With the development of industry, poor cities wanna import resource from rich ones. The roads existed are so small that they're unable to ensure the heavy trucks, so new roads should be built. The poor cities strongly BS each other, so are the rich ones. Poor cities don't wanna build a road with other poor ones, and rich ones also can't abide sharing an end of road with other rich ones. Because of economic benefit, any rich city will be willing to export resource to any poor one.
Rich citis marked from 1 to n are located in Line I and poor ones marked from 1 to n are located in Line II. 
The location of Rich City 1 is on the left of all other cities, Rich City 2 is on the left of all other cities excluding Rich City 1, Rich City 3 is on the right of Rich City 1 and Rich City 2 but on the left of all other cities ... And so as the poor ones. 
But as you know, two crossed roads may cause a lot of traffic accident so JGShining has established a law to forbid constructing crossed roads.
For example, the roads in Figure I are forbidden.In order to build as many roads as possible, the young and handsome king of the kingdom - JGShining needs your help, please help him. ^_^
 
Input
 
Each test case will begin with a line containing an integer n(1 ≤ n ≤ 500,000). Then n lines follow. Each line contains two integers p and r which represents that Poor City p needs to import resources from Rich City r. Process to the end of file.
 
Output
 
For each test case, output the result in the form of sample.  You should tell JGShining what's the maximal number of road(s) can be built. 
 
Sample Input
 
2
1 2
2 1
3
1 2
2 3
3 1
 
Sample Output
 
Case 1:
My king, at most 1 road can be built.
 
Case 2:
My king, at most 2 roads can be built.
 
Hint
 

Huge input, scanf is recommended.

 
题意:输入有两边,为p,q,求的是所有p到q不相交的话最多可以有多少条边相连,那么只要从1到n枚举p,然后road[p]就是一个q,这样求road[p]的LIS就好了。
思路:直接LIS的复杂度为O(n^2),对于500000的数据来说是肯定TLE的,加上二分优化的话复杂度就可以降低到O(nlogn)了。
 #include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
#define N 500005 int dp[N],road[N];
int x; void cal(int a)
{
int l=,r=x,mid;
while(l<=r){
mid=(l+r)>>;
if(dp[mid]<a) l=mid+;
else r=mid-;
}
dp[l]=a;
} int main()
{
int n;
int cas=;
while(~scanf("%d",&n)){
memset(dp,,sizeof(dp));
x=;
for(int i=;i<=n;i++){
int a,b;
scanf("%d%d",&a,&b);
road[a]=b;
}
dp[]=road[];
for(int i=;i<=n;i++){
int a=road[i];
if(a>dp[x]) dp[++x]=a;
else cal(a);
}
printf("Case %d:\n",++cas);
if(x==) printf("My king, at most 1 road can be built.\n\n");
else printf("My king, at most %d roads can be built.\n\n",x);
}
return ;
}

HDU 1025:Constructing Roads In JGShining's Kingdom(LIS+二分优化)的更多相关文章

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

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

  2. 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 ...

  3. [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 ...

  4. 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 ...

  5. 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 ...

  6. HDU 1025 Constructing Roads In JGShining's Kingdom(DP+二分)

    点我看题目 题意 :两条平行线上分别有两种城市的生存,一条线上是贫穷城市,他们每一座城市都刚好只缺乏一种物资,而另一条线上是富有城市,他们每一座城市刚好只富有一种物资,所以要从富有城市出口到贫穷城市, ...

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

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

  8. hdu 1025 Constructing Roads In JGShining’s Kingdom 【dp+二分法】

    主题链接:pid=1025">http://acm.acmcoder.com/showproblem.php?pid=1025 题意:本求最长公共子序列.但数据太多. 转化为求最长不下 ...

  9. hdu 1025 Constructing Roads In JGShining's Kingdom

    本题明白题意以后,就可以看出是让求最长上升子序列,但是不知道最长上升子序列的算法,用了很多YY的方法去做,最后还是超时, 因为普通算法时间复杂度为O(n*2),去搜了题解,学习了一下,感觉不错,拿出来 ...

  10. 最长上升子序列 HDU 1025 Constructing Roads In JGShining's Kingdom

    最长上升子序列o(nlongn)写法 dp[]=a[]; ; ;i<=n;i++){ if(a[i]>dp[len]) dp[++len]=a[i]; ,dp++len,a[i])=a[i ...

随机推荐

  1. TASM 5.0 安装及使用教程

    安装TASM 5.0很简单,您只需要下载本站[相关工具]中的"TASM50.zip"文件,解压后在Windows9x/NT下执行"INSTALL.EXE"即可开 ...

  2. <转>C# 动态创建DataTable

    C# 动态创建DataTable,有时候在做些测试Demo中用来模拟一些数据比较不错.记在这里避免以后重写呵呵... DataTable dt = new DataTable(); dt.Column ...

  3. Apache Cordova介绍

    原文:Apache Cordova介绍 Apache Cordova是一套设备API,允许移动应用的开发者使用JavaScript来访问本地设备的功能,比如摄像头.加速计.它可以与UI框架(如jQue ...

  4. Java发展历程

    Java 的发展要追溯到 1991 年,Patrick Naughton(帕特里克·诺顿)和 James Gosling(詹姆斯·高斯林)带领 Sun 公司的工程师打算为有线电视转换盒之类的消费产品设 ...

  5. Expression Blend学习二UI布局

    什么是布局? · Panels控件(其实就是容器控件) · 对内部的子控件提供了自动布局功能 · 可以在容器控件内继续添加容器控件(一个复杂的界面往往是多种容器控件嵌套而组成的) · 一些界面器控件也 ...

  6. Linux杂谈: 树形显示多级目录--tree

    最近写博客的时候偶尔会需要将文件目录结构直观地列出来,例如python的包结构. 于是在网上搜了搜,发现了一个Linux下还不错的工具--tree tree 可以很直观地显示多级目录结构. 1. 安装 ...

  7. GIS基础软件及操作(十二)

    原文 GIS基础软件及操作(十二) 练习十二. ArcMap制图-地图版面设计 设置地图符号-各种渲染方式的使用 使用ArcMap Layout(布局)界面制作专题地图 将各种地图元素添加到地图版面中 ...

  8. Dec Working Note

    01 新的一个月,也是16年最后一个月,意义非凡. 那么第一天就要来点非凡的意义:提出离职. 纠结了好久,最后还是离职了,感觉是好他妈的爽,纠结什么呢. 不过今天状态不好,最近状态一直不好,上火,也没 ...

  9. Linux中的进程

    进程,线程,程序 通俗的说,进程是程序的一次执行过程,程序是一种静态概念,如果在系统中引入线程,则进程是资源分配单元,线程是系统执行单元.此处不懂应参阅<操作系统> 进程衍生 fork-e ...

  10. Mono 4.0 发布,开源跨平台 .Net 框架

    快速使用Romanysoft LAB的技术实现 HTML 开发Mac OS App,并销售到苹果应用商店中.   <HTML开发Mac OS App 视频教程> 土豆网同步更新:http: ...