【t019】window(单调队列)
Time Limit: 2 second
Memory Limit: 256 MB
【问题描述】
给你一个长度为N 的数组,一个长为K的滑动的窗体从最左移至最右端,你只能见到窗口的K个数,每次窗体向右移动一位,如下表:
Window position Min value Max 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
你的任务是找出窗口在各位置时的Max value和Min value。
【输入格式】
第一行N,K,第二行为长度为N的数组
【输出格式】
第一行每个位置的Min value,第二行每个位置的Max value
【输入样例1】
8 3
1 3 -1 -3 5 3 6 7
【输出样例1】
-1 -3 -3 -3 3 3
3 3 5 5 6 7
数据规模
20%:N≤500;50%:N≤100000;100%:N≤1000000;
【题目链接】:http://noi.qz5z.com/viewtask.asp?id=t019
【题解】
最小值维护一个单调递增队列就能搞定;(a[i]<=a[i+1]
每次加入队列中的元素先和队尾比较一下,对于队尾比它大的数,直接删掉就好,因为要求的是最小值,而你加入的这个数是在框框里面的,那么如果你比这个数还大,那么你肯定没有存在的价值了;
然后对于队头元素,如果它不在框框里面也全都删掉,直到队头元素在框框里为止,然后输出队头元素;
最大值类似.
(可以在2s内出)
【完整代码】
#include <cstdio>
#include <algorithm>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
const int MAXN = 1e6+100;
int n,k,a[MAXN];
int m[MAXN],mm[MAXN],h,t;
int main()
{
//freopen("F:\\rush.txt","r",stdin);
scanf("%d%d",&n,&k);
for (int i = 1;i <= n;i++)
scanf("%d",&a[i]);
h = 1,t = 0;
for (int i = 1;i <= k-1;i++)
{
while (t>=h && m[t]>=a[i]) t--;
t++;
m[t] = a[i];mm[t] = i;
}
for (int i = k;i<= n;i++)
{
while (t>=h && m[t]>=a[i]) t--;
t++;
m[t] = a[i];mm[t] = i;
while (mm[h]<i-k+1 && h <= t) h++;
printf("%d",m[h]);
if (i==n)
puts("");
else
putchar(' ');
}
h = 1,t = 0;
for (int i = 1;i <= k-1;i++)
{
while (t>=h && m[t]<=a[i]) t--;
t++;
m[t] = a[i];mm[t] = i;
}
for (int i = k;i <= n;i++)
{
while (t>=h && m[t]<=a[i]) t--;
t++;
m[t] = a[i];mm[t] = i;
while (mm[h]<i-k+1 && h <= t) h++;
printf("%d",m[h]);
if (i==n)
puts("");
else
putchar(' ');
}
return 0;
}
【t019】window(单调队列)的更多相关文章
- POJ 2823 Sliding Window + 单调队列
一.概念介绍 1. 双端队列 双端队列是一种线性表,是一种特殊的队列,遵守先进先出的原则.双端队列支持以下4种操作: (1) 从队首删除 (2) 从队尾删除 (3) 从队尾插入 (4) ...
- poj 2823 Sliding Window (单调队列入门)
/***************************************************************** 题目: Sliding Window(poj 2823) 链接: ...
- POJ2823 Sliding Window(单调队列)
单调队列,我用deque维护.这道题不难写,我第二次写单调队列,1次AC. -------------------------------------------------------------- ...
- POJ 2823:Sliding Window 单调队列
Sliding Window Time Limit: 12000MS Memory Limit: 65536K Total Submissions: 48930 Accepted: 14130 ...
- POJ 2823 Sliding Window (单调队列)
单调队列 加了读入挂比不加更慢.... 而且这份代码要交c++ 有大神G++跑了700ms..... orzorzorz #include<iostream> #include<cs ...
- POJ 2823 UESTCoj 1221 Sliding Window 单调队列 经典入门题
题意:给出一个序列,求出每连续k个数字中最大的数和最小的数. 这是道单调队列裸题,直接写就行了. 本来用deque写出来后,发现在poj上硬是超时了,在discuss上看很多人也在抱怨超时的问题,据说 ...
- poj2823Sliding Window——单调队列
题目:http://poj.org/problem?id=2823 单调队列模板. 代码如下: #include<iostream> #include<cstdio> usin ...
- POJ2823 Sliding Window (单调队列)
POJ2823 Sliding Window Time Limit: 12000MS Memory Limit: 65536K Total Submissions: 38342 Accepte ...
- POJ 2823 Sliding Window(单调队列入门题)
Sliding Window Time Limit: 12000MS Memory Limit: 65536K Total Submissions: 67218 Accepted: 190 ...
随机推荐
- PHP服务器环境打开配置文件
MAC 1. cd /usr/local/etc/nginx/servers vim www.xxx.com 2. 在/usr/local/etc/nginx/servers目录下,不同的 .con ...
- FansMail:邮件发送标准API与技术实现(Java)
发送邮件,是Web系统等IT建设中最常见的一种功能. 我对最常见的一种需求进行了抽象和封装,定义了一套标准的API,并且使用Java技术实现. 项目信息 项目名称:FansMail 项目作者:LeiW ...
- [React] Remove React PropTypes by using Flow Annotations (in CRA)
Starting from v15.5 if we wanted to use React's PropTypes we had to change our code to use a separat ...
- poj 1191 棋盘切割 (压缩dp+记忆化搜索)
一,题意: 中文题 二.分析: 主要利用压缩dp与记忆化搜索思想 三,代码: #include <iostream> #include <stdio.h> #include & ...
- [Err] 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''<h1 style="text-align: center;">php
[Err] 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL s ...
- ejs模板引擎的使用
引入ejs.min.js 创建模板,以<%=jsCode%>包裹起来其余的html和html结构一样 focusTemplateData是模板使用的数据,使用$.each()方法遍历绑定数 ...
- javascript中的this指向问题总结
this的指向在函数定义的时候是确定不了的,只有函数执行的时候才能确定this到底指向谁,实际上this的最终指向的是那个调用它的对象 1.函数执行的时候,首先看函数名前边是否有点 ‘·’,有的话点’ ...
- 【零基础入门学习Python笔记012】一个打了激素的数组3
列表的一些经常使用操作符 比較操作符 逻辑操作符 连接操作符 反复操作符 成员关系操作符 +表示两个连接.*表示复制. list中"+"两边的类型必须一致. 演示样例: water ...
- python 命令行:help(),'more'不是内部或外部命令,也不是可运行的程序或批处理文件
Python下使用help(dict),显示'more'不是内部或外部命令,也不是可运行的程序或批处理文件,该如何处理? 环境变量设置的问题,进入 Path 的环境变量设置界面,将;%SystemRo ...
- TF卡电压 SD卡引脚
//////////////////////////////////////////////////////////////////////////////////////////////////// ...