ZOJ 3349 Special Subsequence 简单DP + 线段树
同 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 + 线段树的更多相关文章
- Special Subsequence(离散化线段树+dp)
Special Subsequence Time Limit: 5 Seconds Memory Limit: 32768 KB There a sequence S with n inte ...
- ZOJ 3349 Special Subsequence
Special Subsequence Time Limit: 5000ms Memory Limit: 32768KB This problem will be judged on ZJU. Ori ...
- codeforces750E New Year and Old Subsequence 矩阵dp + 线段树
题目传送门 思路: 先看一个大牛的题解 题解里面对矩阵的构造已经写的很清楚了,其实就是因为在每个字符串都有固定的很多中状态,刚好可以用矩阵来表达,所以$(i,j)$这种状态可以通过两个相邻的矩阵的$m ...
- 线段树:CDOJ1591-An easy problem A (RMQ算法和最简单的线段树模板)
An easy problem A Time Limit: 1000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Pr ...
- [Codeforces 280D]k-Maximum Subsequence Sum(线段树)
[Codeforces 280D]k-Maximum Subsequence Sum(线段树) 题面 给出一个序列,序列里面的数有正有负,有两种操作 1.单点修改 2.区间查询,在区间中选出至多k个不 ...
- hdu 3016 dp+线段树
Man Down Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...
- cf834D(dp+线段树区间最值,区间更新)
题目链接: http://codeforces.com/contest/834/problem/D 题意: 每个数字代表一种颜色, 一个区间的美丽度为其中颜色的种数, 给出一个有 n 个元素的数组, ...
- 「CQOI2006」简单题 线段树
「CQOI2006」简单题 线段树 水.区间修改,单点查询.用线段树维护区间\([L,R]\)内的所有\(1\)的个数,懒标记表示为当前区间是否需要反转(相对于区间当前状态),下方标记时懒标记取反即可 ...
- Codeforces Round #620 F2. Animal Observation (hard version) (dp + 线段树)
Codeforces Round #620 F2. Animal Observation (hard version) (dp + 线段树) 题目链接 题意 给定一个nm的矩阵,每行取2k的矩阵,求总 ...
随机推荐
- oracle-12c-rac 报:ORA-01078
OS: Oracle Linux Server release 5.7 DB: Oracle Database 12c Enterprise Edition Release 12.1.0.1.0 - ...
- ios系统 处理内存警告
iPhone下每个app可用的内存是被限制的,如果一个app使用的内存超过20M,则系统会向该app发送Memory Warning消息.收到此消息后,app必须正确处理,否则可能出错或者出现内存泄露 ...
- Sublime Text 2 入门
SublimeText 2 的介绍视频: http://player.youku.com/player.php/partnerid/XOTcy/sid/XMzU5NzQ5ODgw/v.swf 以下 ...
- SharePoint 优化显示WebParts
在开发sharepoint中,经常遇到需要自定义显示列表中的一部分作为导航的内容, 如公告栏,新闻链接,最新动态等.... 我们通常需要显示一个列表的标题,并且限制字符长度, 外加一些条件,如按创建的 ...
- SQL Server数据库文件存储目录转移
USE master GO DECLARE @DBName sysname, ) DECLARE @DB table( name sysname, physical_name sysname) BEG ...
- 有n个人围成一圈,顺序排号。从第一个人开始报数(从1到3报数),凡报到3的人退出圈子,问最后留下的是原来第几号的那位。
先写我的思路,没有用指针的做法.如果你用的是VC,把第六行去掉. #include<stdio.h> #include<stdlib.h> int main() { setvb ...
- NGUI屏幕自适应解决方案
NGUI研究院之自适应屏幕 http://www.xuanyusong.com/archives/2536 Unity3D研究院之使用Android的硬件缩放技术优化执行效率 http://www.x ...
- DIY Ruby CPU 分析——Part I
[编者按]原文作者 Emil Soman,Rubyist,除此之外竟然同时也是艺术家,吉他手,Garden City RubyConf 组织者.本文是DIY Ruby CPU Profiling 的第 ...
- 【设计模式六大原则1】单一职责原则(Single Responsibility Principle)
http://blog.csdn.net/zhengzhb/article/category/926691/1 图片素材来源,java学习手册 ps.内容为自己整理 定义:不要存在多于一个 ...
- What are Scopes?
scope is an object that refers to the application model. It is an execution context for expressions. ...