POJ 2823

Sliding Window
Time Limit: 12000MS   Memory Limit: 65536K
Total Submissions: 35941   Accepted: 10636
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 positionMinimum valueMaximum 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

 
RMQ 
 
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 
 6 using namespace std;
 7 
 8 const int MAX_N = 1e6 + 5;
 9 int N,K;
10 int  mi[MAX_N],a[MAX_N],ma[MAX_N];
11 int k;
12 
13 void RMQ() {
14         for(int j = 1; j <= k; ++j) {
15                 for(int i = 1; i + (1 << j) - 1 <= N; ++i) {
16                         mi[i] = min(mi[i],mi[i + (1 << (j - 1))]);
17                         ma[i] = max(ma[i],ma[i + (1 << (j - 1))]);
18                 }
19         }
20 }
21 
22 int main()
23 {
24     //freopen("sw.in","r",stdin);
25     while(~scanf("%d%d",&N,&K)) {
26             for(int i = 1; i <= N; ++i) {
27                     scanf("%d",&a[i]);
28                     ma[i] = mi[i] = a[i];
29             }
30 
31             k = 0;
32             while((1 << (k + 1)) < K) ++k;
33             RMQ();
34             for(int i = 1; i <= N - K + 1; ++i) {
35                     printf("%d%c",min(mi[i],mi[i + K - (1 << k)]),i == N - K + 1 ? '\n' : ' ');
36             }
37 
38             for(int i = 1; i <= N - K + 1; ++i) {
39                     printf("%d%c",max(ma[i],ma[i + K - (1 << k)]),i == N - K + 1 ? '\n' : ' ');
40             }
41 
42     }
43 
44     return 0;
45 }
View Code

 

 

单调队列

 

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 
 6 using namespace std;
 7 
 8 const int MAX_N = 1e6 + 5;
 9 int N,K;
10 int a[MAX_N];
11 int mi[2 * MAX_N],ma[2 * MAX_N];
12 int ans[MAX_N];
13 
14 void solve() {
15         int s = 0,e = 0;
16         for(int i = 1; i <= N; ++i) {
17                 while(s < e && a[i] < a[ mi[e - 1] ]) --e;
18                 mi[e++] = i;
19                 if(i - K + 1 >= 1) {
20                         ans[i - K + 1] = a[ mi[s] ];
21                 }
22                 if(mi[s] == i - K + 1) ++s;
23         }
24 
25         for(int i = 1; i + K - 1 <= N; ++i) {
26                 printf("%d%c",ans[i],i == N - K + 1 ? '\n' : ' ');
27         }
28 
29         s = 0,e = 0;
30         for(int i = 1; i <= N; ++i) {
31                 while(s < e && a[i] > a[ ma[e - 1] ]) --e;
32                 ma[e++] = i;
33                 if(i - K + 1 >= 1) {
34                         ans[i - K + 1] = a [ ma[s] ];
35                 }
36                 if(ma[s] == i - K + 1) ++s;
37         }
38 
39         for(int i = 1; i + K - 1 <= N; ++i) {
40                 printf("%d%c",ans[i],i == N - K + 1 ? '\n' : ' ');
41         }
42 
43 }
44 
45 int main() {
46         while(~scanf("%d%d",&N,&K)) {
47                 for(int i = 1; i <= N; ++i) {
48                         scanf("%d",&a[i]);
49                 }
50 
51                 solve();
52         }
53 
54         return 0;
55 
56 }
View Code

 

posted @ 2014-04-17 00:20  hyx1  阅读(176)  评论(0编辑  收藏  举报