传送门:小明系列问题——小明序列

题意:有n个数,求间距大于d的最长上升序列。

分析:dp[i]表示在i点以a[i]结束距离大于d的最长上升序列,然后每更新到第i点时,取i-d之前小于a[i]的数为结束的最长上升序列进行状态转移,并维护前i-d之前的最大上升序列,维护i-d之前的每点为结束的最长上升序列用线段树维护即可。

#pragma comment(linker,"/STACK:1024000000,1024000000")
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <limits.h>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cstdlib>
#include <stack>
#include <vector>
#include <set>
#include <map>
#define LL long long
#define mod 100000000
#define inf 0x3f3f3f3f
#define eps 1e-6
#define N 100010
#define FILL(a,b) (memset(a,b,sizeof(a)))
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define PII pair<int,int>
using namespace std;
inline int read()
{
char ch=getchar();
int x=;
while(ch>''||ch<'')ch=getchar();
while(ch<=''&&ch>=''){x=x*+ch-'';ch=getchar();}
return x;
}
int dp[N],a[N];
int mx[N<<];
void pushup(int rt)
{
int ls=rt<<,rs=ls|;
mx[rt]=max(mx[ls],mx[rs]);
}
void build(int l,int r,int rt)
{
mx[rt]=;
if(l==r)return;
int m=(l+r)>>;
build(lson);
build(rson);
}
void update(int pos,int c,int l,int r,int rt)
{
if(l==r)
{
mx[rt]=max(mx[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 mx[rt];
int m=(l+r)>>;
int res=;
if(L<=m)res=max(res,query(L,R,lson));
if(m<R)res=max(res,query(L,R,rson));
return res;
}
int main()
{
int n,d;
while(scanf("%d%d",&n,&d)>)
{
build(,N,);
for(int i=;i<=n;i++)dp[i]=;
int ans=;
for(int i=;i<=n&&i<=d;i++)
{
//scanf("%d",&a[i]);
a[i]=read();
}
for(int i=d+;i<=n;i++)
{
//scanf("%d",&a[i]);
a[i]=read();
if(a[i]>)dp[i]=query(,a[i]-,,N,)+;
else dp[i]=;
update(a[i-d],dp[i-d],,N,);
ans=max(ans,dp[i]);
}
printf("%d\n",ans);
}
}

hdu4521(线段树+dp)的更多相关文章

  1. HDU4521+线段树+dp

    题意:在一个序列中找出最长的某个序列.找出的序列满足题中的条件. 关键:对于 第 i 个位置上的数,要知道与之相隔至少d的位置上的数的大小.可以利用线段树进行统计,查询.更新的时候利用dp的思想. / ...

  2. Tsinsen A1219. 采矿(陈许旻) (树链剖分,线段树 + DP)

    [题目链接] http://www.tsinsen.com/A1219 [题意] 给定一棵树,a[u][i]代表u结点分配i人的收益,可以随时改变a[u],查询(u,v)代表在u子树的所有节点,在u- ...

  3. HDU 3016 Man Down (线段树+dp)

    HDU 3016 Man Down (线段树+dp) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Ja ...

  4. lightoj1085 线段树+dp

    //Accepted 7552 KB 844 ms //dp[i]=sum(dp[j])+1 j<i && a[j]<a[i] //可以用线段树求所用小于a[i]的dp[j ...

  5. [CF 474E] Pillars (线段树+dp)

    题目链接:http://codeforces.com/contest/474/problem/F 意思是给你两个数n和d,下面给你n座山的高度. 一个人任意选择一座山作为起始点,向右跳,但是只能跳到高 ...

  6. HDU-3872 Dragon Ball 线段树+DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3872 题意:有n个龙珠按顺序放在一列,每个龙珠有一个type和一个权值,要求你把这n个龙珠分成k个段, ...

  7. Codeforces Round #343 (Div. 2) D - Babaei and Birthday Cake 线段树+DP

    题意:做蛋糕,给出N个半径,和高的圆柱,要求后面的体积比前面大的可以堆在前一个的上面,求最大的体积和. 思路:首先离散化蛋糕体积,以蛋糕数量建树建树,每个节点维护最大值,也就是假如节点i放在最上层情况 ...

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

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

  9. hdu 4117 GRE Words (ac自动机 线段树 dp)

    参考:http://blog.csdn.net/no__stop/article/details/12287843 此题利用了ac自动机fail树的性质,fail指针建立为树,表示父节点是孩子节点的后 ...

随机推荐

  1. HttpURLConnection中使用代理(Proxy)及其验证(Authentication)

    HttpURLConnection中使用代理(Proxy)及其验证(Authentication) 使用Java的HttpURLConnection类可以实现HttpClient的功能,而不需要依赖任 ...

  2. Javascript关闭详细说明

    在我的博客:http://blog.csdn.net/u011043843/article/details/26148265中也有对闭包的解释 在javascript中闭包是一个非常不好理解的概念.可 ...

  3. Javascript跨域请求的几种解决方法

    什么情况下才会出现跨域? 假设域名是:http://www.example.com.cn/ 如果所请求的域名跟这个域名不致,这种情况就是跨域,由于跨域存在漏洞,所以一般来说正常的跨域请求方式是请求不到 ...

  4. select的种种取值

    今天别人问我一个问题 <body> <select id="tests" onchange="test()"> <option & ...

  5. webdynpro tree控件使用

    1.  首先创建一个TREE控件 2.  在Tree下面创建一个TREE  NODE  TYPE ,node type 是可以继续展开的,而Item type是无法展开的. 3. 创建node.,下面 ...

  6. POJ2782:Bin Packing

    Description   A set of n<tex2html_verbatim_mark> 1-dimensional items have to be packed in iden ...

  7. SpringMVC之Controller传递JSON数据到页面

    在Controller中,组装好JSON格式的数据,然后输入到页面,或者通过ajax请求在页面进行解析,都可以做到. 1.Controller /** * JSON DATA TO PAGE VEIW ...

  8. SMART rule之个人理解

    SMART原则通常应用于绩效评估中. S是specific的缩写,也就是你所设定的目标必须是一个具体的目标,而不是一个空而大的东西. 比如你的目标定位为今年学习linux,并对进程管理.内存管理等要能 ...

  9. 记一个手游app数据文件的破解

    出于一些非常猥琐的须要,同一时候自己也想做一些新奇的尝试,周末用了大半天时间破解了某款手游的数据文件. 过程比我预想的要顺利,主要原因还是我们开发者的懈怠.咳咳. 步骤例如以下: 下载安装包,解压,发 ...

  10. ASP.NET - 分页

    效果: SQL-存储过程(Paging): ROW_NUMBER() over(order by MessageDateTime desc)  其中的 MessageDateTime desc 代表的 ...