POJ2823 Sliding Window

Time Limit: 12000MS   Memory Limit: 65536K
Total Submissions: 53086   Accepted: 15227
Case 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  -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

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 3
1 3 -1 -3 5 3 6 7

Sample Output

-1 -3 -3 -3 3 3
3 3 5 5 6 7

Source

POJ Monthly--2006.04.28, Ikki

模拟滑动区间,用单调队列维护最值即可。本来正常做法是用单调队列存下标,然而蠢蠢的我又多开了俩数组……这样效率会降低,好在还是过了。

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<algorithm>
 4 #include<map>
 5 using namespace std;
 6 const int mxn=2000200;
 7 int n,k;
 8 int num;
 9 int qmx[mxn],qmi[mxn];
10 int lmx[mxn],lmi[mxn];
11 int ansmx[mxn];
12 int main(){
13     scanf("%d%d",&n,&k);
14     int i,j;
15
16     int h1=1,t1=0;
17     int h2=1,t2=0;
18     for(int cnt=1;cnt<=n;cnt++){
19         scanf("%d",&num);
20         //max
21         while(h1<=t1 && num>=qmx[t1])t1--;
22         qmx[++t1]=num;
23         lmx[t1]=cnt;//下标
24
25
26         //min
27         while(h2<=t2 && num<=qmi[t2])t2--;
28         qmi[++t2]=num;
29         lmi[t2]=cnt;
30
31         if(lmx[t1]-lmx[h1]>=k)h1++;
32         if(lmi[t2]-lmi[h2]>=k)h2++;
33 //        printf("test: h1:%d t1:%d\n",h1,t1);
34         ansmx[cnt]=qmx[h1];
35         if(cnt>=k)printf("%d ",qmi[h2]);
36     }
37     printf("\n");
38     for(i=k;i<=n;i++)printf("%d ",ansmx[i]);
39
40     return 0;
41 }
时间: 2024-12-17 00:04:57

POJ2823 Sliding Window的相关文章

POJ2823 Sliding Window (单调队列)

POJ2823 Sliding Window Time Limit: 12000MS   Memory Limit: 65536K Total Submissions: 38342   Accepted: 11359 Case 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 ve

POJ2823 Sliding Window【双端队列】

求连续的k个中最大最小值,k是滑动的,每次滑动一个 用双端队列维护可能的答案值 如果要求最小值,则维护一个单调递增的序列 对一开始的前k个,新加入的如果比队尾的小,则弹出队尾的,直到新加入的比队尾大,加入队尾 从第k+1个到最后一个,按照上述规则,压入新数,然后弹出队首元素(满足队首元素对应原来序列的位置必须在视窗内,否则,继续弹出下一个) #include <cstdio> #include <cstdlib> #include <iostream> #include

POJ2823 Sliding Window(单调队列)

单调队列,我用deque维护.这道题不难写,我第二次写单调队列,1次AC. ----------------------------------------------------------------------------------- #include<cstdio> #include<cstring> #include<deque> #define rep(i,r) for(int i=0;i<r;i++) #define clr(x,c) memset

POJ2823.Sliding Window——单调队列

http://poj.org/problem?id=2823 求长度为k的子序列里面的最大值,最小值 #include <iostream> #include <cstring> #include <cstdio> #include <vector> #include <algorithm> #define pk push_back const int INF = 0x3f3f3f3f; const int maxn = 1000010; usi

POJ2823 Sliding Window (单调队列的基本应用)

题目链接: http://poj.org/problem?id=2823 题意: 给定一个长度为n的序列,求每个长度为k的区间的最大值与最小值 分析: 单调队列的基本应用 代码如下: <span style="font-size:14px;">#include <iostream> #include <cstring> #include <cstdio> #include <queue> #include <vector

ACM-单调队列之Sliding Window——poj2823

Sliding Window Time Limit: 12000MS   Memory Limit: 65536K Total Submissions: 36326   Accepted: 10762 Case 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

poj 2823 Sliding Window (单调队列)

Sliding Window Time Limit: 12000MS   Memory Limit: 65536K Total Submissions: 46705   Accepted: 13485 Case 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

239. Sliding Window Maximum

Given an array nums, 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 right by one position. For example, Given num

Sliding Window Maximum 解答

Question Given an array of n integer with duplicate number, and a moving window(size k), move the window at each iteration from the start of the array, find the maximum number inside the window at each moving. Example For array [1, 2, 7, 7, 8], movin