单调栈(G - Sliding Window POJ - 2823 )
题目链接:https://cn.vjudge.net/contest/276251#problem/G
题目大意:给你n和m,然后问你对于(m,n)这中间的每一个数,(i-m+1,i)这个区间的最小值和最大值。
具体思路:单调队列,对于个数的控制,我们通过队列来实现一个模拟的滑动窗口。然后最值的寻找,我们可以通过控制队列保持单调递增或者单调递减来实现。
STL AC代码(耗时:10985s):
#include<iostream>
#include<stack>
#include<cmath>
#include<string>
#include<stdio.h>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
# define inf 0x3f3f3f3f
# define ll long long
const int maxn = 1e6+;
int minn[maxn],maxx[maxn],sto[maxn];
inline int read1()
{
int f=,x=;
char s=getchar();
while(s<'' || s>'')
{
if(s=='-')
f=-;
s=getchar();
}
while(s>='' && s<='')
{
x=x*+s-'';
s=getchar();
}
return x*f;
}
int main()
{
int n,m;
n=read1();
m=read1();
// scanf("%d %d",&n,&m);
deque<int>q1;
deque<int >q2;
int tot=,tmp;
for(int i=;i<=n;i++){
sto[i]=read1();
}
for(int i=; i<=n; i++)
{
while(!q1.empty()&&q1.front()<i-m+)//先判断左边界有没有超出范围
q1.pop_front();
while(!q1.empty()&&sto[i]<sto[q1.back()])//保持队列单调递增
{
q1.pop_back();
}
q1.push_back(i);
if(i>=m)
minn[++tot]=q1.front();
while(!q2.empty()&&q2.front()<i-m+)
q2.pop_front();
while(!q2.empty()&&sto[i]>sto[q2.back()])
{
q2.pop_back();
}
q2.push_back(i);
if(i>=m)
maxx[tot]=q2.front();
}
for(int i=; i<=tot; i++)
{
if(i==)
printf("%d",sto[minn[i]]);
else
printf(" %d",sto[minn[i]]);
}
printf("\n");
for(int i=; i<=tot; i++)
{
if(i==)
printf("%d",sto[maxx[i]]);
else
printf(" %d",sto[maxx[i]]);
}
printf("\n");
return ;
}
数组模拟(耗时:7152ms)AC代码:
#include<iostream>
#include<stack>
#include<cmath>
#include<string>
#include<stdio.h>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
# define inf 0x3f3f3f3f
# define ll long long
const int maxn = 1e6+;
int minn[maxn],maxx[maxn],sto[maxn];
int moni[maxn];
inline int read1()
{
int f=,x=;
char s=getchar();
while(s<'' || s>'')
{
if(s=='-')
f=-;
s=getchar();
}
while(s>='' && s<='')
{
x=x*+s-'';
s=getchar();
}
return x*f;
}
int main()
{
int n,m;
n=read1();
m=read1();
int l=,r=;
int tot=;
for(int i=; i<=n; i++)
{
sto[i]=read1();
}
for(int i=; i<=n; i++)
{
while(l<=r&&moni[l]<i-m+)
l++;
while(l<=r&&sto[i]<sto[moni[r]]){
r--;
}
moni[++r]=i;
if(i>=m)
minn[++tot]=moni[l];
}
l=,r=,tot=;
for(int i=; i<=n; i++)
{
while(l<=r&&moni[l]<i-m+)
l++;
while(l<=r&&sto[i]>sto[moni[r]]){
r--;
}
moni[++r]=i;
if(i>=m)
maxx[++tot]=moni[l];
}
for(int i=; i<=tot; i++)
{
if(i==)
printf("%d",sto[minn[i]]);
else
printf(" %d",sto[minn[i]]);
}
printf("\n");
for(int i=; i<=tot; i++)
{
if(i==)
printf("%d",sto[maxx[i]]);
else
printf(" %d",sto[maxx[i]]);
}
printf("\n");
return ;
}
(我太菜了,,,数组模拟调了半个小时。。)
单调栈(G - Sliding Window POJ - 2823 )的更多相关文章
- Sliding Window POJ - 2823 单调队列模板题
Sliding Window POJ - 2823 单调队列模板题 题意 给出一个数列 并且给出一个数m 问每个连续的m中的最小\最大值是多少,并输出 思路 使用单调队列来写,拿最小值来举例 要求区间 ...
- Sliding Window POJ - 2823
Description An array of size n ≤ 106 is given to you. There is a sliding window of size k which is m ...
- AC日记——Sliding Window poj 2823
2823 思路: 单调队列: 以前遇到都是用线段树水过: 现在为了优化dp不得不学习单调队列了: 代码: #include <cstdio> #include <cstring> ...
- sliding windows (poj 2823) 题解
[问题描述] 给你一个长度为N的数组,一个长为K的滑动的窗体从最左移至最右端,你只能见到窗口的K个数,每次窗体向右移动一位,如下表: [样例输入] 8 3 1 3 -1 -3 5 3 6 7 [样例输 ...
- poj 2823 Sliding Window (单调队列入门)
/***************************************************************** 题目: Sliding Window(poj 2823) 链接: ...
- Codeforces 1107G Vasya and Maximum Profit 线段树最大子段和 + 单调栈
Codeforces 1107G 线段树最大子段和 + 单调栈 G. Vasya and Maximum Profit Description: Vasya got really tired of t ...
- POJ 2823 Sliding Window + 单调队列
一.概念介绍 1. 双端队列 双端队列是一种线性表,是一种特殊的队列,遵守先进先出的原则.双端队列支持以下4种操作: (1) 从队首删除 (2) 从队尾删除 (3) 从队尾插入 (4) ...
- 题解报告:poj 2823 Sliding Window(单调队列)
Description An array of size n ≤ 106 is given to you. There is a sliding window of size k which is m ...
- POJ 2823 Sliding Window(单调队列入门题)
Sliding Window Time Limit: 12000MS Memory Limit: 65536K Total Submissions: 67218 Accepted: 190 ...
随机推荐
- ACM数论之旅17---反演定理 第一回 二项式反演(神说要有光 于是就有了光(´・ω・`))
终于讲到反演定理了,反演定理这种东西记一下公式就好了,反正我是证明不出来的~(-o ̄▽ ̄)-o 首先,著名的反演公式 我先简单的写一下o( ̄ヘ ̄*o) 比如下面这个公式 f(n) = g(1) + g ...
- CF271D_Good Substrings
给一个原串,以及那些字符是坏的,现在问你可以从原串中取出多少个不同子串,使得其所含的坏字符的个数不超过一个定数. 这个题目网上有各种各样的解法.如hash,tire. 我说一下我的解法. 解法一:后缀 ...
- 013 BIO、NIO、AIO的区别
作者:nnngu GitHub:https://github.com/nnngu 博客园:http://www.cnblogs.com/nnngu 简书:https://www.jianshu.com ...
- [BZOJ3065]带插入区间K小值 解题报告 替罪羊树+值域线段树
刚了一天的题终于切掉了,数据结构题的代码真**难调,这是我做过的第一道树套树题,做完后感觉对树套树都有阴影了......下面写一下做题记录. Portal Gun:[BZOJ3065]带插入区间k小值 ...
- 用Python实现的数据结构与算法:基本搜索
一.顺序搜索 顺序搜索 是最简单直观的搜索方法:从列表开头到末尾,逐个比较待搜索项与列表中的项,直到找到目标项(搜索成功)或者 超出搜索范围 (搜索失败). 根据列表中的项是否按顺序排列,可以将列表分 ...
- php高效遍历文件夹、高效读取文件
/** * PHP高效遍历文件夹(大量文件不会卡死) * @param string $path 目录路径 * @param integer $level 目录深度 */ function fn_sc ...
- 模拟器下的虚拟sd卡添加文件
1.若出现mkdir failed for myData Read-only file system,在执行 adb shell 命令后,执行mount -o remount ,rw / (去除文件的 ...
- bzoj 2002
2002: [Hnoi2010]Bounce 弹飞绵羊 Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 12203 Solved: 6162[Subm ...
- haproxy做TCP层的负载均衡
最新项目中发现,大量游戏玩家访问登录服务器时出现延迟,导致玩家无法登录,愿意可能是登录服务器性能达到极限. 所以目前想通过proxy的方式访问登录服务器集群,避免登录延迟. 1.下载haproxy最新 ...
- P4779 【模板】单源最短路径(标准版)
P4779 [模板]单源最短路径(标准版) 求单源最短路, 输出距离 Solution \(nlogn\) 堆优化 \(Djs\) Code #include<iostream> #inc ...