单调栈(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 ...
随机推荐
- android之layer-list
效果图: 实现代码: <?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:and ...
- ASP.NET MVC 模型绑定
模型绑定指的是MVC从浏览器发送的HTTP请求中为我们创建.NET对象,在HTTP请求和C#间起着桥梁的作用.模型绑定的一个最简单的例子是带参数的控制器action方法,比如我们注册这样的路径映射: ...
- service依赖dao的接口进行数据传输
service依赖dao的接口进行数据传输
- innobackupx备份原理
1.工具内容 该软件安装完成会有四个工具,如下所示: usr├── bin│ ├── innobackupex│ ├── xbcrypt│ ├── xbstream│ └── xtrabackup 其 ...
- BZOJ5020 THUWC2017在美妙的数学王国中畅游(LCT)
明摆着的LCT,问题在于如何维护答案.首先注意到给出的泰勒展开式,并且所给函数求导非常方便,肯定要用上这玩意.容易想到展开好多次达到精度要求后忽略余项.因为x∈[0,1]而精度又与|x-x0|有关,当 ...
- java 自动装箱
Java 编译器把原始类型自动转换为封装类的过程称为自动装箱(autoboxing),相当于调用包装类的valueof方法.举例说明: 源码: 编译之后的代码:
- 【转】__ATTRIBUTE__ 你知多少
__ATTRIBUTE__ 你知多少? GNU C 的一大特色就是__attribute__ 机制.__attribute__ 可以设置函数属性(Function Attribute ).变量属性(V ...
- Metasploit+python生成免杀exe过360杀毒
Metasploit+python生成免杀exe过360杀毒 1在kali下生成一个反弹的msf的python脚本,命令如下: msfvenom -p windows/meterpreter/reve ...
- 单点登录(八)-----遇到问题-----Application Not Authorized to Use CAS
配置好cas后访问cas client 并没有跳转到登录页面,而是页面报错误提示: Application Not Authorized to Use CAS. The application yo ...
- 构建工具-----Gradle-----安装配置
介绍 Gradle 是一款构建工具,继 Ant .Maven 之后的现代构建工具. 下载 Gradle 下面是 Gradle 的官方网站地址: Gradle l Modern Open-Source ...