HDU 1025:Constructing Roads In JGShining's Kingdom(LIS+二分优化)
Posted Shadowdsp
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 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. ^_^
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.
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
题意:输入有两边,为p,q,求的是所有p到q不相交的话最多可以有多少条边相连,那么只要从1到n枚举p,然后road[p]就是一个q,这样求road[p]的LIS就好了。
思路:直接LIS的复杂度为O(n^2),对于500000的数据来说是肯定TLE的,加上二分优化的话复杂度就可以降低到O(nlogn)了。
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <iostream> 5 using namespace std; 6 #define N 500005 7 8 int dp[N],road[N]; 9 int x; 10 11 void cal(int a) 12 { 13 int l=1,r=x,mid; 14 while(l<=r){ 15 mid=(l+r)>>1; 16 if(dp[mid]<a) l=mid+1; 17 else r=mid-1; 18 } 19 dp[l]=a; 20 } 21 22 int main() 23 { 24 int n; 25 int cas=0; 26 while(~scanf("%d",&n)){ 27 memset(dp,0,sizeof(dp)); 28 x=1; 29 for(int i=1;i<=n;i++){ 30 int a,b; 31 scanf("%d%d",&a,&b); 32 road[a]=b; 33 } 34 dp[1]=road[1]; 35 for(int i=2;i<=n;i++){ 36 int a=road[i]; 37 if(a>dp[x]) dp[++x]=a; 38 else cal(a); 39 } 40 printf("Case %d:\n",++cas); 41 if(x==1) printf("My king, at most 1 road can be built.\n\n"); 42 else printf("My king, at most %d roads can be built.\n\n",x); 43 } 44 return 0; 45 }
以上是关于HDU 1025:Constructing Roads In JGShining's Kingdom(LIS+二分优化)的主要内容,如果未能解决你的问题,请参考以下文章
HDU 1025:Constructing Roads In JGShining's Kingdom(LIS+二分优化)
HDU 1025 Constructing Roads In JGShining's Kingdom
hdu-1025 Constructing Roads In JGShining's Kingdom(二分查找)
hdu1025 Constructing Roads In JGShining's Kingdom(二分+dp)
Constructing Roads In JGShining's Kingdom(HDU 1025 LIS nlogn方法)