HDOJ(HDU).1025 Constructing Roads In JGShining's Kingdom (DP)
HDOJ(HDU).1025 Constructing Roads In JGShining’s Kingdom (DP)
题目分析
题目大意就是给出两两配对的poor city和rich city,求解最多能修几条不相交的路。此题可以转化为LIS问题。转化过程如下:
数据中有2列,为方便表述,暂且叫做第一列和第二列。
1.若第一列是是递增的(给出的2个样例都是递增的),那么要想尽可能多的做连线,则那么就需要找出第二列中最长的递增子序列,若出现非递增的序列,那么连线后一定会相交。
2.若第一列不是递增的,排序后按照1分析即可。
综上所述,题目便转换成LIS问题。
LIS有2种写法,一种是o(n²)的写法,一种是o(nlogn)的写法。题目中给出n<=500,500.采用o(n²)必定超时,最佳策略是o(nlogn)。
推荐一篇介绍这种写法的博文 最长上升子序列nlogn算法。通俗易懂,在此就不赘述如何设计此算法了。
代码总览
/*
Title:HDOJ.1025
Author:pengwill
Date:2017-2-15
*/
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#define nmax 500005
using namespace std;
int a[nmax],dp[nmax];
int BS(int dt[],int t[],int left, int right,int i)
{
int mid;
while(left<right){
mid = (left+right)/2;
if(dt[mid]>=t[i]) right = mid;
else left = mid+1;
}
return left;
}
int main()
{
// freopen("in.txt","r",stdin);
int n,t = 0;
while(scanf("%d",&n) != EOF){
printf("Case %d:\n",++t);
int x,y;
for(int i = 1;i<=n; ++i){scanf("%d%d",&x,&y);a[x] = y;}
dp[1] = a[1];
int len=1;
// for(int i = 1; i<=n; ++i){
// int m = 0;
// for(int j = 1; j<i ;++j ){
// if(a[i]>a[j] && dpa[j]>m)
// m = dpa[j];
// }
// dpa[i] = m+1;
// }
for(int i = 2; i<=n;++i){
if(a[i]>dp[len]) dp[++len] = a[i];
else{
int pos = BS(dp,a,1,len,i);
dp[pos] = a[i];
}
}
if(len == 1) 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 0;
}
HDOJ(HDU).1025 Constructing Roads In JGShining's Kingdom (DP)的更多相关文章
- 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+二分)
点我看题目 题意 :两条平行线上分别有两种城市的生存,一条线上是贫穷城市,他们每一座城市都刚好只缺乏一种物资,而另一条线上是富有城市,他们每一座城市刚好只富有一种物资,所以要从富有城市出口到贫穷城市, ...
- 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 【dp+二分法】
主题链接:pid=1025">http://acm.acmcoder.com/showproblem.php?pid=1025 题意:本求最长公共子序列.但数据太多. 转化为求最长不下 ...
- 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 ...
随机推荐
- django1.11+xadmin的搭建
1.git clone https://github.com/sshwsfc/xadmin.git或者直接下载zip包 2..在项目根目录下建一个extra_apps的包,将xadmin源码包存放在里 ...
- 第3章 TCP协议详解
第3章 TCP协议详解 3.1 TCP服务的特点 传输协议主要有两个:TCP协议和UDP协议,TCP协议相对于UDP协议的特点是 面向连接使用TCP协议通信的双方必须先建立连接,完成数据交换后,通信双 ...
- Java开发工程师(Web方向) - 03.数据库开发 - 第1章.JDBC
第1章--JDBC JDBC基础 通过Java Database Connectivity可以实现Java程序对后端数据库的访问 一个完整的数据库部署架构,通常是由客户端和服务器端两部分组成 客户端封 ...
- Pyhton网络爬虫实例_豆瓣电影排行榜_BeautifulSoup4方法爬取
-----------------------------------------------------------学无止境------------------------------------- ...
- hadoop3.0新特性及新功能
Hadoop-3.0.0-alpha2版本发布,相比之前的hadoop-2.x有一系列的功能增强.但目前还是个alpha版本,有很多bug,且不能保证API的稳定和质量. 主要变化 Java最低版本要 ...
- 2018(容斥定理 HDU6286)
2018 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- Ubuntu—安装并运行sublime
step1 到官网看看 https://www.sublimetext.com/3 step2 根据版本选择,我的是32位的 step3 ubuntu终端安装 (1)切换目录 -$ cd /opt ...
- sql STUFF 分组
---将sql想关的数据放到一个字段里 select r.Region_Name, )) [text()] from City c2 inner join Region as r2 on c2.Reg ...
- HTMLTestRunner解决UnicodeDecodeError: ‘ascii’ codec can’t decode byte 0xe5 in position 108: ordinal not in range(128)
其中HTML和数据库都是设置成utf-8格式编码,插入到数据库中是正确的,但是当读取出来的时候就会出错,原因就是python的str默认是ascii编码,和unicode编码冲突,就会报这个标题错误. ...
- Qt-排序
1.要求传入起始指针,总长度,单元素空间占用大小(sizeof(A[i])),判断函数. 判断函数参数类型为const void *,使用需要在函数内自行转换为对应类型, 返回值为整数型,升序排序时正 ...