单调队列&单调栈 基础
参考博客https://www.cnblogs.com/tham/p/8038828.html
例题 poj 2823
| Time Limit: 12000MS | Memory Limit: 65536K | |
| Total Submissions: 67137 | Accepted: 19061 | |
| Case Time Limit: 5000MS | ||
Description
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
Output
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]);
}
}
单调队列&单调栈 基础的更多相关文章
- 单调队列 && 单调栈
单调队列 && 单调栈 单调队列 维护某个滑动区间的min or max,可用于dp的优化 以维护min为例,采用STL双端队列实现 每次加入元素x前 先检查队首元素==滑动后要删除的 ...
- 联赛模拟测试18 A. 施工 单调队列(栈)优化DP
题目描述 分析 对于 \(Subtask\ 1\),可以写一个 \(n^3\) 的 \(DP\),\(f[i][j]\) 代表第 \(i\) 个建筑高度为 \(j\) 时的最小花费,随便转移即可 时间 ...
- 数据结构录 之 单调队列&单调栈。
队列和栈是很常见的应用,大部分算法中都能见到他们的影子. 而单纯的队列和栈经常不能满足需求,所以需要一些很神奇的队列和栈的扩展. 其中最出名的应该是优先队列吧我觉得,然后还有两种比较小众的扩展就是单调 ...
- 单调队列&单调栈
单调队列 例题: Poj 2823给定一个数列,从左至右输出每个长度为m的数列段内的最小数和最大数.数列长度:N<=106,m<=N 对于单调队列,我们这样子来定义: 1.维护区间最值 2 ...
- 数据结构录 之 单调队列&单调栈。(转)
http://www.cnblogs.com/whywhy/p/5066306.html 队列和栈是很常见的应用,大部分算法中都能见到他们的影子. 而单纯的队列和栈经常不能满足需求,所以需要一些很神奇 ...
- 大视野 1012: [JSOI2008]最大数maxnumber(线段树/ 树状数组/ 单调队列/ 单调栈/ rmq)
1012: [JSOI2008]最大数maxnumber Time Limit: 3 Sec Memory Limit: 162 MBSubmit: 9851 Solved: 4318[Submi ...
- 小Z爱序列(NOIP信(sang)心(bin)赛)From FallDream(粗制单调队列&单调栈的算法解析)
原题: 小Z最擅长解决序列问题啦,什么最长公共上升然后下降然后上升的子序列,小Z都是轻松解决的呢. 但是小Z不擅长出序列问题啊,所以它给了你一道签到题. 给定一个n个数的序列ai,你要求出满足下述条件 ...
- 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 ...
- 单调队列&单调栈归纳
单调队列 求长度为M的区间内的最大(小)值 单调队列的基本操作,也就是经典的滑动窗口问题. 求长度为M的区间内最大值和最小值的最大差值 两个单调队列,求出长度为M的区间最大最小值的数组,分别求最大最小 ...
随机推荐
- 前台js获得json数据
$.ajax({ type:"post", url:"testAction.action", data:{ classId:classId }, success ...
- javaScript关闭浏览器 (不弹出提示框)
一段JavaScript脚本程序,负责关闭窗口,如果网页不是通过脚本程序打开的(window.open()),调用window.close()脚本关闭窗口前,必须先将window.opener对象置为 ...
- AJPFX详解泛型中super和extends关键字
首先,我们定义两个类,A和B,并且假设B继承自A.下面的代码中,定义了几个静态泛型方法,这几个例子随便写的,并不是特别完善,我们主要考量编译失败的问题: Java代码 public class Ge ...
- AJPFX关于增强for的概述和使用(foreach)
增强for的概述和使用(foreach)1.增强for的概述和使用(foreach) 格式: for(数组或者Collection集合中元素 ...
- nginx for windows 安装
一.nginx for windows 的安装地址: http://nginx.org/en/download.html 二.nginx 安装地址: http://nginx.org/en/docs/ ...
- java调用jacob生成pdf,word,excel横向
/* * 传进一个office文件的byte[]以及后缀,生成一个pdf文件的byte[] */ public byte[] jacob_Office2Pdf(byte[] srcFileBytes, ...
- iTOP-6818开发板-Android4.4系统下RFID射频模块测试例程
平台:迅为iTOP-6818开发板 系统:Android4.4版本 例程:RFID射频模块测试例程 rc522 驱动在 Android 系统的内核是默认集成的,用户可以在开发板上使用命令“ls /de ...
- Android(java)学习笔记198:ContentProvider使用之内容观察者(观察发出去的短信)
1.新建一个案例如下: 2. 不需要添加权限,同时这里布局文件不做修改,来到MainActivity,如下: package com.itheima.sendsmslistener; import a ...
- laravel学习:模块化caffeinated
# Modules Extract and modularize your code for maintainability. Essentially creates "mini-larav ...
- java线程池 多线程 搜索包含关键字的文件路径
package org.jimmy.searchfile20180807.main; public class ThreadMain implements Runnable{ private int ...