hdu 1025 Constructing Roads In JGShining's Kingdom(DP)
2015-08-07 20:05
344 查看
传送门
Total Submission(s): 18809 Accepted Submission(s): 5314
[/b]
[align=left]Problem Description[/align]
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.
![](http://acm.hdu.edu.cn/data/images/1025-1.png)
In order to build as many roads as possible, the young and handsome king of the kingdom - JGShining needs your help, please help him. ^_^
[align=left]Input[/align]
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.
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
[align=left]Sample Output[/align]
题目:
输入第一行为n,代表n个贫困城市和n个富裕城市。
接下来n行,每一行两个数,第一个数代表贫困城市的坐标,第二个代表富裕城市的坐标。
问你如果在贫困城市和富裕城市之间建工路,并且任何两段公路都不能相交,每个贫困城市和每个富裕城市最多只能建一条公路,问你最多能建多少条公路。
分析:这个题主要是分析,如果分析好了就是一个很简单的问题,现在我们思考这样一个问题,把两个数组的最长公共子序列里的每个数在两数组里对应位置连一条边,很容易发现他们并不会产生相交的边,这样就和这个题一样了,我们只需求出贫困城市和富裕城市坐标的最长公共子序列就行了,但是还可以简化一下,为了方便,我们把贫困城市坐标由小到大排序,当然富裕城市坐标也要一一对应,那么贫困城市坐标就是一个不降的序列,现在只需求出富裕城市坐标的最长上升子序列即可。
Constructing Roads In JGShining's Kingdom
[b]Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 18809 Accepted Submission(s): 5314
[/b]
[align=left]Problem Description[/align]
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.
![](http://acm.hdu.edu.cn/data/images/1025-1.png)
In order to build as many roads as possible, the young and handsome king of the kingdom - JGShining needs your help, please help him. ^_^
[align=left]Input[/align]
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.
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
2 1 2 2 1 3 1 2 2 3 3 1
[align=left]Sample Output[/align]
Case 1: My king, at most 1 road can be built. Case 2: My king, at most 2 roads can be built.
题目:
输入第一行为n,代表n个贫困城市和n个富裕城市。
接下来n行,每一行两个数,第一个数代表贫困城市的坐标,第二个代表富裕城市的坐标。
问你如果在贫困城市和富裕城市之间建工路,并且任何两段公路都不能相交,每个贫困城市和每个富裕城市最多只能建一条公路,问你最多能建多少条公路。
分析:这个题主要是分析,如果分析好了就是一个很简单的问题,现在我们思考这样一个问题,把两个数组的最长公共子序列里的每个数在两数组里对应位置连一条边,很容易发现他们并不会产生相交的边,这样就和这个题一样了,我们只需求出贫困城市和富裕城市坐标的最长公共子序列就行了,但是还可以简化一下,为了方便,我们把贫困城市坐标由小到大排序,当然富裕城市坐标也要一一对应,那么贫困城市坐标就是一个不降的序列,现在只需求出富裕城市坐标的最长上升子序列即可。
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; int dp[500002]; struct node { int p,r; }E[500002]; bool cmp(node x,node y) { return x.p<y.p; } int main() { int n,ca=0; while(~scanf("%d",&n)) { for(int i=1;i<=n;i++) scanf("%d%d",&E[i].p,&E[i].r); sort(E+1,E+n+1,cmp); dp[1]=E[1].r; int k=0; for(int i=1;i<=n;i++) { if(E[i].r>dp[k]) { k++; dp[k]=E[i].r; continue; } int pos=lower_bound(dp+1,dp+k+1,E[i].r)-dp; dp[pos]=E[i].r; } printf("Case %d:\n",++ca); if(k==1) printf("My king, at most %d road can be built.\n",k); else printf("My king, at most %d roads can be built.\n",k); printf("\n"); } return 0; }
相关文章推荐
- pku Power Strings
- poj 2752 Seek the Name, Seek the Fame
- hdu 1063 Exponentiation(有小数的高精)
- Java — 程序设计基础(Core Java I)
- C++对象模型——指向Data Members的指针(Pointer to Data Members)(第三章)
- windows平台下的换行
- odoo多币种
- Odoo 运费
- ACM求路灯照射的最小半径问题
- Android(java)学习笔记143:android提供打开各种文件的API接口:setDataAndType
- 我的明天在何处
- servlet
- git 常用命令
- 使用xib封装一个自定义view的步骤
- hdu5277 YJC counts stars(最大团)
- Android 应用程序窗体显示状态操作(requestWindowFeature()的应用
- [淘宝经验] - HBASE在淘宝网的应用和优化小结
- POJ 1011 Sticks dfs+剪枝
- POJ-2864 Pascal Library-年年参加聚会
- 2015 Multi-University Training Contest 6