题目:给出一个序列,找出一个最长的子序列,相邻的两个数的差在d以内。

 /*
线段树优化dp
dp[i]表示前i个数的最长为多少,则dp[i]=max(dp[j]+1) abs(a[i]-a[j])<=d
复杂度为O(n ^ 2)
利用线段树优化,线段树保存区间最大值。离散化后便可求出,还要注意 对于叶子节点保存的即为dp的值,每次更改即可,开始一直累加。。。。。
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm> using namespace std;
#define lson l,m,rt<<1
#define rson m + 1, r, rt<<1|1
const int maxn = 1e5 + ;
int s[maxn];
int n, d;
int san[maxn], tot;
int sum[maxn << ];
void pushUp(int rt){
sum[rt] = max(sum[rt<<], sum[rt<<|]);
}
void update(int pos, int c, int l, int r, int rt){
if (l == r){
sum[rt] = c;//注意
return ;
}
int m = (l + r) >> ;
if (pos <= m) update(pos, c, lson);
else update(pos, c, rson);
pushUp(rt);
}
int query(int L, int R, int l, int r, int rt){
if (L <= l && R >= r){
return sum[rt];
}
int m = (l + r) >> ;
int ret = ;
if (L <= m) ret = query(L, R, lson);
if (R > m) ret = max(ret, query(L, R, rson));
return ret;
}
int main(){
while (~scanf("%d%d", &n, &d)){
tot = ;
for (int i = ; i <= n; ++i){
scanf("%d", &s[i]);
san[tot++] = s[i];
}
sort(san, san + tot);
tot = unique(san, san + tot) - san; memset(sum, , sizeof(sum));
int ans = ;
for (int i = ; i <= n; ++i){
int pos = lower_bound(san, san + tot, s[i]) - san + ;
int l = lower_bound(san, san + tot, s[i] - d) - san + ;
int r = upper_bound(san, san + tot, s[i] + d) - san;
int que = query(l, r, , tot, ) + ;
//cout << " l = " << l << " r = " << r << endl;
ans = max(ans, que);
//cout << " ans = " << ans << endl;
update(pos, que, , tot, );
}
printf("%d\n", ans);
}
return ;
}

zoj 3349 dp + 线段树优化的更多相关文章

  1. [USACO2005][POJ3171]Cleaning Shifts(DP+线段树优化)

    题目:http://poj.org/problem?id=3171 题意:给你n个区间[a,b],每个区间都有一个费用c,要你用最小的费用覆盖区间[M,E] 分析:经典的区间覆盖问题,百度可以搜到这个 ...

  2. HDU4719-Oh My Holy FFF(DP线段树优化)

    Oh My Holy FFF Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) T ...

  3. UVA-1322 Minimizing Maximizer (DP+线段树优化)

    题目大意:给一个长度为n的区间,m条线段序列,找出这个序列的一个最短子序列,使得区间完全被覆盖. 题目分析:这道题不难想,定义状态dp(i)表示用前 i 条线段覆盖区间1~第 i 线段的右端点需要的最 ...

  4. 完美字符子串 单调队列预处理+DP线段树优化

    题意:有一个长度为n的字符串,每一位只会是p或j.你需要取出一个子串S(注意不是子序列),使得该子串不管是从左往右还是从右往左取,都保证每时每刻已取出的p的个数不小于j的个数.如果你的子串是最长的,那 ...

  5. Contest20140906 ProblemA dp+线段树优化

    Problem A 内存限制 256MB 时间限制 5S 程序文件名 A.pas/A.c/A.cpp 输入文件 A.in 输出文件 A.out 你有一片荒地,为了方便讨论,我们将这片荒地看成一条直线, ...

  6. POJ 3171.Cleaning Shifts-区间覆盖最小花费-dp+线段树优化(单点更新、区间查询最值)

    Cleaning Shifts Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4721   Accepted: 1593 D ...

  7. 题解 HDU 3698 Let the light guide us Dp + 线段树优化

    http://acm.hdu.edu.cn/showproblem.php?pid=3698 Let the light guide us Time Limit: 5000/2000 MS (Java ...

  8. 省选模拟赛 4.26 T1 dp 线段树优化dp

    LINK:T1 算是一道中档题 考试的时候脑残了 不仅没写优化 连暴力都打挂了. 容易发现一个性质 那就是同一格子不会被两种以上的颜色染.(颜色就三种. 通过这个性质就可以进行dp了.先按照左端点排序 ...

  9. 【uva1502/hdu4117-GRE Words】DP+线段树优化+AC自动机

    这题我的代码在hdu上AC,在uva上WA. 题意:按顺序输入n个串以及它的权值di,要求在其中选取一些串,前一个必须是后一个的子串.问d值的和最大是多少. (1≤n≤2×10^4 ,串的总长度< ...

随机推荐

  1. C语言经典算法五个人问岁数!——————【Badboy】

    有5 个人坐在一起,问第五个人多少岁?他说比第4 个人大2 岁.问第4 个人岁数.他说比第3 个人大2 岁.问第三个人,又说比第2 人大两岁.问第2 个人.说比第一个人大两岁.最后问第一个人.他说是1 ...

  2. 关于Linux开源项目基础组件make编译流程

     关于Linux开源项目基础组件make编译流程 非常多Linux开源项目都会用到编译出可运行文件的make.这个是有一套流程的. 首先,GNU构建系统:https://en.wikipedia. ...

  3. Android中的Service组件具体解释

    Service与Activity的差别在于:Service一直在后台执行,他没实用户界面,绝不会到前台来. 一,创建和配置Service 开发Service须要两个步骤:1.继承Service子类,2 ...

  4. Appstore 提交时错误

      ERROR ITMS-9000:This boundle is invalid. New apps and app updates submitted to the App Store must  ...

  5. Oracle 和sqlserver 字符串补齐

    Oracle:Lpad函数 语法格式如下: lpad( string, padded_length, [ pad_string ] ) string 准备被填充的字符串: padded_length ...

  6. 《我是一只IT小小鸟》(胡江堂主编)读后感

    http://blog.csdn.net/wojiushiwo987/article/details/8685539<我是一只IT小小鸟>(胡江堂主编)读后感 2011年下半年研二的时候, ...

  7. Eclipse安装Properties Editore插件

    Properties Editor for Eclipse3[1].0-3.2安装使用-http://jzgl-javaeye.iteye.com/blog/386010 PropertiesEdit ...

  8. Win7与虚拟机Linux互通ping的网络设置

    转载请标明出处:http://www.linuxidc.com/Linux/2014-04/100450.htm 虽然从WinXP到Win7一直都可以使用VMWARE虚拟机安装Linux系统,记得每次 ...

  9. linux-c 调试 gdb

    GDB(GNU Debugger) gcc -g –o testarg testarg.c //可执行文件中带上调试信息,用于后续的gdb调试 gdb testarg l; list //显示源程序 ...

  10. CentOS6.5配置PHP CI程序

    步骤: 1.安装CentOS6.5系统:     1.选择PHP+Mysql环境 2.关闭防火墙和SeLinux     1.chkconfig --level 35 iptables off     ...