博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
最长上升子序列(LIS) Medium2
阅读量:6942 次
发布时间:2019-06-27

本文共 3077 字,大约阅读时间需要 10 分钟。

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

21 22 131 22 33 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
#include
#include
#include
using namespace std;const int N = 500000 + 10;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 = 0; sort(City, City + n, cmp); memset(Q, 0, sizeof(Q)); int cur = 0; for(int i = 0; i < n; i++){ if(City[i].r > Q[cur]) Q[++cur] = City[i].r; else *upper_bound(Q, Q + cur + 1, City[i].r) = City[i].r; } printf("Case %d:\nMy king, at most %d road%s can be built.\n\n", ++Case, cur, cur > 1?"s":"");}int main(){ int n; while(scanf("%d", &n) == 1){ for(int i = 0; i < n; i++) scanf("%d %d", &City[i].p, &City[i].r); Solve( n ); } return 0;}

 

转载于:https://www.cnblogs.com/Pretty9/p/7406880.html

你可能感兴趣的文章
转载mysql数据库配置优化
查看>>
Perl图书的一些体会
查看>>
阿里Java开发规范&谷歌Java开发规范&华为Java开发规范&Tab键和空格比较&Eclipse的Tab键设置 总结...
查看>>
android电话状态的监听
查看>>
Linq中string转int的方法
查看>>
循环-12. 打印九九口诀表(15)
查看>>
html5--4-4 audio元素/格式的转换
查看>>
『TensorFlow』读书笔记_进阶卷积神经网络_分类cifar10_上
查看>>
SecureCRT发送键盘按键对应表(转义字符)
查看>>
SQL Server DBA SQL
查看>>
事务 ~ 锁(转)
查看>>
CentOS7脱机安装SQL Server 2017
查看>>
RK3399参考设计方案之DC-DC电源芯片RK808D【转】
查看>>
加密货币 (Cryptocurrency) 市值 (market capitalization) 列表
查看>>
julia应用于自动驾驶汽车、机器人、3D 打印、精准医疗、增强现实、基因组学、能源交易、机器学习、金融风控和太空任务设计等多个领域...
查看>>
Go Web:数据存储(3)——gob对象序列化
查看>>
协同过滤的几点思考
查看>>
multiple levlve proxy route deploy
查看>>
父窗体刷新子窗体
查看>>
MSSQL 数据页查询使他 DBCC PAGE 详细说明
查看>>