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 ...
随机推荐
- tcp.validnode_checking踩过的坑
对Oracle 检查ip合法性,就必须在服务器端的sqlnet.ora文件中设置如下参数 TCP.INVITED_NODES=(10.0.0.36,10.0.0.1,10.0.0.35) TCP.EX ...
- CodeForces - 455C Civilization (dfs+并查集)
http://codeforces.com/problemset/problem/455/C 题意 n个结点的森林,初始有m条边,现在有两种操作,1.查询x所在联通块的最长路径并输出:2.将结点x和y ...
- ObservableData-另一种姿势的观察者模式
使用 数据的定义 public class TestModel { private static TestModel mInstance; public static TestModel GetIns ...
- 通用Excel文件导出工具类
1:Excel格式 2:ExcelUtil.java import java.io.ByteArrayOutputStream; import java.io.IOException; import ...
- Anaconda3 指南
Anaconda 是一个 Python 的生态.它包含很多的科学计算库和大数据处理工具等. $ python --version Python 3.6.1 :: Anaconda 4.4.0 (64- ...
- 有关ACM学习的博客链接
大综合: 杭电OJ水题大全题解:http://blog.csdn.net/ysc504?viewmode=contents 14级浙江财经大学大佬:http://blog.csdn.net/jtjy5 ...
- 位运算&,逻辑与and
在python中,0是否 >>> True and True True >>> True and True True >>> 2 and 4 4 ...
- mysql 原理 ~ change buffer
一 简介:今天咱们来聊聊mysql的change buffer二 详细说明 1 +-change Buffer和数据页一样,也是物理页的一个组成部分,数据结构也是一颗B+树,这棵B+树放在共享表空 ...
- mysql 查询优化~ 分页优化讲解
一 简介:今天咱们来聊聊mysql的分页查询二 语法 LIMIT [offset,] rows offset是第多少条 rows代表多少条之后的行数 性能消耗 se ...
- css scrollbar样式设置
参考链接:https://segmentfault.com/a/1190000012800450