hdu4521(线段树+dp)
传送门:小明系列问题——小明序列
题意:有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)的更多相关文章
- HDU4521+线段树+dp
题意:在一个序列中找出最长的某个序列.找出的序列满足题中的条件. 关键:对于 第 i 个位置上的数,要知道与之相隔至少d的位置上的数的大小.可以利用线段树进行统计,查询.更新的时候利用dp的思想. / ...
- Tsinsen A1219. 采矿(陈许旻) (树链剖分,线段树 + DP)
[题目链接] http://www.tsinsen.com/A1219 [题意] 给定一棵树,a[u][i]代表u结点分配i人的收益,可以随时改变a[u],查询(u,v)代表在u子树的所有节点,在u- ...
- HDU 3016 Man Down (线段树+dp)
HDU 3016 Man Down (线段树+dp) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...
- lightoj1085 线段树+dp
//Accepted 7552 KB 844 ms //dp[i]=sum(dp[j])+1 j<i && a[j]<a[i] //可以用线段树求所用小于a[i]的dp[j ...
- [CF 474E] Pillars (线段树+dp)
题目链接:http://codeforces.com/contest/474/problem/F 意思是给你两个数n和d,下面给你n座山的高度. 一个人任意选择一座山作为起始点,向右跳,但是只能跳到高 ...
- HDU-3872 Dragon Ball 线段树+DP
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3872 题意:有n个龙珠按顺序放在一列,每个龙珠有一个type和一个权值,要求你把这n个龙珠分成k个段, ...
- Codeforces Round #343 (Div. 2) D - Babaei and Birthday Cake 线段树+DP
题意:做蛋糕,给出N个半径,和高的圆柱,要求后面的体积比前面大的可以堆在前一个的上面,求最大的体积和. 思路:首先离散化蛋糕体积,以蛋糕数量建树建树,每个节点维护最大值,也就是假如节点i放在最上层情况 ...
- Special Subsequence(离散化线段树+dp)
Special Subsequence Time Limit: 5 Seconds Memory Limit: 32768 KB There a sequence S with n inte ...
- hdu 4117 GRE Words (ac自动机 线段树 dp)
参考:http://blog.csdn.net/no__stop/article/details/12287843 此题利用了ac自动机fail树的性质,fail指针建立为树,表示父节点是孩子节点的后 ...
随机推荐
- 基于visual Studio2013解决面试题之1403插入排序
题目
- 在JS中,一个自定义函数如何调用另一个自定义函数中的变量
function aa1511() { var chengshi="马鞍山"; var shengfen="安徽省"; return shengfen+&quo ...
- ajax终结篇
Ajax中post和get的区别 在ajax中有这个方法 xmlreq.open("post","servlet/MyServlet?time="+newDat ...
- javascript 偏移量
通过四个属性可以获得元素的偏移量: 1.offsetHeight: 元素在垂直方向上占用的空间的大小,(像素).包括元素的高度,(可见的)水平滚动条的高度,上边框高度和下边框高度. 2.offsetW ...
- 【视频】零基础学Android开发:蓝牙聊天室APP(四)
零基础学Android开发:蓝牙聊天室APP第四讲 4.1 ListView控件的使用 4.2 BaseAdapter具体解释 4.3 ListView分布与滚动事件 4.4 ListView事件监听 ...
- mysql-5.6.13在windows平台下的安装、使用(图解)
本文同步至:http://www.waylau.com/mysql-5-6-13-windows-platform-installation-use-graphic/ 一. 首先电脑要具备.Net F ...
- sharepoint 2010 在自定义列表的字段上增加功能菜单
sharepoint 2010 在自定义列表的字段上增加功能菜单方法 打开sharepoint designer 2010,找到需要修改的视图页面,例如allitem.aspx,编辑这个页面,点击高级 ...
- (原创)优酷androidclient 下载中 bug 解决
在网络情况不好的情况下,优酷androidclient下载视频会终止,用户放弃下载点击 删除该任务以后,切换到网络好的情况下进行下载,会显示该视频已在下载队列里,然后clientUI界面却什么都看不到 ...
- Python集成开发环境(Eclipse+Pydev)
刚開始学习python,就用Editplus, Notepad++来写小程序, 后来接触了Sublime Text2.认为很不错,没事写写代码.就用编辑器Sublime Text2,最好再配搭一个ap ...
- Nginx负载均衡:分布式/热备Web Server的搭建
Nginx是一款轻量级的Web server/反向代理server及电子邮件(IMAP/POP3)代理server.并在一个BSD-like 协议下发行.由俄罗斯的程序设计师Igor Sysoev所开 ...