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指针建立为树,表示父节点是孩子节点的后 ...
随机推荐
- perl $d = encode_utf8($r); $f = decode_json($d)
[root@dr-mysql01 ~]# cat a1.pl my $url="http://192.168.32.101:3000/api/getcode?env=zj&phone ...
- 基于visual Studio2013解决C语言竞赛题之1041反向打印
题目 解决代码及点评 /* 功能:将一个整数(最多是10位数)从低位到高位打印出来, 如该数是12345时,输出应是54321(只占一行) 时间:19:18 201 ...
- µC/OS-II版本升级指南
IDE: MDK V4+ MCU: LPC17xx(Cortex-M3) RTOS: µC/OS-II 升级顺序:V2.52->V2.62->V2.76-> ...
- MIPI CSI-2规范一——概述及层级
MIPI CSI-2规范一——概述及层级 CSI-2概述 CSI-2规范定义了发送者和接收者之间传输和控制接口的标准数据.数据传输接口(指CSI-2)是单向差分串行接口,传输数据和始终信号:接口的物理 ...
- C#、WinForm、ASP.NET - SQLHelper.cs
SQLHelper.cs using System; using System.Data; using System.Configuration; using System.Data.SqlClien ...
- 自己用h5写的转盘。写贴上来吧。
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- 百度地图SDK for Android v2.1.2全新发布
2013年6月20日 Android SDK:V2.1.2产品上线 新增: 自定义指南针位置(类:MapController,方法:setCompassMargin) 自定义当前位置图标(类:MyLo ...
- Effective C++_笔记_条款08_别让异常逃离析构函数
(整理自Effctive C++,转载请注明.整理者:华科小涛@http://www.cnblogs.com/hust-ghtao/) C++并不禁止析构函数吐出异常,但它不鼓励你这样做.考虑如下代码 ...
- EasyUI - 要引入的JS文件
引入的JS: 使用时候修改路径. <script src="../Quote/EasyUI/locale/easyui-lang-zh_CN.js"></scri ...
- UpdateWindow API函数的作用很明显
待续 摘自<Delphi深度历险>