题目大意:给出一串长度为n的整数串,求最长的一个连续子序列,满足该序列中最大的元素与最小的元素之差大于等于m, 并且小于等于k。n<=100000

分析:维护两个单调队列,一个递增的,维护最小值,一个递减的,维护最大值。设f[i]表示当前i所在区间的长度,若a[i]在最大值和最小值之间,则当前元素可以加入到上一个元素的区间中,即f[i]=f[i-1]+1;若a[i]>最大值,则当前区间的上限变为a[i],下限在范围[a[i]-m,a[i]-k]之内。在递增的单调队列中不断的删除队头,直到找到一个在范围[a[i]-m,a[i]-k]内的队头元素位置,找不到则f[i]=0,否则f[i]的值为队头的位置到i的位置的元素个数,要记得将a[i]加入到两个队列的队尾。

若a[i]<最小值,方法是对称的,即在递减的队列中去查找队头满足[[a[i]+m,a[i]+k],不满足即删除队头。f[i]的长度为队头的位置到i的位置的元素个数,或为0.

HDU3530 子序列的更多相关文章

  1. 用python实现最长公共子序列算法(找到所有最长公共子串)

    软件安全的一个小实验,正好复习一下LCS的写法. 实现LCS的算法和算法导论上的方式基本一致,都是先建好两个表,一个存储在(i,j)处当前最长公共子序列长度,另一个存储在(i,j)处的回溯方向. 相对 ...

  2. codevs 1576 最长上升子序列的线段树优化

    题目:codevs 1576 最长严格上升子序列 链接:http://codevs.cn/problem/1576/ 优化的地方是 1到i-1 中最大的 f[j]值,并且A[j]<A[i] .根 ...

  3. [LeetCode] Arithmetic Slices II - Subsequence 算数切片之二 - 子序列

    A sequence of numbers is called arithmetic if it consists of at least three elements and if the diff ...

  4. [LeetCode] Is Subsequence 是子序列

    Given a string s and a string t, check if s is subsequence of t. You may assume that there is only l ...

  5. [LeetCode] Wiggle Subsequence 摆动子序列

    A sequence of numbers is called a wiggle sequence if the differences between successive numbers stri ...

  6. [LeetCode] Increasing Triplet Subsequence 递增的三元子序列

    Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the ar ...

  7. [LeetCode] Distinct Subsequences 不同的子序列

    Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...

  8. 动态规划之最长公共子序列(LCS)

    转自:http://segmentfault.com/blog/exploring/ LCS 问题描述 定义: 一个数列 S,如果分别是两个或多个已知数列的子序列,且是所有符合此条件序列中最长的,则 ...

  9. [Data Structure] LCSs——最长公共子序列和最长公共子串

    1. 什么是 LCSs? 什么是 LCSs? 好多博友看到这几个字母可能比较困惑,因为这是我自己对两个常见问题的统称,它们分别为最长公共子序列问题(Longest-Common-Subsequence ...

随机推荐

  1. ZOJ 1201 Inversion

    原题链接 题目大意:给一个数组{ A1,A2,…,An } ,要求生成另一个数组B1,B2,…,Bn,使得Bi表示的是在数组A中排在i前面大于i的数字的个数.题目的输入是数组A(字母P表示)或者数组B ...

  2. leetcode 123. Best Time to Buy and Sell Stock III ----- java

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  3. 2-1-1 GDI

    主要内容:介绍Windows图形设备接口(GDI)及相关概念 1.Windows图形设备接口(GDI) <1>Windows图形设备接口(GDI),是为与设备无关的图形设计的.  所谓设备 ...

  4. mysql创建用户和创建数据库赋值用户操作权限

    mysql 进入mysqldb SELECT DISTINCT CONCAT('User: ''',user,'''@''',host,''';') AS query FROM mysql.user; ...

  5. Visible 绑定

    目的 Visible绑定通过绑定一个值来确定DOM元素显示或隐藏 <script src="knockout.js"></script><div da ...

  6. ps调色技能

    色相/饱和度: 色相->当你看到一种颜色,你会很快的辨别他为什么颜色,比如说红色,绿色,黄色.选中某种颜色然后调整色相. 饱和度->色彩艳丽程度. 明度->调整图片暗亮 曲线:暗亮调 ...

  7. HTTPS-透彻学习汇总

    SSL和SSH和OpenSSH,OpenSSL有什么区别 一.SSL的作用 不使用SSL/TLS的HTTP通信,就是不加密的通信.所有信息明文传播,带来了三大风险. 窃听风险(eavesdroppin ...

  8. FSM, VISIBILITY MAP AND VACUUM

    Update: Heikki’s slides are here! Heikki Linnakangas gave a presentation this past Sunday at FOSDEM  ...

  9. postgresql downgrade issue

    Q: Dear Support Team, If we use ubuntu server to install postgresql9.4, how can we keep original dat ...

  10. 转-Spring 注解学习手札(七) 补遗——@ResponseBody,@RequestBody,@PathVariable

    转-http://snowolf.iteye.com/blog/1628861/ Spring 注解学习手札(七) 补遗——@ResponseBody,@RequestBody,@PathVariab ...