POJ 2823 Sliding Window(单调队列)
来源:互联网 发布:如何加入淘宝热卖 编辑:程序博客网 时间:2024/06/13 12:32
Sliding Window
Time Limit: 12000MS Memory Limit: 65536KTotal Submissions: 61914 Accepted: 17733Case Time Limit: 5000MS
Description
An array of size n ≤ 106 is given to you. There is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window moves rightwards by one position. Following is an example:
The array is [1 3 -1 -3 5 3 6 7], and k is 3.Window position Minimum value Maximum value [1 3 -1] -3 5 3 6 7 -13 1 [3 -1 -3] 5 3 6 7 -33 1 3 [-1 -3 5] 3 6 7 -35 1 3 -1 [-3 5 3] 6 7 -35 1 3 -1 -3 [5 3 6] 7 36 1 3 -1 -3 5 [3 6 7]37
The array is [1 3 -1 -3 5 3 6 7], and k is 3.
Your task is to determine the maximum and minimum values in the sliding window at each position.
Input
The input consists of two lines. The first line contains two integers n and k which are the lengths of the array and the sliding window. There are n integers in the second line.
Output
There are two lines in the output. The first line gives the minimum values in the window at each position, from left to right, respectively. The second line gives the maximum values.
Sample Input
8 31 3 -1 -3 5 3 6 7
Sample Output
-1 -3 -3 -3 3 33 3 5 5 6 7
Source
POJ Monthly--2006.04.28, Ikki
题意:
滑动窗口,一次只能看k个数字,求每次窗口中的最大值最小值。
POINT:
单调队列。
#include <iostream>#include <stdio.h>#include <string.h>#include <math.h>#include <vector>#include <algorithm>using namespace std;#define LL long longconst int N = 1000000+10;struct node{ int x,y;}v[N];int a[N],n,k;int ans[N];void getmax(){ int cnt=0; int head=1,end=0; for(int i=1;i<k;i++) { while(head<=end&&v[end].x<=a[i]) end--; v[++end].x=a[i],v[end].y=i; } for(int i=k;i<=n;i++) { while(head<=end&&v[end].x<=a[i]) end--; v[++end].x=a[i],v[end].y=i; while(i-v[head].y>=k) head++; ans[++cnt]=v[head].x; } for(int i=1;i<=cnt;i++) { if(i-1) printf(" "); printf("%d",ans[i]); } printf("\n");}void getmin(){ int cnt=0; int head=1,end=0; for(int i=1;i<k;i++) { while(head<=end&&v[end].x>=a[i]) end--; v[++end].x=a[i],v[end].y=i; } for(int i=k;i<=n;i++) { while(head<=end&&v[end].x>=a[i]) end--; v[++end].x=a[i],v[end].y=i; while(i-v[head].y>=k) head++; ans[++cnt]=v[head].x; } for(int i=1;i<=cnt;i++) { if(i-1) printf(" "); printf("%d",ans[i]); } printf("\n"); }int main(){ scanf("%d %d",&n,&k); for(int i=1;i<=n;i++) { scanf("%d",&a[i]); } getmin(); getmax(); }
阅读全文
0 0
- poj 2823 Sliding Window(单调队列)
- POJ 2823 Sliding Window(单调队列)
- POJ 2823 Sliding Window(单调队列)
- poj 2823 Sliding Window(单调队列)
- poj 2823 Sliding Window ( 单调队列 )
- POJ 2823 Sliding Window (单调队列)
- poj 2823 Sliding Window(单调队列)
- poj - 2823 - Sliding Window(单调队列)
- POJ 2823 Sliding Window (单调队列)
- POJ 2823 Sliding Window(单调队列)
- POJ 2823 Sliding Window(单调队列)
- POJ 2823 Sliding Window(单调队列)
- POJ 2823 Sliding Window (单调队列)
- POJ 2823 Sliding Window(单调队列)
- poj 2823 Sliding Window(单调队列)
- POJ 2823 Sliding Window(单调队列)
- [POJ 2823] Sliding Window (单调队列)
- 【POJ 2823】Sliding Window(单调队列)
- HEVC_帧内
- POJ
- SpringMVC与Struts2 的区别!
- Python静态方法(staticmethod)和类方法(classmthod)区别
- JAVA 实现队列功能
- POJ 2823 Sliding Window(单调队列)
- python学习笔记之004.py
- Spark集群工作异常,无法读取Hadoop集群文件处理办法
- yii2.0-captcha验证码--详细设置
- 【深入理解Java虚拟机学习笔记】虚拟机类加载机制
- 一台电脑安装多个mysql(mysql修改密码)
- 解决BCGControlbar中Ribbon界面主按钮一直显示File的问题
- MRv1 VS MRv2
- CgLib动态代理初步实践