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 ...
随机推荐
- Leetcode 104. Maximum Depth of Binary Tree(二叉树的最大深度)
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- 在 Transact-SQL 中使用 TRY...CATCH
在 Transact-SQL 中使用 TRY...CATCH (注:本文来自于 http://msdn.microsoft.com/zh-cn/library/ms179296.aspx) ...
- oracle怎么查看表字段的类型
select column_name,data_type,DATA_LENGTH From all_tab_columns where table_name=upper('表名') AND owner ...
- Linux - 查看系统的版本信息
在 Linux 中,有多种方法可以查看系统的版本信息. uname 命令 huey@huey-K42JE:~$ uname -a Linux huey-K42JE 3.5.0-43-generic # ...
- HTML <input> 标签的 type 属性
HTML <input> 标签的 type 属性 HTML <input> 标签 实例 下面的表单拥有两个输入字段以及一个提交按钮: <form action=" ...
- Cisco交换机中的flash,Rom,RAM,nvram的区别
Flash内存,也叫闪存,是路由器当中常用的一种内存类型.它是可读写的存储器,在系统重新启动或关机之后仍能保存数据.Flash中存放着当前使用中的IOS(路由器操作系统). 只读内存(ROM)在Cis ...
- sql2008存储过程解密。
今天有一个同事在做一个项目的时候,因为现在公司不跟某一家公司合作.有一些sql的存储过程是加密,现在想打开那些存储过程来解密.故查看了一些资料终于解密成功.步骤如下: 1.需要开始DAC连接. 1.1 ...
- xadmin学习笔记(一)——编程准备
前言 xadmin是GitHub上的开源项目,它是Django admin的超强升级版,提供了强大的插件系统,丰富的内置功能,以及无与伦比的UI主题,使管理系统的实现变得异常简单.详情请参见官方网址. ...
- iOS开发——百度云推送
由于公司项目是集成的极光推送,详见下一篇博客. 集成百度推送大体相当,最好都参考官方文档集成,官方文档或官方网站教程是最好的博客. 百度Push服务SDK用户手册(iOS版) http://push. ...
- IOS开发之NSPredicate谓词的用法
编程的人员不管是上过大学还是从培训机构出来的或做后台的.前端的都应该SQL语句有所了解,我们知道,在SQL语句当中 where 条件表达式可以对二维关系表的数据做条件筛选.微软的C# .net中也实现 ...