参考博客https://www.cnblogs.com/tham/p/8038828.html

例题  poj 2823

                                       Sliding Window
Time Limit: 12000MS   Memory Limit: 65536K
Total Submissions: 67137   Accepted: 19061
Case Time Limit: 5000MS

Description

An array of size n ≤ 106 is given to you. There is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window moves rightwards by one position. Following is an example: 
The array is [1 3 -1 -3 5 3 6 7], and k is 3.

Window position Minimum value Maximum value
[1  3  -1] -3  5  3  6  7  -1 3
 1 [3  -1  -3] 5  3  6  7  -3 3
 1  3 [-1  -3  5] 3  6  7  -3 5
 1  3  -1 [-3  5  3] 6  7  -3 5
 1  3  -1  -3 [5  3  6] 7  3 6
 1  3  -1  -3  5 [3  6  7] 3 7

Your task is to determine the maximum and minimum values in the sliding window at each position.

Input

The input consists of two lines. The first line contains two integers n and k which are the lengths of the array and the sliding window. There are n integers in the second line. 

Output

There are two lines in the output. The first line gives the minimum values in the window at each position, from left to right, respectively. The second line gives the maximum values. 

Sample Input

8 3
1 3 -1 -3 5 3 6 7

Sample Output

-1 -3 -3 -3 3 3
3 3 5 5 6 7 题意 给出一个序列 n个数 求每个长度为k的子串的最大值和最小值
解析 如果用尺取写的话 失匹的话就要花费o(k)的复杂度去维护最大值 最小值 总时间复杂度o(n*k) 数据卡的死就肯定超时
    我们发现维护最大最最小值的时候 发现有很多重复的比较 所以我们可以维护一个最值数组 使它严格单调 这样直接取第一个元素就好了(单调队列)。
    
AC代码(c++)
 #include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <string>
#include <queue>
#include <map>
#include <vector>
using namespace std;
const int maxn = 1e6+;
const int inf = 0x3f3f3f3f,mod = ;
const double epx = 1e-;
typedef long long ll;
struct node
{
int x,y; //x值 y下标
}v[maxn];
int a[maxn],mn[maxn],mx[maxn];
int n,m;
void getmin()
{
int head=,tail=;
for(int i=;i<m;i++)
{
while(head<=tail&&a[i]<=v[tail].x)tail--;
v[++tail].x=a[i],v[tail].y=i;
}
for(int j=m;j<=n;j++)
{
while(head<=tail&&a[j]<=v[tail].x)tail--;
v[++tail].x=a[j],v[tail].y=j;
while(j-v[head].y>=m)head++;
mn[j]=v[head].x;
}
}
void getmax()
{
int head=,tail=;
for(int i=;i<m;i++)
{
while(head<=tail&&a[i]>=v[tail].x)tail--;
v[++tail].x=a[i],v[tail].y=i;
}
for(int j=m;j<=n;j++)
{
while(head<=tail&&a[j]>=v[tail].x)tail--;
v[++tail].x=a[j],v[tail].y=j;
while(j-v[head].y>=m)head++;
mx[j]=v[head].x;
}
}
int main()
{
scanf("%d %d",&n,&m);
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
getmax();
getmin();
for(int i=m;i<=n;i++)
{
printf(i!=n?"%d ":"%d\n",mn[i]);
}
for(int i=m;i<=n;i++)
{
printf(i!=n?"%d ":"%d\n",mx[i]);
}
}
												

单调队列&单调栈 基础的更多相关文章

  1. 单调队列 && 单调栈

    单调队列 && 单调栈 单调队列 维护某个滑动区间的min or max,可用于dp的优化 以维护min为例,采用STL双端队列实现 每次加入元素x前 先检查队首元素==滑动后要删除的 ...

  2. 联赛模拟测试18 A. 施工 单调队列(栈)优化DP

    题目描述 分析 对于 \(Subtask\ 1\),可以写一个 \(n^3\) 的 \(DP\),\(f[i][j]\) 代表第 \(i\) 个建筑高度为 \(j\) 时的最小花费,随便转移即可 时间 ...

  3. 数据结构录 之 单调队列&单调栈。

    队列和栈是很常见的应用,大部分算法中都能见到他们的影子. 而单纯的队列和栈经常不能满足需求,所以需要一些很神奇的队列和栈的扩展. 其中最出名的应该是优先队列吧我觉得,然后还有两种比较小众的扩展就是单调 ...

  4. 单调队列&单调栈

    单调队列 例题: Poj 2823给定一个数列,从左至右输出每个长度为m的数列段内的最小数和最大数.数列长度:N<=106,m<=N 对于单调队列,我们这样子来定义: 1.维护区间最值 2 ...

  5. 数据结构录 之 单调队列&单调栈。(转)

    http://www.cnblogs.com/whywhy/p/5066306.html 队列和栈是很常见的应用,大部分算法中都能见到他们的影子. 而单纯的队列和栈经常不能满足需求,所以需要一些很神奇 ...

  6. 大视野 1012: [JSOI2008]最大数maxnumber(线段树/ 树状数组/ 单调队列/ 单调栈/ rmq)

    1012: [JSOI2008]最大数maxnumber Time Limit: 3 Sec  Memory Limit: 162 MBSubmit: 9851  Solved: 4318[Submi ...

  7. 小Z爱序列(NOIP信(sang)心(bin)赛)From FallDream(粗制单调队列&单调栈的算法解析)

    原题: 小Z最擅长解决序列问题啦,什么最长公共上升然后下降然后上升的子序列,小Z都是轻松解决的呢. 但是小Z不擅长出序列问题啊,所以它给了你一道签到题. 给定一个n个数的序列ai,你要求出满足下述条件 ...

  8. POJ 3494 Largest Submatrix of All 1’s 单调队列||单调栈

    POJ 3494 Largest Submatrix of All 1’s Description Given a m-by-n (0,1)-matrix, of all its submatrice ...

  9. 单调队列&单调栈归纳

    单调队列 求长度为M的区间内的最大(小)值 单调队列的基本操作,也就是经典的滑动窗口问题. 求长度为M的区间内最大值和最小值的最大差值 两个单调队列,求出长度为M的区间最大最小值的数组,分别求最大最小 ...

随机推荐

  1. Codeforces Round #179 (Div. 1)

    A 直接线段树过的 两遍 貌似大多是标记过的..注意long long #include <iostream> #include <cstdio> #include <c ...

  2. [转]强制取消TFS2008中其它成员的签出文件

    本文转自:http://www.cnblogs.com/georgehu/archive/2010/10/23/1859573.html 有个项目,以前的成员离职了,刚好又签出了一个文件在TFS中并且 ...

  3. c#拖拽文件

    在“属性”窗口中,先设置MDI的父窗口的AllowDrop 属性更改为true;2.在父窗口的事件中添加下面两个事件 private void Form1_DragEnter(object sende ...

  4. IIS中不让下级应用程序继承主域名的web.config配置

    <location path="." allowOverride="true" inheritInChildApplications="fals ...

  5. 【转】Nicescroll滚动条插件的用法

    原网址:http://blog.csdn.net/mss359681091/article/details/52838179 Nicescroll滚动条插件是一个非常强大的基于JQUERY的滚动条插件 ...

  6. 微信小程序组件解读和分析:九、form表单

    form表单组件说明: 表单,将组件内的用户输入的<switch/> <input/> <checkbox/> <slider/> <radio/ ...

  7. SELECT TOP 100 PERCENT * 的含义

    --返回符合条件的100%的记录,即所有符合条件的记录SELECT TOP 100 PERCENT * --返回符合条件的100条记录,即只返回符合条件的100条记录SELECT TOP 100 * ...

  8. windows sdk编程禁止窗体最大化最小化

    #include <windows.h> /*消息处理函数声明*/ HRESULT CALLBACK WindowProc(HWND hwnd, UINT message, WPARAM ...

  9. Android网站

    http://blog.csdn.net/airsaid/article/details/52902299 android调用传感器的代码 http://blog.csdn.net/huangbiao ...

  10. extjs传递参数

    以前我是这么传的: 先获取表Form,再通过表找组件,然后用gradeCode=0&groupCode=1传 现在只需要先获得表,然后通过form.getValues()取得所有的值,再使用E ...