题意:求最长上升序列的长度(LIS),但是要求相邻的两个数距离至少为d,数据范围较大,普通dp肯定TLE。线段树搞之就可以了,或者优化后的nlogn的dp。

代码为  线段树解法。

 #include <set>
#include <map>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const double eps = 1e-;
const int maxn = 1e5+;
int dp[maxn],seg[maxn<<]; //线段树区间求最值
int a[maxn];
void update(int l,int r,int pos,int x,int val)
{
if (l == r)
{
seg[pos] = val;
return ;
}
int mid = (l + r) >> ;
if (x <= mid)
update(l,mid,pos<<,x,val);
if (x > mid)
update(mid+,r,pos<<|,x,val);
seg[pos] = max(seg[pos<<],seg[pos<<|]);
}
int query(int l,int r,int pos,int x,int y)
{
if (x <= l && y >= r)
return seg[pos];
int mid = (l + r) >> ;
int ans1 = ,ans2 = ;
if (x <= mid)
ans1 = query(l,mid,pos<<,x,y);
if (y > mid)
ans2 = query(mid+,r,pos<<|,x,y);
return max(ans1,ans2);
}
int main(void)
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
int n,d;
while (~scanf ("%d%d",&n,&d))
{
memset(seg,,sizeof(seg));
int len = ;
for (int i = ; i < n; i++)
{
scanf ("%d",a+i);
a[i] += ; //因为a[i]可能为0,而我的线段树是从1开始的所以加2
len = max(len,a[i]);
}
int ans = ;
for (int i = ; i < n; i++)
{
dp[i] = query(,len,,,a[i]-) + ; //不能等于,所以减1,dp[i]表示以a[i]结尾的最长不降序列长度,
if (i >= d) //延迟更新,这是这道题的关键,
update(,len,,a[i-d],dp[i-d]);
ans = max(dp[i],ans);
}
printf("%d\n",ans);
}
return ;
}

hdu4521-小明系列问题——小明序列(线段树区间求最值)的更多相关文章

  1. 【codevs2216】行星序列 线段树 区间两异同修改+区间求和*****

    [codevs2216]行星序列 2014年2月22日3501 题目描述 Description “神州“载人飞船的发射成功让小可可非常激动,他立志长大后要成为一名宇航员假期一始,他就报名参加了“小小 ...

  2. Wannafly 挑战赛22 D 整数序列 线段树 区间更新,区间查询

    题目链接:https://www.nowcoder.com/acm/contest/160/D 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 262144K,其他语言524288K ...

  3. COGS.1272.[AHOI2009]行星序列(线段树 区间加、乘、求和)

    题目链接 //注意取模! #include<cstdio> #include<cctype> using namespace std; const int N=1e5+5; i ...

  4. hdu----(4521)小明系列问题——小明序列

    小明系列问题——小明序列 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Tota ...

  5. 2018.07.08 hdu4521 小明系列问题——小明序列(线段树+简单dp)

    小明系列问题--小明序列 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) Proble ...

  6. HDU-4521 小明系列问题——小明序列 间隔限制最长上升子序列

    题意:给定一个长度为N的序列,现在要求给出一个最长的序列满足序列中的元素严格上升并且相邻两个数字的下标间隔要严格大于d. 分析: 1.线段树 由于给定的元素的取值范围为0-10^5,因此维护一棵线段树 ...

  7. 小明系列问题――小明序列(LIS)

    小明系列问题――小明序列 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit ...

  8. 小明系列问题——小明序列(Lis 相距大于d的单调上升子序列)

    小明系列问题——小明序列 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Tot ...

  9. hdu 4521 小明系列问题——小明序列 线段树+二分

    小明系列问题——小明序列 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Pro ...

随机推荐

  1. java中post和get请求

    示例代码: package com.shareboxes.util; import java.io.BufferedReader; import java.io.IOException; import ...

  2. 数据库版本管理工具Flyway(4.0.3)---工作机制(译文)

    How Flyway works The easiest scenario is when you point Flyway to an empty database. 最容易的方案是Flyway指向 ...

  3. pic_for_youdao

  4. (转)在SAE使用Apple Push Notification Service服务开发iOS应用, 实现消息推送

    在SAE使用Apple Push Notification Service服务开发iOS应用, 实现消息推送 From: http://saeapns.sinaapp.com/doc.html 1,在 ...

  5. java.lang.OutOfMemoryError: Java heap space错误及处理办法(收集整理、转)

    下面是从网上找到的关于堆空间溢出的错误解决的方法: java.lang.OutOfMemoryError: Java heap space ============================== ...

  6. 【转】MVP和MVC的区别

    转自:http://www.cnblogs.com/end/archive/2011/06/02/2068512.html MVC和MVP到底有什么区别呢? 从这幅图可以看到,我们可以看到在MVC里, ...

  7. css选择器基本属性

    选择器一,相邻选择器: 1,相邻选择器 1),定义:相邻选择器匹配指定元素的相邻兄弟元素 2),用法:如果需要选择紧接在另一个元素后的元素,而且二者有相同的父元素,可以使用相邻兄弟选择器 3),表示符 ...

  8. css属性之vertical-align详解

    inline-block 该值会让元素生成一个内联级块容器(inline-level block container).一个inline-block的内部会被格式化成一个块盒,而该元素本身会被格式化成 ...

  9. github 预览html

    在网址前加 http://htmlpreview.github.io/?

  10. 委托与Lambda-浅谈

    委托概述 委托是寻址方法的.NET版本. 在C++中,函数指针只不过是一个指向内存位置的指针,它不是类型安全的.我们无法判断这个指针实际指向什么,更不知晓像参数和返回类型等项了. 而.NET委托完全不 ...