点我看题目

题意 :两条平行线上分别有两种城市的生存,一条线上是贫穷城市,他们每一座城市都刚好只缺乏一种物资,而另一条线上是富有城市,他们每一座城市刚好只富有一种物资,所以要从富有城市出口到贫穷城市,所以要修路,但是不能从富有的修到富有的也不能从贫穷的修到贫穷的,只能从富有的修到贫穷的,但是不允许修交叉路,所以问你最多能修多少条路。

题意 :这个题一开始我瞅了好久都没觉得是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+二分)的更多相关文章

  1. HDOJ(HDU).1025 Constructing Roads In JGShining's Kingdom (DP)

    HDOJ(HDU).1025 Constructing Roads In JGShining's Kingdom (DP) 点我挑战题目 题目分析 题目大意就是给出两两配对的poor city和ric ...

  2. 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 ...

  3. [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 ...

  4. 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 ...

  5. 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 ...

  6. hdu 1025 Constructing Roads In JGShining’s Kingdom 【dp+二分法】

    主题链接:pid=1025">http://acm.acmcoder.com/showproblem.php?pid=1025 题意:本求最长公共子序列.但数据太多. 转化为求最长不下 ...

  7. HDU 1025 Constructing Roads In JGShining's Kingdom(求最长上升子序列nlogn算法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1025 解题报告:先把输入按照r从小到大的顺序排个序,然后就转化成了求p的最长上升子序列问题了,当然按p ...

  8. hdu 1025 Constructing Roads In JGShining's Kingdom

    本题明白题意以后,就可以看出是让求最长上升子序列,但是不知道最长上升子序列的算法,用了很多YY的方法去做,最后还是超时, 因为普通算法时间复杂度为O(n*2),去搜了题解,学习了一下,感觉不错,拿出来 ...

  9. 最长上升子序列 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 ...

随机推荐

  1. 最佳vim技巧

    最佳vim技巧----------------------------------------# 信息来源----------------------------------------www.vim ...

  2. hasLayout与Block formatting contexts的学习(下)

    BFC布局规则: 内部的Box会在垂直方向,一个接一个地放置. Box垂直方向的距离由margin决定.属于同一个BFC的两个相邻Box的margin会发生重叠 每个元素的margin box的左边, ...

  3. JS+CSS简单实现DIV遮罩层显示隐藏【转藏】

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. Dremel made simple with Parquet

    http://lastorder.me/tag/parquet.html https://blog.twitter.com/2013/dremel-made-simple-with-parquet 对 ...

  5. 【HTTPS】Https和SSL学习笔记(一)

    1. 什么是HTTPS 在说HTTPS之前必须要先说一下HTTP.我们平常浏览网页用的就是HTTP协议,HTTP协议之间传输的数据都是明文,这样对于一些敏感信息传输其实是不安全的,很容易被恶意窃取.应 ...

  6. zookeeper实现互斥锁

    简单的说,zookeeper就是为了解决集群环境中数据一致性的问题. 举个很简单栗子: 有一个变量A,分别存在于两台服务器中,某个程序需要用到变量A,就随机地访问其中一台服务器并取得变量A的值,对吧? ...

  7. ###《More Effective C++》- 异常

    More Effective C++ #@author: gr #@date: 2015-05-24 #@email: forgerui@gmail.com 九.利用destructors避免泄漏资源 ...

  8. Podfile 文件的编写

    # Uncomment this line to define a global platform for your projectplatform :ios, '9.0' target 'Cocoa ...

  9. vs 2010 Cannot find or open the PDB file

    打开VS2010:工具-->选项-->>调试-->符号接下来就是选择Microsoft,然后确认  接着随便编译一个程序,过程会灰常的慢. 看到此目录下符号缓存了吗?C:\Us ...

  10. 08_rlCoachKin自主编译,调试

    为了知道参数的意思,以及为了从头建立一个项目,我从使用QTCreator来单独建立项目(当然也可以直接使用源代码中建立好的VS项目). 其实也推荐 VS2010调试 如果是用自带的VS项目,那么我们需 ...