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 ...
随机推荐
- SQO2008配置管理工具服务显示远程过程调用失败
前两天,装了VS2012后,打开SQL2008配置管理工具,发现SQL服务名称里什么也没有,只有一个提示:(如图) 卸载了一个叫"Microsoft SQL Server 2012Local ...
- maven 命令备忘
1. 打包时 不执行测试 mvn package -Dmaven.test.skip=true
- hdu1068 Girls and Boys
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1068 二分图的最大独立集数=节点数(n)— 最大匹配数(m) 另外需要注意的是: 本题求出的最大匹配数是实 ...
- dubbo与zookeeper安装手册
原文 示例提供者安装 (+) (#) 安装: wget http://code.alibabatech.com/mvn/releases/com/alibaba/dubbo-demo-provider ...
- Java学习笔记之:Java简介
一.引言 Java是由Sun Microsystems公司于1995年5月推出的Java面向对象程序设计语言和Java平台的总称.由James Gosling和同事们共同研发,并在1995年正式推出. ...
- Qt出现警告 Unescaped backslashes are deprecated!解决办法
Fixing Qt Warning: Unescaped backslashes are deprecated! From: http://www.openguru.com/2011/10/fixin ...
- MFC编程入门
一. 什么是MFC? 如果你要建立一个Windows应用程序,应该如何下手? 好的开端是从设计用户界面开始. 首先,你要决定什么样的用户能使用该程序并根据需要来设置相应的用户界面对象.Windows用 ...
- 【转载】git/github初级运用自如
之前了解过github,并在上面看了一些项目的源代码,于是自己也在github上创建了账户,希望以后有机会也把自己的项目托管在上面去.但是前提你要先了解git/github,下面的内容是从我的好基友虫 ...
- 10个提供免费PHP脚本下载的网站
本文将重点介绍10个PHP脚本的免费资源下载站.之前推荐 <16个下载超酷脚本的热门网站>,这些网站除了PHP脚本,还有JavaScript.Java.Perl.ASP等脚本.如果你已是脚 ...
- oracle 11g SQL Developer instead of isqlplus
Oracle11g的新工具SQL DEVELOPER,替代了 isqlplus 1.创建连接方式 2.SQL输入窗口 3.表的结构及其他信息查阅 4.SQL语句测试 5.创建表 6.用户授权 7.数据 ...