HDU 1025-Constructing Roads In JGShining's Kingdom(最长不降子序列,线段树优化)
分析:
最长不降子序列,n很大o(n^2)肯定超,想到了小明序列那个题用线段树维护前面的最大值即可
该题也可用二分搜索来做。
注意问题输出时的坑,路复数后加s
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <string>
#include <cctype>
#include <complex>
#include <cassert>
#include <utility>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
typedef pair<int,int> PII;
typedef long long ll;
#define lson l,m,rt<<1
#define pi acos(-1.0)
#define rson m+1,r,rt<<1|1
#define All 1,N,1
#define N 500010
#define read freopen("in.txt", "r", stdin)
const ll INFll = 0x3f3f3f3f3f3f3f3fLL;
const int INF= 0x7ffffff;
const int mod = ;
struct city{
int p,r;
}t[N];
bool cmp(city x,city y){
return x.p<y.p;
}
int maxv[N*],n,dp[N];
void pushup(int rt){
maxv[rt]=max(maxv[rt<<],maxv[rt<<|]);
}
void build(int l,int r,int rt){
maxv[rt]=;
if(l==r){
return;
}
int m=(l+r)>>;
build(lson);
build(rson);
}
void update(int p,int v,int l,int r,int rt){
if(l==r){
maxv[rt]=max(maxv[rt],v);
return;
}
int m=(l+r)>>;
if(p<=m)update(p,v,lson);
else update(p,v,rson);
pushup(rt);
}
int query(int L,int R,int l,int r,int rt){
if(L<=l&&R>=r)
return maxv[rt];
int m=(l+r)>>;
int ans=;
if(L<=m)ans=max(ans,query(L,R,lson));
if(R>m)ans=max(ans,query(L,R,rson));
return ans;
}
int main()
{
int ca=;
while(~scanf("%d",&n)){
memset(dp,,sizeof(dp));
for(int i=;i<n;++i)
scanf("%d%d",&t[i].p,&t[i].r);
sort(t,t+n,cmp);
build(,n,);
int maxn=;
for(int i=;i<n;++i){
dp[i]=query(,t[i].r,,n,)+;
maxn=max(maxn,dp[i]);
update(t[i].r,dp[i],,n,);
}
printf("Case %d:\n",++ca);
if(maxn>)
printf("My king, at most %d roads can be built.\n",maxn);
else
printf("My king, at most %d road can be built.\n",maxn);
printf("\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(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(求最长上升子序列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 ...
随机推荐
- 用supervisor控制celery时的脚本
注意启停的先后顺序及判断即可. 这样,在更改task之后,要重启就方便很多啦.. #!/bin/sh supervisord_count=`ps -elf | grep celery | grep - ...
- QTP之web常用对象
web对象是我做自动化以来最早学习,最早接触的.对现在而言也是最熟悉不过的了,但是为了以后更稳健的前进,对基础的东西搞扎实,相信以后的路会顺畅许多,下边简单汇总下web的常用几类对象: Browser ...
- TortoiseSVN文件夹及文件图标不显示解决方法(转发)
地址:http://blog.csdn.net/lishehe/article/details/8257545 由于自己的电脑是win7(64位)的,系统安装TortoiseSVN之后,其他的功能都能 ...
- 使用CXF与Spring集成实现RESTFul WebService
以下引用与网络中!!! 一种软件架构风格,设计风格而不是标准,只是提供了一组设计原则和约束条件.它主要用于客户端和服务器交互类的软件.基于这个风格设计的软件可以更简洁,更有层次,更易于实现缓存 ...
- 【Linux高频命令专题(11)】cp
概述 cp命令用来复制文件或者目录,是Linux系统中最常用的命令之一.一般情况下,shell会设置一个别名,在命令行下复制文件时,如果目标文件已经存在,就会询问是否覆盖,不管你是否使用-i参数.但是 ...
- PCB板的价格是怎么算出来的?
Part 1 :影响一块PCB板价格的各种因素 PCB的价格是很多采购者一直很困惑的事情,很多人在线下单时也会疑问这些价格是怎么算出来的,下面我们就一起谈论一下PCB价格的组成因素. 1.PCB所用材 ...
- HDU 4351 Digital root 线段树区间合并
依然不是十分理解……待考虑…… #include <cstdio> #include <cstring> #include <cstdlib> #include & ...
- Android SDK +Eclipse+ADT+CDT+NDK 开发环境在windows 7下的搭建
Android SDK+Eclipse+ADT+CDT+NDK 开发环境在windows 7下的搭建 这几天一直在研究 Android SDK C/C++平台的搭建,尽管以前有成功在Windows ...
- How to learn linux device driver
To learn device driver development, like any other new knowledge, the bestapproach for me is to lear ...
- 面试题_93_to_102_编程和代码相关的面试题
93)怎么检查一个字符串只包含数字?(解决方案) 94)Java 中如何利用泛型写一个 LRU 缓存?(答案<) 95)写一段 Java 程序将 byte 转换为 long?(答案) 95)在不 ...