点我看题目

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

题意 :这个题一开始我瞅了好久都没觉得是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. Linux系统下安装ISO文件

    1.在/mnt目录下,创建相应的iso文件夹,例如cd1,cd2. 2.然后,用命令:mount -o loop /home/kinglu/Matlab/matlab1.iso(iso文件路径名) / ...

  2. [wordpress]后台自定义菜单字段和使用wordpress color picker

    Wordpress Version 4.4.2 参考链接 插件使用wordpress color picker:Add A New Color Picker To WordPress 后台菜单自定义字 ...

  3. (原创)openvswitch实验连载1-fedora 17下安装openvswitch

    1 软件安装 1.1测试环境和网络拓朴 大部分朋友估计也没有一个真实环境来完全整个的测试,所以我也是使用了在一台PC机上使用Vmware Workstation的方式来进行实验.总体结构是在PC机上安 ...

  4. smarty实现缓存

    首先需要在mySmarty中添加配置信息,开启缓存,设置缓存文件存放目录,设置缓存时间缓存可以实现减少访问数据库,减轻数据库压力,访问一次数据库,形成静态页面,下次直接调用这个页面,也可以用nocac ...

  5. Floyd-Warshall算法详解(转)

    Floyd-Warshall算法,简称Floyd算法,用于求解任意两点间的最短距离,时间复杂度为O(n^3).我们平时所见的Floyd算法的一般形式如下: void Floyd(){ int i,j, ...

  6. 解决从linux本地文件系统上传文件到HDFS时的权限问题

    当使用 hadoop fs -put localfile /user/xxx 时提示: put: Permission denied: user=root, access=WRITE, inode=& ...

  7. 英语中的 姓氏/Surname

    .Chomsky (Belarusian: Хомскі, Russian: Хомский, Ukrainian: Хомський, Hebrew: חומסקי‎‎, "from (V ...

  8. 洛谷 P1108 低价购买

    P1108 低价购买 标签 动态规划 难度 提高+/省选- 题目描述 "低价购买"这条建议是在奶牛股票市场取得成功的一半规则.要想被认为是伟大的投资者,你必须遵循以下的问题建议:& ...

  9. 关于JSON对象,以及联合数组,eval函数的使用参考

    关于JSON对象,以及联合数组,eval函数的使用参考 var json="{persons:[{name:'Zhangsan',sex:'male'},{name:'Lisi',sex:' ...

  10. 九度OJ 城际公路网 -- 图论

    题目地址:http://ac.jobdu.com/problem.php?pid=1343 题目描述: 为了加快城市之间的通行和物资流动速度,A国政府决定在其境内的N个大中型城市之间,增加修建K条公路 ...