hdu 1025 上面n个点与下面n个点对应连线 求最多能连有多少条不相交的线 (LIS)
题目大意
有2n个城市,其中有n个富有的城市,n个贫穷的城市,其中富有的城市只在一种资源富有,且富有的城市之间富有的资源都不相同,贫穷的城市只有一种资源贫穷,且各不相同,现在给出一部分贫穷城市的需求,每个需求都是一个贫穷的向一个富有的城市要资源,且每个富有的城市都想向贫穷的城市输入自己富有的那部分资源,现在为了运输要建设多条路,但是路与路之间不允许有交叉,求满足贫穷城市的各种要求最多可以建设多少条路
简化版::上面n个点,下面n个点,然后在这2n个点之间随意连线,一个点只能被连一次,问最多有多少条线不交叉。
解题思路:
将贫穷的城市按从小到大的顺序排列,然后求富有的城市序号的最大上升子序列LIS解决即可
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.
# include <iostream>
# include <cstdio>
# include <cstring>
# include <algorithm>
# include <cmath>
# define LL long long
using namespace std ; int f[] ;
//int dp[500010] ;
int n ; struct city
{
int p ;
int r ;
}a[]; bool cmp(const city &x , const city &y)
{
return x.p < y.p ;
} int bsearch(int size, const int &a) {
int l=, r=size-;
while( l <= r ){
int mid = (l+r)/;
if( a > f[mid-] && a <= f[mid] ) return mid;// >&&<= 换为: >= && <
else if( a < f[mid] ) r = mid-;
else l = mid+;
}
} int LIS()
{
int i, j, size = ;
f[] = a[].r;
//dp[0] = 1;
for( i=; i < n; ++i )
{
if( a[i].r <= f[] ) j = ; // <= 换为: <
else if( a[i].r > f[size-] ) j = size++;// > 换为: >=
else j = bsearch(size, a[i].r);
f[j] = a[i].r;
//dp[i] = j+1;
}
return size;
} int main ()
{
//freopen("in.txt","r",stdin) ;
int Case = ;
while(scanf("%d" , &n) !=EOF)
{
printf("Case %d:\n" , Case) ;
Case++ ;
int i ;
for (i = ; i < n ; i++)
{
scanf("%d %d" , &a[i].p , &a[i].r) ;
}
sort(a,a+n,cmp) ;
int ans = LIS() ;
if (ans == )
printf("My king, at most %d road can be built.\n" , ans) ;
else
printf("My king, at most %d roads can be built.\n" , ans) ;
printf("\n") ; } return ;
}
hdu 1025 上面n个点与下面n个点对应连线 求最多能连有多少条不相交的线 (LIS)的更多相关文章
- HDU 1025 LIS二分优化
题目链接: acm.hdu.edu.cn/showproblem.php?pid=1025 Constructing Roads In JGShining's Kingdom Time Limit: ...
- HDOJ(HDU).1025 Constructing Roads In JGShining's Kingdom (DP)
HDOJ(HDU).1025 Constructing Roads In JGShining's Kingdom (DP) 点我挑战题目 题目分析 题目大意就是给出两两配对的poor city和ric ...
- HDU 1025 Constructing Roads In JGShining's Kingdom(二维LIS)
Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65 ...
- HDU 1025 Constructing Roads In JGShining's Kingdom(DP+二分)
点我看题目 题意 :两条平行线上分别有两种城市的生存,一条线上是贫穷城市,他们每一座城市都刚好只缺乏一种物资,而另一条线上是富有城市,他们每一座城市刚好只富有一种物资,所以要从富有城市出口到贫穷城市, ...
- 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 Des ...
- HDU 1025 Constructing Roads In JGShining's Kingdom(求最长上升子序列nlogn算法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1025 解题报告:先把输入按照r从小到大的顺序排个序,然后就转化成了求p的最长上升子序列问题了,当然按p ...
- HDU 1025 DP + 二分
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1025 求最长递增子序列,O(n^2)的复杂度超时,需要优化为O(n*logn) f[i]存储长度为i的最小 ...
- hdu 1025 Constructing Roads In JGShining’s Kingdom 【dp+二分法】
主题链接:pid=1025">http://acm.acmcoder.com/showproblem.php?pid=1025 题意:本求最长公共子序列.但数据太多. 转化为求最长不下 ...
- Hdu 1025(LIS)
Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65 ...
随机推荐
- 为什么推荐InnoDB引擎使用自增主键?
索引使用时遇到的问题(顺丰)--InnoDB引擎不使用自增主键导致性能问题,也可答最左前缀 InnoDB自增主键 InnoDB主索引(同时也是数据文件)的示意图: 上文讨论过InnoDB的索引实现,I ...
- cdh-5.10.0搭建安装
1.修改主机名为master, slave1, slave2 vim /etc/sysconfig/network HOSTNAME = master HOSTNAME = slave1 HOSTNA ...
- 006、容器 What、Why、How(2018-12-21 周五)
参考https://www.cnblogs.com/CloudMan6/p/6751516.html What - 什么是容器? 容器是一种轻量级.可移植.自包含的软件打包技术,是应用 ...
- Oracle语句优先级
SQL> SELECT SAL SALARY FROM EMP WHERE SALARY<2500;Warning: connection was lost and re-establis ...
- 【搬运】C指针 一
本文搬运自https://fishc.com.cn/forum.php?mod=viewthread&tid=71654&extra=page%3D1%26filter%3Dtypei ...
- 夏令时(DST)测试
夏令时测试是比较小众的测试,主要针对在有夏令时的国家使用的软件,如果你接触到了这方面的测试,说明你在挣国外的钱:). 话不多说,先来介绍下什么是夏令时: 夏时制,夏时令(Daylight Sa ...
- u-boot移植(九)---代码修改---NAND
一.NAND原理 NAND 无地址空间,地址和数据的发送都依赖于LDATA[0:7]这一串数据总线. 不看随机页编程,看到从高位到低位的页,总共分为64个页面,每个页的组成是2K + 64 个byt ...
- Aircrack-ng介绍
1.介绍 Aircrack-ng主要使用了两种攻击方式进行WEP破解:一种是FMS攻击:一种是KoreK攻击. 2.主要组件 1.aircrack-ng 主要用于WEP及WPA-PSK密码的恢复,主要 ...
- dubbo集群服务下一台服务挂了对服务调用的影响
一.问题描述:项目中2台dubbo服务给移动端提供查询接口,移动端反应说查询时而很快(秒刷),时而很慢(4-5秒). 二.问题分析: 1.问题猜想:网络不稳定原因导致,但是切换公司wifi和手机4G, ...
- 【洛谷P1896【SCOI2005】】互不侵犯King
题目描述 在N×N的棋盘里面放K个国王,使他们互不攻击,共有多少种摆放方案.国王能攻击到它上下左右,以及左上左下右上右下八个方向上附近的各一个格子,共8个格子. 输入输出格式 输入格式: 只有一行,包 ...