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. Bootstrap 图片形状

    @{    Layout = null;}<!DOCTYPE html><html><head>    <meta name="viewport&q ...

  2. VisualSVN5.1.7补丁原创发布

    VisualSVN5.1.7补丁原创发布 一切尽在发布中.

  3. [WPF疑难] 模式窗口被隐藏后重新显示时变成了非模式窗口

    原文:[WPF疑难] 模式窗口被隐藏后重新显示时变成了非模式窗口 [WPF疑难] 模式窗口被隐藏后重新显示时变成了非模式窗口 周银辉 现象: 大家可以试试下面这个很有趣但会带来Defect的现象:当我 ...

  4. Win8Metro(C#)数字图像处理--2.21二值图像腐蚀

    原文:Win8Metro(C#)数字图像处理--2.21二值图像腐蚀  [函数名称] 二值图像腐蚀函数CorrosionProcess(WriteableBitmap src) [算法说明] 二值 ...

  5. Linux ACL对某一些文件有管理权限

    某些系统账号希望对某一些文件有管理权限,有三种方法: 1 加入属主所在的同一个组中,这等于扩大了访问其他文件的权限了. 2 加入other中,这样权限放开的更大了. 3 给文件的sudo权限. 4 采 ...

  6. ubuntu 16.04 android studio 开发环境搭建

    安装步骤: 1. 安装 Java developer kit 2.安装 Android developer kit 3.安装 Android studio 4.真机调试 第一次用Linux,命令基本不 ...

  7. ORACLE 11.2.0.4 Single To Single Data Guard 安装 physical standby

    [root@ORACLE ~]# su - oracle [oracle@ORACLE ~]$ sqlplus / as sysdba . 查看主库归档模式: SQL> select log_m ...

  8. 分布式文件系统之MooseFS----介绍

    前一段公司因为nfs存储的问题,做了一段时间的调研.最终虽然有nfs高可用方案(nfs+drbd+heartbeat),但是在nfs故障切换的时候会出现 2 分钟左右的延时.这样子...就开始了对分布 ...

  9. C++数组指针、指针数组、函数指针的核心概念

    1.什么叫数组指针? 数组指针:一个指向一维或者多维数组的指针. 比如:int * b=new int[10];指向一维数组的指针b ; 注意,这个时候释放空间一定要delete [] ,否则会造成内 ...

  10. 《HTML开发Mac OS App 视频教程》 第001讲、入门教程

    土豆网同步更新:http://www.tudou.com/plcover/VHNh6ZopQ4E/   使用HTML 创建Mac OS App 视频教程. 官方QQ群: (1)App实践出真知 434 ...