题目链接:http://poj.org/problem?id=2823

 题意:

  一个长度为n的序列上有一个长度为k的滑窗从左向右滑动,问每个时刻滑窗内最小值和最大值。

题解:

  我们考虑单调队列。

  对于维护最小值,我们维护一个单调递增的序列。新加入一个数时,弹出队尾比他大的数(因为这些数即比他大,又比他靠前,对于后面的区间来说总是不如新加入的数优)。在队首弹出(在序列中编号<该数在序列中编号-k)的数。然后记录队首即该滑窗内最小值。

  最大值同理维护一个单调递减的序列即可。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<deque>
#define LL long long
#define RI register int
using namespace std;
const int INF = 0x7ffffff ;
const int N = 1e6 + 10 ;inline int read() {int k = 0 , f = 1 ; char c = getchar() ;for( ; !isdigit(c) ; c = getchar())if(c == '-') f = -1 ;for( ; isdigit(c) ; c = getchar())k = k*10 + c-'0' ;return k*f ;
}
int n, m ; int a[N], hh[N], gg[N] ;
deque<int>q1 ; // 递减序列维护最大值
deque<int>q2 ; // 递增序列维护最小值
deque<int>q11 ; // 维护递增序列的值在数组中的下标
deque<int>q22 ; // 维护递减序列的值在数组中的下标 int main() {n = read(), m = read() ;for(int i=1;i<=n;i++) a[i] = read() ;for(int i=1;i<m;i++) {while(q1.size() && q1.back() <= a[i]) q1.pop_back(), q11.pop_back() ;while(q2.size() && q2.back() >= a[i]) q2.pop_back(), q22.pop_back() ;q1.push_back(a[i]), q2.push_back(a[i]) ;q11.push_back(i), q22.push_back(i) ;}int cnt = 0 ;for(int i=m;i<=n;i++) {while(q1.size() && q1.back() <= a[i]) q1.pop_back(), q11.pop_back() ;while(q2.size() && q2.back() >= a[i]) q2.pop_back(), q22.pop_back() ;q1.push_back(a[i]), q2.push_back(a[i]) ; q11.push_back(i), q22.push_back(i) ;while(q11.front() <= i-m) q1.pop_front(), q11.pop_front() ;while(q22.front() <= i-m) q2.pop_front(), q22.pop_front() ;hh[++cnt] = q1.front(), gg[cnt] = q2.front() ;}for(int i=1;i<=cnt;i++) printf("%d ",gg[i]) ; printf("\n") ;for(int i=1;i<=cnt;i++) printf("%d ",hh[i]) ;return 0 ;
}

View Code

 update :

  之前的代码有些麻烦了,更简洁的代码请看这里:

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<queue>
 6 using namespace std;
 7 const int N = 1e6 + 10 ;
 8
 9 inline int read() {
10     int k = 0 , f = 1 ; char c = getchar() ;
11     for( ; !isdigit(c) ; c = getchar())
12       if(c == '-') f = -1 ;
13     for( ; isdigit(c) ; c = getchar())
14       k = k*10 + c-'0' ;
15     return k*f ;
16 }
17 int n, m ;
18 int hh[N], q1[N], q2[N], a1[N], a2[N] ;   // 单调递增维护最小值,单调递减维护最大值  ( 都维护序号
19
20 int main() {
21     n = read(), m = read() ;  int h1 = 1, h2 = 1, t1 = 0, t2 = 0 ;
22     for(int i=1;i<=n;i++) hh[i] = read() ;
23     for(int i=1;i<=m;i++) {
24         while(h1 <= t1 && hh[q1[t1]] >= hh[i]) t1-- ;
25         q1[++t1] = i ;
26         while(h2 <= t2 && hh[q2[t2]] <= hh[i]) t2-- ;
27         q2[++t2] = i ;
28     }
29     int tot = 0 ;  a1[++tot] = hh[q1[h1]], a2[tot] = hh[q2[h2]] ;
30     for(int i=m+1;i<=n;i++) {
31         while(h1 <= t1 && hh[q1[t1]] >= hh[i]) t1-- ;  q1[++t1] = i ;
32         while(q1[h1] <= i-m) h1++ ;
33         while(h2 <= t2 && hh[q2[t2]] <= hh[i]) t2-- ;  q2[++t2] = i ;
34         while(q2[h2] <= i-m) h2++ ;
35         a1[++tot] = hh[q1[h1]], a2[tot] = hh[q2[h2]] ;
36     }
37     for(int i=1;i<=tot;i++) printf("%d ",a1[i]) ; printf("\n") ;
38     for(int i=1;i<=tot;i++) printf("%d ",a2[i]) ;
39     return 0 ;
40 }

转载于:https://www.cnblogs.com/zub23333/p/8568040.html

poj2823 Sliding Window相关推荐

  1. POJ2823 Sliding Window【单调队列】【线段树】【ST表】

    Sliding Window POJ - 2823 题意: 给出一个长度为N的序列,通过一个窗口,可以看到序列中连续的K个元素,窗口从最左边出发,每次移动一个单位,对于每次移动,输出当前窗口中的最大值 ...

  2. POJ2823 Sliding Window 单调队列

    题目大意 给出一段序列,一个长度一定的窗口从左到右滑动.求窗口滑动到每个位置时窗口内数字的最大值.最小值各是多少.n<=1e6. 总体思路 遇到这种对一个沿着一个方向滑动的区间求最值问题,可以运 ...

  3. LeetCode 滑动窗口(Sliding Window)类问题总结

    导语 滑动窗口类问题是面试当中的高频题,问题本身其实并不复杂,但是实现起来细节思考非常的多,想着想着可能因为变量变化,指针移动等等问题,导致程序反复删来改去,有思路,但是程序写不出是这类问题最大的障碍 ...

  4. python实现滑动窗口平均_数据流滑动窗口平均值 · sliding window average from data stream...

    [抄题]: 给出一串整数流和窗口大小,计算滑动窗口中所有整数的平均值. MovingAverage m = new MovingAverage(3); m.next(1) = 1 // 返回 1.00 ...

  5. POJ 2823 Sliding Window

    Sliding Window 链接:http://poj.org/problem?id=2823 Time Limit: 12000MS   Memory Limit: 65536K       Ca ...

  6. LeetCode 239. Sliding Window Maximum

    原题链接在这里:https://leetcode.com/problems/sliding-window-maximum/ 题目: Given an array nums, there is a sl ...

  7. 【POJ - 2823】 Sliding Window(单调队列 用双端队列实现或模拟队列)

    题干: An array of size n ≤ 10 6 is given to you. There is a sliding window of size k which is moving f ...

  8. ST算法 Sliding Window algorithm template

    ST算法(Sliding Window):A easy way to slove the substring problems algorithm template to slove substrin ...

  9. [Swift]LeetCode480. 滑动窗口中位数 | Sliding Window Median

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...

最新文章

  1. java 扫描所有子类,是否可以获取类的所有子类?
  2. truncate table
  3. Apollo自动驾驶入门课程第⑨讲 — 控制(上)
  4. 懂了这些,你才真正懂了C
  5. 2019级C语言大作业 - BrickMansions
  6. String 也能做性能优化,我只能说牛逼!
  7. 2016版excel_巧用这些Excel大神技巧,半小时做完一天工作,办公室女神惊呆了!...
  8. 笔记本显示未连接录音服务器,电脑显示未安装音频设备怎么办?
  9. [问题已处理]- kubernetes报错error creating overlay mount to xx merged- no such file or directory
  10. 北京爷们儿跟北京妞儿 倍儿靠谱儿-----女人篇
  11. ACM1008玛雅日历
  12. 自恢复保险丝工作原理
  13. 使用rails Devise
  14. Python当前时间是一年中第几周
  15. SAP 物料标准价和移动平均价详解
  16. UI界面视觉设计之色彩要素
  17. 沪漂程序员的两年,终说再见,你会不会是下一个离开的人?
  18. 祁文之恋——婚纱照——准备
  19. iOS 5增加了200多个新功能
  20. 从华为全联接大会上的金句中,细品数字化转型市场中的铁律

热门文章

  1. Android 弹出 Toast 时取消上一个 Toast(完美方案)增加同步
  2. ActivityManager: Warning: Activity not started, its current task has been brought to the front
  3. android 解决错误:Intel HAXM is required to run this AVD
  4. IBM Java垃圾回收
  5. You are running Composer with SSL/TLS protection disabled.
  6. pyhton3 os模块
  7. 简单区分Vmware的三种网络连接模式(bridged、NAT、host-only)
  8. java实现ftp文件的上传与下载
  9. C语言重要知识点回顾
  10. Android(Java):jni源代码