hdu 1025 Constructing Roads In JGShining's Kingdom(最长上升子序列)
题意:贫穷和富有的城市都按顺序1-n排列,需要在中间建造尽可能多的道路,最多可以建造多少条?
解:如果条件这样给出,贫穷的城市按顺序排列,并且按顺序给出每个贫穷城市需要的资源,那么能建造的最多的道路数就是这个顺序中的最长上升子序列的长度。
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <queue>
#include <deque>
#include <map> using namespace std;
typedef long long ll; const int maxn=1e5+; ll n,a[maxn];
ll dp[maxn]; ll lower_bound(ll *a,int x,int y,ll v){
int m;
while(x<y){
m=x+(y-x)/;
if(a[m]>=v)y=m;
else x=m+;
}
return x;
} ll q[maxn],r[maxn]; int main(){
int o=;
while(scanf("%lld",&n)!=EOF){
for(int i=;i<n;i++){
scanf("%lld%lld",&q[i],&r[i]);
}
for(int i=;i<n;i++){
a[q[i]-]=r[i]-;
}
int m=;
for(int i=;i<n;i++){
if(m==||dp[m-]<a[i]){
dp[m++]=a[i];
}else{
ll j=lower_bound(dp,,m,a[i]);
dp[j]=a[i];
}
/*验证正确性
for(int j=0;j<m;j++){
printf("%lld ",dp[j]);
}printf("\n");
*/
}
printf("Case %d:\n",o++);
printf("My king, at most %d road",m);
if(m>)printf("s");
printf(" can be built.\n\n"); }
return ;
}
hdu 1025 Constructing Roads In JGShining's Kingdom(最长上升子序列)的更多相关文章
- 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(求最长上升子序列nlogn算法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1025 解题报告:先把输入按照r从小到大的顺序排个序,然后就转化成了求p的最长上升子序列问题了,当然按p ...
- HDU 1025 Constructing Roads In JGShining's Kingdom(DP+二分)
点我看题目 题意 :两条平行线上分别有两种城市的生存,一条线上是贫穷城市,他们每一座城市都刚好只缺乏一种物资,而另一条线上是富有城市,他们每一座城市刚好只富有一种物资,所以要从富有城市出口到贫穷城市, ...
- 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 ...
随机推荐
- The related functions and attributes for managing attributes - 操作属性的重要属性和函数
特性 property 都是类属性(静态变量),但是特性管理的其实是实例属性的存取, ****** 回顾 -'类方法' classmethod 和 '静态方法' staticmethod 皆可以访问类 ...
- LeetCode29 Medium 不用除号实现快速除法
本文始发于个人公众号:TechFlow,原创不易,求个关注 链接 Divide Two Integers 难度 Medium 描述 给定两个整数,被除数和除数,要求在不使用除号的情况下计算出两数的商 ...
- ELF文件之七——使用链接脚本-2个函数-data-bss-temp-call
main.c int enable; ; int main() { int temp; add(); ; } int add() { ; } o反汇编的地址都是0起始,elf的地址都是映射后的地址. ...
- MongoDB 复本集搭建
复制集的特点 数据一致性 主是唯一的,但不是固定的 没有MySQL那样的双主结构 大多数原则,集群存活节点小于等于二分之一时集群不可写,只可读. 是否能选举出新的主节点,是由当前复制集成员存活量 ...
- 让div充满整个body
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Jenkins 插件使用国内镜像源-解决插件下载慢的问题
问题 我们在Jenkins里面经常会遇到安装插件很慢,这是由于我们使用的是更新中心镜像默认为国外的源.现在我们可以进行设置为国内镜像源,来解决安装插件慢的问题. 解决办法 安装插件localizati ...
- python range函数(42)
在python中使用最多的除了print函数 就是 for循环 了,那么这里就不得不介绍一下python内置函数range函数! 一.range函数简介 python range函数可创建一个整数列表 ...
- mysql必知必会-创建高级联结
使用表别名 使用别名引用被检索的表列 别名除了用于列名和计算字段外,SQL还允许给表名起别名.这样做 有两个主要理由: 缩短SQL语句: 允许在单条 SELECT 语句中多次使用相同的表. 可以看到, ...
- Java线程池中线程的生命周期
设:我们有一个coreSize=10,maxSize=20,keepAliveTime=60s,queue=40 1.池初始化时里面没有任何线程. 2.当有一个任务提交到池就创建第一个线程. 3.若继 ...
- 整理了Linux常用命令变量
查看信息命令 ls 查看当前目录下面的所有文件 -a 显示所有文件(包括隐藏文件) -l 显示所有文件(包括文件的详细信息) 格式: ls 参数 目录路径(绝对/相对) cd 切换目录 格式: cd ...