参考博客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. [转]Using the Interop Activity in a .NET Framework 4 Workflow

    本文转自:http://msdn.microsoft.com/en-us/library/ee264174(v=vs.100).aspx This topic applies to Windows W ...

  2. Oracle 的备份和恢复

    Oracle数据库有三种标准的备份方法,它们分别是导出/导入(EXP/IMP).热备份和冷备 份.导出备件是一种逻辑备份,冷备份和热备份是物理备份. 一. 导出/导入(Export/Import) 利 ...

  3. 腾讯云COS对象存储的简单使用

    叮当哥之前买了一年的腾讯云服务器,昨日偶然发现腾讯云送了叮当哥半年的cos对象存储服务器,于是就撸起袖子传了几张珍藏的高清大图上去,现将其上传的简单使用步骤总结一波(其它操作参加官方SDK文档API) ...

  4. js中,浏览器中不同元素位置属性解析

    offset()   只对可见元素有效,获取匹配元素在当前视口的相对偏移,返回的对象有两个整型属性,top和left,像素计算: position() 相对父元素的偏移,position.left   ...

  5. javascript中闭包与作用域的理解

    很多js的框架与插件编写都用到了闭包,所以,阅读和掌握闭包很有必要.最近学习vue框架时,经常会猜想很多功能的native js实现,很多都应用到了闭包,闭包除了目前已知的一些特性,如:可以保持局部变 ...

  6. SQL Case 语句的使用

    -----简单case 使用 select 学号,姓名, case 专业 when '金融系' then '1' when '材料成型及控制工程' then '2' else '3' end from ...

  7. 绿化VSCode

    通过啃源码, 终于找到了解决办法, 设置环境变量: VSCODE_APPDATA=C:\Program Files\VSCode\UserData VSCODE_EXTENSIONS=%VSCODE_ ...

  8. 【译】x86程序员手册32-9.4 中断描述符表

    9.4 Interrupt Descriptor Table 中断描述符表 The interrupt descriptor table (IDT) associates each interrupt ...

  9. QQ感叹号是什么鬼?原来是服务器波动,腾讯官方来辟谣了

    今天晚上很多网友在用QQ发送消息的时候发现,自己发送的消息一直是感叹号❗到底是怎么回事呢?是消息都发不出去了吗?马浩周通过手机测试后发现,其实消息是可以发出去的,而官方手机QQ出来已经通知了,是服务器 ...

  10. freopen()重定向的打开和关闭

    freopen函数 功能 使用不同的文件或模式重新打开流,即重定向. 实现重定向,把预定义的标准流文件定向到由path指定的文件中.(直观感觉/实际操作都像是把文件定向到流,难道是说,对流来说就是重定 ...