HDU3530 子序列
题目大意:给出一串长度为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 子序列的更多相关文章
- 用python实现最长公共子序列算法(找到所有最长公共子串)
软件安全的一个小实验,正好复习一下LCS的写法. 实现LCS的算法和算法导论上的方式基本一致,都是先建好两个表,一个存储在(i,j)处当前最长公共子序列长度,另一个存储在(i,j)处的回溯方向. 相对 ...
- codevs 1576 最长上升子序列的线段树优化
题目:codevs 1576 最长严格上升子序列 链接:http://codevs.cn/problem/1576/ 优化的地方是 1到i-1 中最大的 f[j]值,并且A[j]<A[i] .根 ...
- [LeetCode] Arithmetic Slices II - Subsequence 算数切片之二 - 子序列
A sequence of numbers is called arithmetic if it consists of at least three elements and if the diff ...
- [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 ...
- [LeetCode] Wiggle Subsequence 摆动子序列
A sequence of numbers is called a wiggle sequence if the differences between successive numbers stri ...
- [LeetCode] Increasing Triplet Subsequence 递增的三元子序列
Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the ar ...
- [LeetCode] Distinct Subsequences 不同的子序列
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- 动态规划之最长公共子序列(LCS)
转自:http://segmentfault.com/blog/exploring/ LCS 问题描述 定义: 一个数列 S,如果分别是两个或多个已知数列的子序列,且是所有符合此条件序列中最长的,则 ...
- [Data Structure] LCSs——最长公共子序列和最长公共子串
1. 什么是 LCSs? 什么是 LCSs? 好多博友看到这几个字母可能比较困惑,因为这是我自己对两个常见问题的统称,它们分别为最长公共子序列问题(Longest-Common-Subsequence ...
随机推荐
- Deep Learning论文笔记之(四)CNN卷积神经网络推导和实现(转)
Deep Learning论文笔记之(四)CNN卷积神经网络推导和实现 zouxy09@qq.com http://blog.csdn.net/zouxy09 自己平时看了一些论文, ...
- 148. Sort List
Sort a linked list in O(n log n) time using constant space complexity. 代码如下: /** * Definition for si ...
- 在JSP页面下使用AJAX实现用户名存在的检测
<script type="text/javascript"> function init(){ document.getElementById ...
- leetcode 91 Decode Ways ----- java
A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...
- PHP设置图片文件上传大小的具体实现方法
PHP默认的上传限定是最大2M,想上传超过此设定的文件,需要调整PHP.apache等的一些参数 我们简要介绍一下PHP文件上传涉及到的一些参数: •file_uploads :是否允许通过HTTP上 ...
- spark sql 访问hive数据时找不mysql的解决方法
我尝试着在classpath中加n入mysql的驱动仍不行 解决方法:在启动的时候加入参数--driver-class中加入mysql 驱动 [hadoop@master spark-1.0.1-bi ...
- 最短路--floyd算法模板
floyd算法是求所有点之间的最短路的,复杂度O(n3)代码简单是最大特色 #include<stdio.h> #include<string.h> ; const int I ...
- treap树及相关算法
#include "stdafx.h" #include <stdio.h> #include <stdlib.h> #include <string ...
- 学习tornado:安全
http://blog.csdn.net/siddontang/article/details/18053915
- 经典ASP.NET MVC3.0入门详解
http://blog.csdn.net/csh624366188/article/details/7064269 :由于本文原在word文档里编写,写本文章时运用了大量截图,直接复制到博客里,没有显 ...