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

题意:有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. 基于visual Studio2013解决C语言竞赛题之1045打印成绩

       题目 解决代码及点评 /* 功能:用记录来描述一个学生的成绩情况,内容包括:姓名.学号.数学成绩和PASCAL成绩. 要求对一个小组的10个学生的成绩进行统计处理: 1)计算学生的总 ...

  2. 免解压版的Mysql的启动脚本,并且执行导入(windows)

    @echo off rem ################### set MYSQL_VERSION=mysql-5.5.32-win32 set LOCK=wot.lock rem ####### ...

  3. bootbox api

    bootbox是boostrap集成的弹窗,基本能完成后台系统的需求,下面是一些使用方法 1.bootbox.alert bootbox.alert使用方法主要有三种 直接传内容 bootbox.al ...

  4. 新发现的Cyberduck(映射网盘)和zsuncloud(硬件产品很新潮),群辉nas的确好用(购买链接)

    https://cyberduck.io/?l=en http://www.zsuncloud.com/ 群辉nas的确好用啊在哪里可以买到?官网 淘宝也可以自己做黑群晖 先用xpenoboot is ...

  5. 基于visual Studio2013解决面试题之0502字符串左移

     题目

  6. 基于visual Studio2013解决C语言竞赛题之1057打印加数

       题目 解决代码及点评 /* 功能: 已知N是正整数, 它可拆写成三个正整数N1.N2和N3之和的形式N=N1+N2+N3. 请编程序打印出满足上式的全部组合,并当N1.N2和N3中至 ...

  7. Linux chmod权限管理需要小心的地方

    档案的权限管理和简单,比如chmod 775 /tmp/test.sh 另外使用chmod +w /tmp/test.sh,会给档案的拥有者,群组,其他人的权限都加上了可编辑.这样就有安全隐患了.所以 ...

  8. POJ 2594 Treasure Exploration(最小路径覆盖变形)

    POJ 2594 Treasure Exploration 题目链接 题意:有向无环图,求最少多少条路径能够覆盖整个图,点能够反复走 思路:和普通的最小路径覆盖不同的是,点能够反复走,那么事实上仅仅要 ...

  9. Android控件拖动的实现

    这个也是从网上得到的代码,例子比较简单,但是如果有需要此功能的,这个例子可以提供很多提示,首先,给个截图 这个是拖动以后的效果,一个imageview和一个button控件,提供两份代码下载吧,一份是 ...

  10. Python 技巧

    1.根据路径导入模块 如果想引用指定路径下的某个模块,则需要使用sys.path.append("module_directory") 来把这个路径添加到sys下,这就涉及到Pyt ...