Constructing Roads In JGShining's Kingdom

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 21002    Accepted Submission(s): 5935

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.

手写二分搜索时经常陷入无限循环,这里是在数组中查找大于等于value的最小元素下标。
    这里注意一下模板写法。数组下表从a到b时,l=a-1,r=b+1,while(r-l>1)
 #include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;
#define Max 500005
int dp[Max];
struct node
{
int p,r;
}a[Max];
bool cmp(node x,node y)
{
return x.p<=y.p;
}
int main()
{
int i,j;
int n;
int t=;
freopen("in.txt","r",stdin);
while(scanf("%d",&n)!=EOF)
{
int maxn=;
for(i=;i<n;i++)
scanf("%d%d",&a[i].p,&a[i].r);
sort(a,a+n,cmp);
for(i=;i<n;i++)
dp[i]=Max;
/*for(i=0;i<n;i++)
*lower_bound(dp,dp+n,a[i].r)=a[i].r;
int p=lower_bound(dp,dp+n,Max)-dp;
if(p==1)
printf("Case %d:\nMy king, at most %d road can be built.\n\n",t++,1);
else
printf("Case %d:\nMy king, at most %d roads can be built.\n\n",t++,p);*/
for(i=;i<n;i++)
{ int l=-,r=n,m;
while(r-l>)
{
m=(l+r)/;
if(dp[m]>=a[i].r)
r=m;
else
l=m;
}
dp[r]=a[i].r;
}
int l=-,r=n,m;
while(r-l>)
{
m=(l+r)/;
if(dp[m]>=Max)
r=m;
else
l=m;
}
int p=r;
if(p==)
printf("Case %d:\nMy king, at most %d road can be built.\n\n",t++,);
else
printf("Case %d:\nMy king, at most %d roads can be built.\n\n",t++,p);
}
return ;
}

Constructing Roads In JGShining's Kingdom(HDU 1025 LIS nlogn方法)的更多相关文章

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

  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. HDOJ(HDU).1025 Constructing Roads In JGShining's Kingdom (DP)

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

  4. HDU 1025:Constructing Roads In JGShining's Kingdom(LIS+二分优化)

    http://acm.hdu.edu.cn/showproblem.php?pid=1025 Constructing Roads In JGShining's Kingdom Problem Des ...

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

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

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

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

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

  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. Ubuntu12.04下载Android4.0.1源码全过程,附若干问题解决[转]

    学校里一直在做应用层开发,考虑到日后就业问题,这次决定研究源码和驱动,并进行编译.没想到就下载源码这一步折腾了我整整两天,期间遇到很多问题,哎,记录于此,希望日后再下源码的人不要再走无谓的弯路了.事实 ...

  2. 局域网架个YUM源-HTTP的

    在安装CDH时,这是个绕不过去的坎. 参考URL: http://www.21ops.com/linux/26465.html 奇怪的是,我并没执行creatrepo这个,直接将光盘MOUNT来用的. ...

  3. VS2010编译Qt程序失败------error LNK1123: 转换到 COFF 期间失败:

    error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏使用VS2010编译VC++项目的时候可能会出这个问题. 据说升级到SP1后可能问题解决,但是下载量太大,目前没有得到证实. ...

  4. Eclipse无法打开“Failed to load the JNI shared library”

    解决方案一 这是因为JDK配置错误所导致的现象. 一般说来,新购笔记本会预装64位的windows系统,而在网上下载软件时,32位会优先出现在页面中(现在来说是这个情况,但我认为未来64位会越来越普及 ...

  5. cf493D Vasya and Chess

    D. Vasya and Chess time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  6. Codeforce 220 div2

    D 插入: 在当前指针位置sz处插入一个1,col[sz]记录插入的内容,sz++; 删除i: 找到第i个1的位置,赋为0; 于是转化为一个维护区间和的问题; trick: 如果是依次删除a[0],a ...

  7. css中的段落样式及背景

    一.段落样式 css中关于段落的样式主要有行高,缩进,段落对齐,文字间距,文字溢出,段落换行等.它们的具体语法如下: line-height : normal | length text-indent ...

  8. 【git 问题小说说】 git add时候报错:LF will be replaced by CRLF

    本文来自:http://blog.csdn.net/loovejava/article/details/22114477 最近工作在window平台,不怎么使用命令行了所以导致很多命令都不熟悉啦 哈哈 ...

  9. Could not load the Tomcat server configuration at /Servers/Tomcat v7.0 Server at localhost-config.

    [问题] Eclipse[Ubuntu14.04]中启动Tomcat Server[7.0.55]的时候出现错误例如以下: [解决方法] 1.将下边的Servers中的server[Tomcat v7 ...

  10. android 4.0之前版本号出现JSONException异常

    今天在调试解析server传过来的JSON数据时,在2.3.7的手机上报了以下这样一个异常. 08-07 22:00:29.597: W/System.err(7610): org.json.JSON ...