Constructing Roads In JGShining's Kingdom

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

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.
 
思路:最长上升子序列+二分。
注意:可建造的road大于1是要输出‘s’。
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXN=;
const int INF=0x3f3f3f3f;
struct Node{
int rich,poor;
}road[MAXN];
int n;
int dp[MAXN];
bool comp(const Node &no1,const Node &no2)
{
return no1.poor < no2.poor;
}
int main()
{
int cas=;
while(scanf("%d",&n)!=EOF)
{
fill(dp,dp+MAXN,INF);
for(int i=;i<n;i++)
{
scanf("%d%d",&road[i].poor,&road[i].rich);
}
sort(road,road+n,comp);
for(int i=;i<n;i++)
{
*lower_bound(dp,dp+n,road[i].rich)=road[i].rich;
}
int res=lower_bound(dp,dp+n,INF)-dp;
printf("Case %d:\n",++cas);
printf("My king, at most %d %s can be built.\n\n",res,res>?"roads":"road");
}
return ;
}

HDOJ1025(最长上升子序列)的更多相关文章

  1. 用python实现最长公共子序列算法(找到所有最长公共子串)

    软件安全的一个小实验,正好复习一下LCS的写法. 实现LCS的算法和算法导论上的方式基本一致,都是先建好两个表,一个存储在(i,j)处当前最长公共子序列长度,另一个存储在(i,j)处的回溯方向. 相对 ...

  2. 动态规划之最长公共子序列(LCS)

    转自:http://segmentfault.com/blog/exploring/ LCS 问题描述 定义: 一个数列 S,如果分别是两个或多个已知数列的子序列,且是所有符合此条件序列中最长的,则 ...

  3. [Data Structure] LCSs——最长公共子序列和最长公共子串

    1. 什么是 LCSs? 什么是 LCSs? 好多博友看到这几个字母可能比较困惑,因为这是我自己对两个常见问题的统称,它们分别为最长公共子序列问题(Longest-Common-Subsequence ...

  4. 动态规划求最长公共子序列(Longest Common Subsequence, LCS)

    1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...

  5. LintCode 77: 最长公共子序列

    public class Solution { /** * @param A, B: Two string. * @return: the length of the longest common s ...

  6. 最长下降子序列O(n^2)及O(n*log(n))解法

    求最长下降子序列和LIS基本思路是完全一样的,都是很经典的DP题目. 问题大都类似于 有一个序列 a1,a2,a3...ak..an,求其最长下降子序列(或者求其最长不下降子序列)的长度. 以最长下降 ...

  7. 删除部分字符使其变成回文串问题——最长公共子序列(LCS)问题

    先要搞明白:最长公共子串和最长公共子序列的区别.    最长公共子串(Longest Common Substirng):连续 最长公共子序列(Longest Common Subsequence,L ...

  8. [BZOJ3173][Tjoi2013]最长上升子序列

    [BZOJ3173][Tjoi2013]最长上升子序列 试题描述 给定一个序列,初始为空.现在我们将1到N的数字插入到序列中,每次将一个数字插入到一个特定的位置.每插入一个数字,我们都想知道此时最长上 ...

  9. 3173: [Tjoi2013]最长上升子序列

    原题:http://www.lydsy.com/JudgeOnline/problem.php?id=3173 题解:促使我写这题的动力是,为什么百度遍地是Treap,黑人问号??? 这题可以用线段树 ...

随机推荐

  1. iOS 字符串截取,将字符串中用括号包含的内容去除

    //去除字符串中用括号括住的位置 -(NSString *)handleStringWithString:(NSString *)str{ NSMutableString * muStr = [NSM ...

  2. Symfony 如何使用ckeditor

    首先: 1)加载以下两个bundle "egeloen/ckeditor-bundle": "^4.0","helios-ag/fm-elfinder ...

  3. ARDUINO W5100 WebClient 测试

    基础工作:W5100扩展板插在ARDUINO上.用网线把W5100和自己家的路由器连接.插上网线能看到侧面网口指示灯变亮.路由器开启DHCP服务(一般都是开启的). 1.打开官方例程里面的Ethern ...

  4. #!/usr/bin/python和#!/usr/bin/env 的区别(转)

    #!/usr/bin/python和#!/usr/bin/env 的区别   #!/usr/bin/python 通常在一个.py文件开头都会有这个语句 它只在Linux系统下生效,意思是当作为可执行 ...

  5. 禁用chrome浏览器的cookie

    Chrome: 1.打开chrome浏览器,点击右上角的“自定义和控制Google Chrome”按钮 2.在下拉菜单中选择设置 3.点击设置页底部的“显示高级设置...” 4.在隐私设置下,点击“内 ...

  6. UIImageView 获取图片的 宽 高

    该文章纯属这两天开发的经验之谈 并且也是平常没注意 这回发现的一个小方法 并且很实用 在开发中 提高了很大的效率 更加符合高保真的要求 通常 美术 切的一些图片 需要 :1还原的 现在 我们一般支持i ...

  7. C#Winform之等待窗体

    窗体主要代码: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 ...

  8. zabbix实现mysql数据库的监控(一)

    zabbix是一个基于WEB界面的提供分布式系统监视以及网络监视功能的企业级的开源解决方案.它能监视各种网络参数,保证服务器系统的安全运营:并提供灵活的通知机制以让系统管理员快速定位/解决存在的各种问 ...

  9. python详细目录

    python第一篇 第二篇.初识列表字典元祖循环 第三篇.内置方法 第四篇.编码解码 列表.元祖 第五篇.数据类型 第六篇 函数 第七篇.函数二 第八篇.递归.装饰器 第九篇 正则表达式 第十篇.模块 ...

  10. 剑指offer——求1+2+...+n

    方法一.通过在类的构造函数中执行加的过程. #include <iostream> using namespace std; class Base { public: Base(){n++ ...