博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 1025--LIS算法
阅读量:7247 次
发布时间:2019-06-29

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

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

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

                         

转载于:https://www.cnblogs.com/hankers/archive/2012/02/07/2340904.html

你可能感兴趣的文章
mysql慢查询日志
查看>>
Office 365系列之九:使用Windows PowerShell管理O365平台
查看>>
CenOS 6.0配置本地yum源
查看>>
小作文_通知和备忘录
查看>>
06-Windows Server 2012 R2 会话远程桌面-标准部署-RD网关(RemoteApp)
查看>>
Alcatel 7750 常用维护命令
查看>>
mysql AUTO_INCREMENT 字符 删除后重1开始
查看>>
IOS应用从容地崩溃
查看>>
CMS之图片管理(1)
查看>>
vue组件
查看>>
生活热水循环泵选型怎么选,如何选型计算?
查看>>
机器学习中的预测问——回归与分类
查看>>
数据结构 -- 静态链表
查看>>
写的py_ping 可以跑了
查看>>
11月18日站立会议
查看>>
dos 磁盘操作系统
查看>>
数据结构顺序队列打卡
查看>>
Web service调用的安全性
查看>>
C语言测试:想成为嵌入式程序员应知道的0x10个基本问题【转】
查看>>
C#集合
查看>>