1025
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.

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.
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.HintHuge input, scanf is recommended.
Author
JGShining(极光炫影)
1 /*Description: to
2 *
3 * Author: Vincent
4 *
5 * Date:2010/04/
6 * Contact:agilely@126.com
7 * Zju CS Lab
8 */
9 /*排序+二分搜索+一维DP
10 Accepted 1025 343MS 760K
11 (1)zoj1986 Bridging Signals
12 (2)hdoj1025 Constructing Roads In JGShining's Kingdom
13 (3)轮船Ships
14 Sample Input 两对面的城市修路
15 2 要求路不能交叉
16 1 2 最多能修几条?
17 2 1
18 3
19 1 2
20 2 3
21 3 1
22 Sample Output
23 Case 1:
24 My king, at most 1 road can be built.
25 Case 2:
26 My king, at most 2 roads can be built.
27 */
28 #include<stdio.h>
29 #include<stdlib.h>
30 #include<string.h>
31 struct node{int p,r;}city[500000];
32 int Max_load[500001];//dp数组
33 int cmp( const void *a ,const void *b)
34 { return (*(node *)a).p > (*(node *)b).p ? 1 : -1; }
35 int main()
36 {
37 int i,n,Cases=0;
38 while(scanf("%d",&n)!=EOF)
39 {//城市的对数n
40 Cases++;
41 if(n==0)
42 {
43 printf("Case %d:\nMy king, at most 0 road can be built.\n\n",Cases);
44 continue;
45 }
46 for(i=0;i<n;i++)
47 {
48 city[i].p=city[i].r=0;
49 Max_load[i]=0;
50 }
51 for(i=0;i<n;i++)//输入想要相通的城市对
52 scanf("%d%d",&city[i].p,&city[i].r);
53 qsort(city,n,sizeof(city[0]),cmp);//按照某一边的城市排序
54 /*O(n*logn) 二分搜索+DP*/
55 Max_load[0]=-1;
56 Max_load[1]=city[0].r;
57 int Len=1;
58 int p,r,m;
59 for(int i=1;i<n;i++)
60 {
61 p=0;r=Len;
62 while(p<=r)
63 {
64 m=(p+r)/2;
65 if(Max_load[m]<=city[i].r)
66 p=m+1;
67 else r=m-1;
68 }
69 Max_load[p]=city[i].r;
70 if(p>Len) Len++;
71 }//Len找到的路条数
72 /*
73 O(n^2)的DP,结果Time Limit Exceeded.
74 for(i=n-2;i>=0;i--)
75 for(j=i+1;j<n;j++)
76 {
77 if(city[i].r<city[j].r&&Max_load[i]<Max_load[j]+1)
78 {
79 Max_load[i]=Max_load[j]+1;
80 }
81 }
82 */
83 /*之前直接用
84 printf("Case %d:\nMy king, at most %d roads can be built.\n\n",Cases,Len);
85 结果就是WA,那个狂郁闷啊!!!!!!*/
86 if(Len==1) printf("Case %d:\nMy king, at most %d road can be built.\n\n",Cases,Len);
87 else
88 printf("Case %d:\nMy king, at most %d roads can be built.\n\n",Cases,Len);
89 }
90 return 0;
91 }
92

浙公网安备 33010602011771号