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

题目:

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.

思路:dp题+二分查找。。。依旧没独立做出来,好惨,最近几个dp题全挂==

  题意是找最长上升子序列,

  懒得写题解了,不是自己想出来的,上一篇写的比较好的题解:http://blog.csdn.net/dangwenliang/article/details/5728363

  这题有两种算法n^2和nlogn的,这题用前一个果断会超时。。。

  在二分法上卡半天,我也是服了我的智商。。

  

 #include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <queue>
#include <stack>
#include <map>
#include <vector> #define PI acos((double)-1)
#define E exp(double(1))
using namespace std; #define N 500009
struct point
{
int x,y;
}; struct point city[N];
int a[N]; bool cmp(point &a,point &b)
{
return a.x<b.x;
} int found(int x,int len)
{
int l,r,mid;
l = ;
r = len;
mid = (l+r)/;
while(l <= r)
{
if(a[mid] < x)
{
l = mid+; }
else if(a[mid] > x)
{
r = mid-;
}
mid = (l+r)/;
}
return l;
}
int main (void)
{
int n,k=;
while(scanf("%d",&n) == )
{
int len;
k++;
for(int i = ;i<=n;i++)
{
scanf("%d%d",&city[i].x,&city[i].y);
a[i]= N + ;
}
sort(city+,city+n+,cmp);
a[] = -;
a[] = city[].y;
len = ;
for(int i = ;i<=n;i++)
{
int j = found(city[i].y,len);
a[j] = city[i].y;
if(len < j)
len = j;
}
if(len == )
printf("Case %d:\nMy king, at most %d road can be built.\n\n",k,len);
else printf("Case %d:\nMy king, at most %d roads can be built.\n\n",k,len);
}
return ;
}

杭电1025Constructing Roads In JGShining's Kingdom的更多相关文章

  1. HDU 1025-Constructing Roads In JGShining's Kingdom(最长不降子序列,线段树优化)

    分析: 最长不降子序列,n很大o(n^2)肯定超,想到了小明序列那个题用线段树维护前面的最大值即可 该题也可用二分搜索来做. 注意问题输出时的坑,路复数后加s #include <map> ...

  2. Constructing Roads In JGShining's Kingdom(HDU1025)(LCS序列的变行)

    Constructing Roads In JGShining's Kingdom  HDU1025 题目主要理解要用LCS进行求解! 并且一般的求法会超时!!要用二分!!! 最后蛋疼的是输出格式的注 ...

  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(二维LIS)

    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(dp/LIS+二分)

    Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65 ...

  6. Constructing Roads In JGShining's Kingdom(HDU 1025 LIS nlogn方法)

    Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65 ...

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

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

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

  9. hdu-1025 Constructing Roads In JGShining's Kingdom(二分查找)

    题目链接: Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others)     Memory Li ...

随机推荐

  1. 第二百四十二节,Bootstrap列表组面板和嵌入组件

    Bootstrap列表组面板和嵌入组件 学习要点: 1.列表组组件 2.面板组件 3.响应式嵌入组件 本节课我们主要学习一下 Bootstrap 的三个组件功能:列表组组件.面板组件. 响应 式嵌入组 ...

  2. MVC模式中M,V,C每个代表意义,并简述在Struts中MVC的表现方式。

    解答: MVC是Model-View-Controller 的缩写,Model代表的是应用的业务逻辑(通过JavaBean,EJB组件实现),View 是应用的表示层(由JSP页面产生)Control ...

  3. Visual Studio 2010自动添加头部注释信息

    在日常的开发中我们经常需要为我们的类库添加注释和版权等信息,这样我们就需要每次去拷贝粘贴同样的文字,为了减少这种重复性的工作,我们可以把这些信息保存在Visual Studio 2010类库模版文件里 ...

  4. 模式识别之Shape Context---利用Shape Context进行形状识别

     什么是Shape Context Shape Context是一个用于形状识别的,非常经典的特征(一串便于计算机处理的数字)提取方法,它由Serge Belongie和Jitendra Malik  ...

  5. 【vijos】1790 拓扑编号(拓扑+贪心)

    https://vijos.org/p/1790 好神的贪心题.. 一开始我也想到了贪心,但是是错的..sad 就是因为每一个节点的编号与逆图的子树有关,且编号一定是>=子树的儿子+1的.但是想 ...

  6. hdu 3681(bfs+二分+状压dp判断)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3681 思路:机器人从出发点出发要求走过所有的Y,因为点很少,所以就能想到经典的TSP问题.首先bfs预 ...

  7. Android版微信小代码(转)

    以下代码仅适用于Android版微信: //switchtabpos:让微信tab更贴合Android Design 如果你并不喜欢微信Android版和iOS端同用一套UI,现在有一个小方法可以实现 ...

  8. 使用c++为node.js扩展模块

    官方文档 编写c++代码 // demo.cc #include <node.h> using v8::FunctionCallbackInfo; using v8::Isolate; u ...

  9. iOS应用开发最佳实践:编写高质量的Objective-C代码

    本文转载至 http://www.cocoachina.com/industry/20131129/7445.html 点标记语法 属性和幂等方法(多次调用和一次调用返回的结果相同)使用点标记语法访问 ...

  10. 【BZOJ5060】魔方国 特判

    [BZOJ5060]魔方国 Description 小奇和魔法猪打开了战狂的遗迹,穿越到了东元20年.东元元年,战狂率领一千万士兵毁灭了一个又一个文明,并建立起了新文明——昌和帝国,招募了八位伟人:大 ...