HDU 2836 只不过改成了求最长子串。

DP+线段树单点修改+区间查最值。

 #include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm> #define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1 using namespace std; const int MAXN = ; int n, d;
int val[MAXN];
int num[MAXN];
int dp[MAXN]; int maxi[ MAXN << ]; void pushUp( int rt )
{
maxi[rt] = max( maxi[rt << ], maxi[rt << | ] );
return;
} void Update( int L, int c, int l, int r, int rt )
{
if ( l == L && L == r )
{
maxi[rt] = c;
return;
} int m = ( l + r ) >> ;
if ( L <= m ) Update( L, c, lson );
else Update( L, c, rson );
pushUp( rt );
return;
} int Query( int L, int R, int l, int r, int rt )
{
if ( L <= l && r <= R )
return maxi[rt]; int m = ( l + r ) >> ; int res = ;
if ( L <= m ) res = max( res, Query( L, R, lson ) );
if ( R > m ) res = max( res, Query( L, R, rson ) ); return res;
} int main()
{
while ( ~scanf( "%d%d", &n, &d ) )
{
for ( int i = ; i <= n; ++i )
{
scanf( "%d", &val[i] );
num[i] = val[i];
} sort( num + , num + + n );
int cnt = unique( num + , num + n + ) - num - ; dp[] = ;
memset( maxi, , sizeof(maxi) );
int ans = ; for ( int i = ; i <= n; ++i )
{
int id = lower_bound( num + , num + cnt + , val[i] ) - num;
int left = lower_bound( num + , num + cnt + , val[i] - d ) - num;
int right = upper_bound( num + , num + cnt + , val[i] + d ) - num - ;
dp[i] = Query( left, right, , n, ) + ;
ans = max( ans, dp[i] );
Update( id, dp[i], , n, );
}
printf( "%d\n", ans );
}
return ;
}

ZOJ 3349 Special Subsequence 简单DP + 线段树的更多相关文章

  1. Special Subsequence(离散化线段树+dp)

    Special Subsequence Time Limit: 5 Seconds      Memory Limit: 32768 KB There a sequence S with n inte ...

  2. ZOJ 3349 Special Subsequence

    Special Subsequence Time Limit: 5000ms Memory Limit: 32768KB This problem will be judged on ZJU. Ori ...

  3. codeforces750E New Year and Old Subsequence 矩阵dp + 线段树

    题目传送门 思路: 先看一个大牛的题解 题解里面对矩阵的构造已经写的很清楚了,其实就是因为在每个字符串都有固定的很多中状态,刚好可以用矩阵来表达,所以$(i,j)$这种状态可以通过两个相邻的矩阵的$m ...

  4. 线段树:CDOJ1591-An easy problem A (RMQ算法和最简单的线段树模板)

    An easy problem A Time Limit: 1000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Pr ...

  5. [Codeforces 280D]k-Maximum Subsequence Sum(线段树)

    [Codeforces 280D]k-Maximum Subsequence Sum(线段树) 题面 给出一个序列,序列里面的数有正有负,有两种操作 1.单点修改 2.区间查询,在区间中选出至多k个不 ...

  6. hdu 3016 dp+线段树

    Man Down Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total S ...

  7. cf834D(dp+线段树区间最值,区间更新)

    题目链接: http://codeforces.com/contest/834/problem/D 题意: 每个数字代表一种颜色, 一个区间的美丽度为其中颜色的种数, 给出一个有 n 个元素的数组, ...

  8. 「CQOI2006」简单题 线段树

    「CQOI2006」简单题 线段树 水.区间修改,单点查询.用线段树维护区间\([L,R]\)内的所有\(1\)的个数,懒标记表示为当前区间是否需要反转(相对于区间当前状态),下方标记时懒标记取反即可 ...

  9. Codeforces Round #620 F2. Animal Observation (hard version) (dp + 线段树)

    Codeforces Round #620 F2. Animal Observation (hard version) (dp + 线段树) 题目链接 题意 给定一个nm的矩阵,每行取2k的矩阵,求总 ...

随机推荐

  1. 无法解析的外部符号 _WinMain@16 fatal error LNK1120: 1 个无法解析的外部命令

    一,问题描述MSVCRTD.lib(crtexew.obj) : error LNK2019: 无法解析的外部符号 _WinMain@16,该符号在函数 ___tmainCRTStartup 中被引用 ...

  2. 【Timers】win服务 定时器

    win服务: using System.ServiceProcess; 继承 ServiceBase 即可 定时器: private System.Timers.Timer PushOrderTime ...

  3. ios6 处理内存警告

    iPhone下每个app可用的内存是被限制的,如果一个app使用的内存超过20M,则系统会向该app发送Memory Warning消息.收到此消息后,app必须正确处理,否则可能出错或者出现内存泄露 ...

  4. Python实战(2)

    在安装python第三方插件库的时候遇到了这个错误 遇到这种问题可以”转战“国内的第三方镜像,问题便可迎刃而解.例如豆瓣镜像——http://pypi.douban.com/simple/ 先安装ea ...

  5. java 词法分析器

    参考:http://www.cnblogs.com/yanlingyin/archive/2012/04/17/2451717.html 实现了一个简单的java词法分析器 功能:词法分析下面一段ja ...

  6. COCOS2DX2.2.2 创建CCEditBox输入框架实现文本及密码输入

    本文转载于: http://5.quanpao.com/?p=561 使用CCEditBox需要启用扩展库既extension ,因此需要引入这个空间名 有两种方法, using namespace ...

  7. iOS中 @synthesize 和 @dynamic

    今天写点过时的东西,我记得  这个是xcode 4 那个年代的事情了,没想到有时候大家还会被问到.可能目的就是看看你是从是么时候才开始接触iOS的. 在声明property属性后,有2种实现选择 @s ...

  8. 贱贱的美团安卓客户端---如何实现让安卓app在应用列表获得较靠前的位置

    起因: 自打愚安我开始使用android设备以来,一直觉得google还算厚道,应用列表里的顺序一直都是依据APP的名称,按照先中文(拼音字母表顺序),后英文(字母表顺序)的原则进行排序的,并没有说G ...

  9. 《C++Primer》复习——with C++11 [2]

    1.数组引用形参,C++允许将变量定义成数组的引用,给予同样的道理,形参也可以是数组的引用,此时引用形参绑定到对应的实参上,也就是绑定到数组上 ]) { for (auto elem : arr) c ...

  10. 3157: 国王奇遇记 & 3516: 国王奇遇记加强版 - BZOJ

    果然我数学不行啊,题解君: http://www.cnblogs.com/zhuohan123/p/3726933.html const h=; var fac,facinv,powm,s:..]of ...