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 ...
随机推荐
- JAVA记录-String/StringBuilder/StringBuffer区别
- java虚拟内存设置 防止内存溢出 OutOfMemory【转】【原】
outofmemory permgen 这个问题主要还是由 java.lang.OutOfMemoryError: Java heap space 引起的. 有这两种解决方法: 1.设置环境变量 解决 ...
- AWT和Swing的关系
1.AWT和Swing都是java中的包. 2.AWT(Abstract Window Toolkit):抽象窗口工具包,早期编写图形界面应用程序的包,AWT是通过调用操作系统的native方法实现的 ...
- HITS算法--从原理到实现
本文介绍HITS算法的相关内容. 1.算法来源 2.算法原理 3.算法证明 4.算法实现 4.1 基于迭代法的简单实现 4.2 MapReduce实现 5.HITS算法的缺点 6.写在最后 参考资料 ...
- es6三个点运算符
...扩展运算符:可以将数组或对象里面的值展开 const b = {a:1,b:2} console.log({...b,c:3}); //{a:1,b:2,c:3} 一定程度上可以替代apply方 ...
- B树学习总结
1,B树的基本介绍 ①B树,相比于二叉树.红黑树而言,它的特点就是树的高度比其他类型的树要低很多.如何做到低呢?B树中的每个结点的分支数目非常大,即每个结点有很多很多孩子结点.这样,在相同结点数目情况 ...
- TCP/IP详解 卷1 第十八章 TCP的建立与终止
第十八章 TCP的建立与终止 tcpdump Tcpdump可以将网络中传送的数据报完截获下来进行分析.它支持针对网络层.协议.主机.网络或端口的过滤,并提供and.or.not等逻辑语句来帮助你去掉 ...
- 六道JavaScript测验题
1.找出数字数组中最大的元素(使用Match.max函数) var a=[123,23432,345,3,34]; console.log(Math.max.apply(null,a)); 2.转化一 ...
- JavaScript之加载表格、表单行数据[插件]
/*** * name:加载表格或表单数据[通用] * name:load-table-or-form-data-common.js * * author:zengtai * date:2017-07 ...
- UE4中Component和Subobject的区别
Component是Subobject的一种. 其中, Subobject是一种Outer不是一个UPackage的UObject,而UPackage是整个层次结构中的最上层,指向磁盘上的一个.uas ...