HDU 1025--LIS算法

HDU 1025

                                                                                           ——LIS算法

题目:
                                                 Constructing Roads In JGShining's Kingdom

                                                    Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
                                                                       Total Submission(s): 6810 Accepted Submission(s): 2015

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

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

Huge input, scanf is recommended.

Author
JGShining(极光炫影)

分析:

此题讲了资源匮乏城市与富裕城市之间建造不相交的道路,开始本以为是二分图匹配,结果果断悲剧,后来又看了看别人的解题报告,才知这道题又是个DP题,用到了LIS(Longest Increasing Subsequence)最长上升(不下降)子序列,它有两种算法复杂度为O(n*logn)和O(n^2)。前者使用二分查找,后者朴素查找。它的算法大致思路:设置两个a,b数组,a数组存储数据,b数组存储最长不降序序列。此算法关键在于设计二分查找。

代码:      

 1 #include<stdio.h>
2 int dp[50005],a[50005];
3
4 int Lis(int n)
5 {
6 int len=1,i,low,high,mid;
7 dp[1]=a[1];
8 for(i=2;i<=n;i++)
9 {
10 low=1;
11 high=len;
12 while(low<=high)
13 {
14 mid=(low+high)/2;
15 if(a[i]>dp[mid])
16 low=mid+1;
17 else
18 high=mid-1;
19 }
20 dp[low]=a[i];
21 if(low>len)
22 len=low;
23 }
24 return len;
25 }
26 int main()
27 {
28 int n,x,y,i,ans,k=1;
29 while(scanf("%d",&n)!=EOF)
30 {
31 for(i=0;i<n;i++)
32 {
33 scanf("%d%d",&x,&y);
34 a[x]=y;
35 }
36 ans=Lis(n);
37 printf("Case %d:\n",k++);
38 if(ans==1)
39 printf("My king, at most 1 road can be built.\n");
40 else
41 printf("My king, at most %d roads can be built.\n",ans);
42 printf("\n");
43 }
44 return 0;
45 }

PS.还有几天就要走了,又要开始北漂生活了,fight!
                         

posted @ 2012-02-07 10:02  hankers  阅读(1736)  评论(0编辑  收藏  举报