题目链接:

Constructing Roads In JGShining's Kingdom

Time Limit: 2000/1000 MS (Java/Others)    

Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 21045    Accepted Submission(s): 5950

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.

题意:给你两个条线上一对一对的点,要把这一对一对的点连接起来,且连线不能相交,问最多有多少条线;

思路:按其中的一组数的大小排序,然后另一列数的最长上升子序列的长度就是答案,然后用一个O(n*logn)的算法找到这个长度,O(n^2)的方法会超时;

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <stack>
#include <algorithm>
using namespace std;
const int N=5e5+;
int ans[N];
struct node
{
int a,b;
};
node x[N];
int cmp(node c,node d)
{
// if(c.a==d.a)return c.b<d.b;
return c.a<d.a;
}
stack<int>s;
int main()
{
int n;
int cnt=;
while(scanf("%d",&n)!=EOF)
{
for(int i=;i<=n;i++)
{
scanf("%d%d",&x[i].a,&x[i].b);
}
sort(x+,x+n+,cmp);
int num=;
ans[]=x[].b;
for(int j=;j<=n;j++)
{
if(x[j].b>ans[num])
{
num++;
ans[num]=x[j].b;
}
else
{
int pos=upper_bound(ans+,ans+num+,x[j].b)-ans;
ans[pos]=x[j].b;
} }
if(num==) printf("Case %d:\nMy king, at most %d road can be built.\n\n",cnt++,num);
else printf("Case %d:\nMy king, at most %d roads can be built.\n\n",cnt++,num);
}
return ;
}

hdu-1025 Constructing Roads In JGShining's Kingdom(二分查找)的更多相关文章

  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),去搜了题解,学习了一下,感觉不错,拿出来 ...

随机推荐

  1. 关于使用eclipse开发最小运行组件包

    有的时候向用eclipse组件,但是其中好多东西是相互关联的,如果在eclipse上做二次开发固然可以,但是有的时候想要的只不过是一个可以运行的架包而已,所以不必要那么多东西. 下面是我使用eclip ...

  2. iptables启动脚本分析

    #!/bin/sh # # iptables Start iptables firewall # # chkconfig: 2345 08 92 # description: Starts, stop ...

  3. unity绝对路径与相对路径转化

    绝对路径->相对路径 string mp =“H:\unity(project)\New Unity Project\Assets\111.mat”; mp = mp.Substring(mp. ...

  4. 数据库导入Excel-从基础做起

    近期一直跟着师傅做考试系统的基础.每天与大量的数据打交道.数据的导入.数据的导出.视图的导入导出.核对信息等等,收获挺多的,培养了自己的耐心和细心,也进一步了解了数据库. 一切从基础做起! 来看看近期 ...

  5. vs重复编译

    VS用了这么久都没有这样的问题,昨天突然发现在自己电脑时间不对了,就调了下,以后这问题都来了.每次运行项目都要重新编译下,不管改不改底层代码.这让我很痛苦,浪费大量时间,找了好久才得到答案: .时间问 ...

  6. Python闲谈(二)聊聊最小二乘法以及leastsq函数

    1 最小二乘法概述 自从开始做毕设以来,发现自己无时无刻不在接触最小二乘法.从求解线性透视图中的消失点,m元n次函数的拟合,包括后来学到的神经网络,其思想归根结底全都是最小二乘法. 1-1 “多线→一 ...

  7. H - Funny Car Racing

    H - Funny Car Racing Time Limit:1000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Desc ...

  8. 【HTML5开发系列】CSS3

    选择器 属性 背景和边框 盒模型 布局 文本 动画 其他

  9. Docker学习总结之docker创建私有仓库(private Repositories)

    Docker 创建 Private Repositories 前言 基于GFW的缘故,国内大陆基本无法pull国外的镜像,更别说官方的index了.如果images无法pull下来,那么docker就 ...

  10. eclipse 安装 json Editor Plugin的方法

    json Editor Plugin是一款可以显示JSON高亮语法,折叠的eclipse插件.但目前网上的安装方法少,且几乎都无效.我按照官网的步骤安装很容易就成功了,现在贴出步骤供大家参考: 1.在 ...