单调栈(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 ...
随机推荐
- (转)web性能优化
前端是庞大的,包括 HTML. CSS. Javascript.Image .Flash等等各种各样的资源.前端优化是复杂的,针对方方面面的资源都有不同的方式.那么,前端优化的目的是什么 ? 1. 从 ...
- CSS 绝对定位与弹性布局合作居中
position: absolute; display:flex; justify-content:center;align-items:center;
- find K maximum value from an unsorted array(implement min heap)
Maintain a min-heap with size = k, to collect the result. //Find K minimum values from an unsorted a ...
- window.location.hash 使用
[转]http://www.cnblogs.com/nifengs/p/5104763.html location是javascript里边管理地址栏的内置对象,比如location.href就管理页 ...
- 洛谷 P3990 [SHOI2013]超级跳马 解题报告
P3990 [SHOI2013]超级跳马 题目描述 现有一个\(n\) 行 \(m\) 列的棋盘,一只马欲从棋盘的左上角跳到右下角.每一步它向右跳奇数列,且跳到本行或相邻行.跳越期间,马不能离开棋盘. ...
- 中行P1签名及验签
分享中国银行快捷.NET P1签名和验签方法代码中ReturnValue为自定义类型请无视 #region 验证签名 /// <summary> /// 验证签名 /// </sum ...
- linux内核分析 第五周 扒开系统调用的三层皮(下)
rm menu -rf 强制删除原menu文件 git clone http://github.com/mengning/menu.git 从github中克隆 cd menu 在test.c中增加上 ...
- bzoj3672: [Noi2014]购票(树形DP+斜率优化+可持久化凸包)
这题的加强版,多了一个$l_i$的限制,少了一个$p_i$的单调性,难了好多... 首先有方程$f(i)=min\{f(j)+(dep_i-dep_j)*p_i+q_i\}$ $\frac {f(j) ...
- 解题:HEOI 2012 采花
题面 题外话:LYD说他当时看错题了,考场爆零了,然后有了作诗这道题=.= 离线处理询问,按右端点递增排序,然后对于每种花$flw[i]$,我们求一个$pre[flw[i]]$表示这种花上一次出现的位 ...
- python之旅:迭代器、生成器、面向过程编程
1.什么是迭代器? 1.什么是迭代器 迭代的工具 什么是迭代? 迭代是一个重复的过程,每一次重复都是基于上一次结果而进行的 # 单纯的重复并不是迭代 while True: print('=====& ...