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

InputEach 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. 
OutputFor 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.
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio> using namespace std;
const int N = + ;
struct node{
int p, r;
}City[N]; int Q[N]; bool cmp(const node & x, const node & y){
return x.p < y.p;
}
void Solve(int n){
static int Case = ;
sort(City, City + n, cmp);
memset(Q, , sizeof(Q));
int cur = ;
for(int i = ; i < n; i++){
if(City[i].r > Q[cur]) Q[++cur] = City[i].r;
else *upper_bound(Q, Q + cur + , City[i].r) = City[i].r;
}
printf("Case %d:\nMy king, at most %d road%s can be built.\n\n", ++Case, cur, cur > ?"s":"");
}
int main(){
int n;
while(scanf("%d", &n) == ){
for(int i = ; i < n; i++) scanf("%d %d", &City[i].p, &City[i].r);
Solve( n );
}
return ;
}

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

  1. 2.16 最长递增子序列 LIS

    [本文链接] http://www.cnblogs.com/hellogiser/p/dp-of-LIS.html [分析] 思路一:设序列为A,对序列进行排序后得到B,那么A的最长递增子序列LIS就 ...

  2. 最长上升子序列LIS(51nod1134)

    1134 最长递增子序列 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 给出长度为N的数组,找出这个数组的最长递增子序列.(递增子序列是指,子序列的元素是递 ...

  3. 动态规划(DP),最长递增子序列(LIS)

    题目链接:http://poj.org/problem?id=2533 解题报告: 状态转移方程: dp[i]表示以a[i]为结尾的LIS长度 状态转移方程: dp[0]=1; dp[i]=max(d ...

  4. 【部分转载】:【lower_bound、upperbound讲解、二分查找、最长上升子序列(LIS)、最长下降子序列模版】

    二分 lower_bound lower_bound()在一个区间内进行二分查找,返回第一个大于等于目标值的位置(地址) upper_bound upper_bound()与lower_bound() ...

  5. 题解 最长上升子序列 LIS

    最长上升子序列 LIS Description 给出一个 1 ∼ n (n ≤ 10^5) 的排列 P 求其最长上升子序列长度 Input 第一行一个正整数n,表示序列中整数个数: 第二行是空格隔开的 ...

  6. 最长回文子序列LCS,最长递增子序列LIS及相互联系

    最长公共子序列LCS Lintcode 77. 最长公共子序列 LCS问题是求两个字符串的最长公共子序列 \[ dp[i][j] = \left\{\begin{matrix} & max(d ...

  7. 一个数组求其最长递增子序列(LIS)

    一个数组求其最长递增子序列(LIS) 例如数组{3, 1, 4, 2, 3, 9, 4, 6}的LIS是{1, 2, 3, 4, 6},长度为5,假设数组长度为N,求数组的LIS的长度, 需要一个额外 ...

  8. 1. 线性DP 300. 最长上升子序列 (LIS)

    最经典单串: 300. 最长上升子序列 (LIS) https://leetcode-cn.com/problems/longest-increasing-subsequence/submission ...

  9. 最长上升子序列(LIS)模板

    最长递增(上升)子序列问题:在一列数中寻找一些数,这些数满足:任意两个数a[i]和a[j],若i<j,必有a[i]<a[j],这样最长的子序列称为最长递增(上升)子序列. 考虑两个数a[x ...

随机推荐

  1. 正则爬取京东商品信息并打包成.exe可执行程序

    本文爬取内容,输入要搜索的关键字可自动爬取京东网站上相关商品的店铺名称,商品名称,价格,爬取100页(共100页) 代码如下: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 ...

  2. C++ 打印XPS文档

    CoInitializeEx(, COINIT_MULTITHREADED); IXpsOMObjectFactory *xpsFactory; HRESULT hr = CoCreateInstan ...

  3. 微信小程序访问后台出现 对应的服务器证书无效。控制台输入 showRequestInfo() 可以获取更详细信息。

    检查微信开发者平台配置 https 服务端 nginx 配置 ssl 协议是否有效 在开发者工具中可以使用(详情 > 不校验合法域名.web-view(业务域名).TLS 版本以及 HTTPS ...

  4. Linux vi/vim and linux yum 命令

    Linux vi/vim 所有的 Unix Like 系统都会内建 vi 文书编辑器,其他的文书编辑器则不一定会存在. 但是目前我们使用比较多的是 vim 编辑器. vim 具有程序编辑的能力,可以主 ...

  5. Spring Data Jpa (五)@Entity实例里面常用注解详解

    详细介绍javax.persistence下面的Entity中常用的注解. 虽然Spring Data JPA已经帮我们对数据的操作封装得很好了,约定大于配置思想,帮我们默认了很多东西.JPA(Jav ...

  6. NOIP2018复赛游记

    Day -oo (这里是负无穷啦qwq) 为了NOIP2018,我已经准备了好久 暑假的封闭式训练,国庆后停掉了晚自习,距NOIP一周时更是停掉了全天的课...... 我像是一个圆,在近乎无限的空间里 ...

  7. WebView:是应用程序打开web网页的UI控件后端

    public class WebViewActivity extends Activity { private WebView webView; @Override protected void on ...

  8. git跟yum一样 linux下的命令使用和思想是类似的

    git跟yum一样 linux下的命令使用和思想是类似的

  9. leetcode 回文二叉树

    C++最简单的方法,遍历存在vector<int> ivec容器中,然后头尾对应比较 O(n)时间,O(n)空间 /** * Definition for singly-linked li ...

  10. 阶段3 1.Mybatis_11.Mybatis的缓存_2 延迟加载和立即加载的概念

    用户关联的account信息,假设一个用户管理的account有100个.那么我们在查询用户的时候那100个关联的信息也被查询出来. 用的时候才去查关联的数据 这两个不同的地方就是查询的时机不同 什么 ...