HDU 1025 Constructing Roads In JGShining's Kingdom(DP+二分)
题意 :两条平行线上分别有两种城市的生存,一条线上是贫穷城市,他们每一座城市都刚好只缺乏一种物资,而另一条线上是富有城市,他们每一座城市刚好只富有一种物资,所以要从富有城市出口到贫穷城市,所以要修路,但是不能从富有的修到富有的也不能从贫穷的修到贫穷的,只能从富有的修到贫穷的,但是不允许修交叉路,所以问你最多能修多少条路。
题意 :这个题一开始我瞅了好久都没觉得是DP,后来二师兄给讲了一下才恍然大悟。其实就是用到了DP中的那个最长上升子序列,把题中贫穷城市的坐标当成数组的下标,跟其相连的富有城市的坐标当作数组的值,这样的话,因为不能有交叉,所以只能一直往右,这就好比找最长上升子序列,因为用朴素的算法会超时所以要用二分。
解题报告这里边的解释非常详细,就是怎么用二分去做,其实就是将找到的子序列保存到B数组里,但是往里插入的过程用的是二分。
//HDU 1025 #include <iostream>
#include <stdio.h>
#include <string.h> using namespace std; int dp[] ;
int B[] ; int main()
{
int n ;
int casee = ;
while(~scanf("%d",&n))
{
int a,b ;
for(int i = ; i < n ; i++)
{
scanf("%d %d",&a,&b) ;
dp[a] = b ;//这样就相当于去找dp这个数组的最长上升子序列了
}
int len = ;
B[] = dp[] ;
for(int i = ; i <= n ; i++)//这里掉了等号WA到死啊
{
int low = , high = len ;
while(low <= high)
{
int mid = (low+high) >> ;
if(B[mid] < dp[i]) low = mid+ ;
else high = mid - ;
}
B[low] = dp[i] ;
if(low > len) len++ ;
}
printf("Case %d:\n",casee++) ;
if(len == )
printf("My king, at most 1 road can be built.\n\n") ;
else printf("My king, at most %d roads can be built.\n\n",len) ;
}
return ;
}
HDU 1025 Constructing Roads In JGShining's Kingdom(DP+二分)的更多相关文章
- 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 ...
- [ACM] hdu 1025 Constructing Roads In JGShining's Kingdom (最长递增子序列,lower_bound使用)
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 + 二分优化)
Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65 ...
- HDU 1025 Constructing Roads In JGShining's Kingdom[动态规划/nlogn求最长非递减子序列]
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+二分法】
主题链接:pid=1025">http://acm.acmcoder.com/showproblem.php?pid=1025 题意:本求最长公共子序列.但数据太多. 转化为求最长不下 ...
- HDU 1025 Constructing Roads In JGShining's Kingdom(求最长上升子序列nlogn算法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1025 解题报告:先把输入按照r从小到大的顺序排个序,然后就转化成了求p的最长上升子序列问题了,当然按p ...
- hdu 1025 Constructing Roads In JGShining's Kingdom
本题明白题意以后,就可以看出是让求最长上升子序列,但是不知道最长上升子序列的算法,用了很多YY的方法去做,最后还是超时, 因为普通算法时间复杂度为O(n*2),去搜了题解,学习了一下,感觉不错,拿出来 ...
- 最长上升子序列 HDU 1025 Constructing Roads In JGShining's Kingdom
最长上升子序列o(nlongn)写法 dp[]=a[]; ; ;i<=n;i++){ if(a[i]>dp[len]) dp[++len]=a[i]; ,dp++len,a[i])=a[i ...
随机推荐
- 滑动条slider
#include"ui/CocosGUI.h" using namespace ui; Text* displayValudLabel = Text::create("轻 ...
- 为 ASP.NET Web API 创建帮助页
http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/creating-api-help-pages 以前实例 ...
- 高级C#
使用delegates委托写插件方法: public delegate int Transformer (int x); public class Util { public static void ...
- web框架--来自维基百科
- jfinal取消默认跳转到view.jsp页面的方法
今天为了在一个列表中添加一个删除的方法,直接在方法里面谢了一个dao.del();方法,但是调用的时候却出现404错误. 然后就写了一句下面的代码 redirect("/api/listMe ...
- java培训(1-4节课)
课程安排:JavaEE方向(控制台程序,GUI程序,Web程序,手机程序)(dos命令是控制台程序:QQ是GUI程序,放在计算机上:QQ空间是Web程序,放在腾讯公司) 讲课的13本教材:C语言,Ja ...
- Mysql表操作
查看表结构: 可以使用describe或show create table语句查看表的结构: describe表名; Show create table 表名; 修改表名: Alter table 旧 ...
- 简单易用的Rest
今天碰巧,用到了淘宝的在线IP地址查询的Rest API,它提供接口给用户查询IP地址的归宿地.数据库比较庞大,准确性也比较高.地址为:http://ip.taobao.com/instruction ...
- Centos7源码安装mysql及读写分离,互为主从
Linux服务器 -源码安装mysql 及读写分离,互为主从 一.环境介绍: Linux版本: CentOS 7 64位 mysq版本: mysql-5.6.26 这是我安装时所使用的版本, ...
- HTML5的简介
前言:作为IOS开发工程师,终会接触到网页前端开发,甚至可能会有 用HTML5开发IOS的app客户端的需求.比如现在上架的app就有比如理财类型的app有的就用HTML开发的,从理财类型的app需求 ...