码迷,mamicode.com
首页 > Windows程序 > 详细

POJ2823 Sliding Window

时间:2017-09-03 16:38:49      阅读:253      评论:0      收藏:0      [点我收藏+]

标签:war   utc   cstring   ios   而不是   numbers   enter   sed   tab   

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

 
【题解】
单调队列裸题
 
选C++而不是g++就能不TLE了
技术分享
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cstdlib>
 5 
 6 inline void read(int &x)
 7 {
 8     x = 0;char ch = getchar(), c = ch;
 9     while(ch < 0 || ch > 9)c = ch, ch = getchar();
10     while(ch <= 9 && ch >= 0)x = x * 10 + ch - 0, ch = getchar();
11     if(c == -)x = -x;
12 }
13 
14 const int INF = 0x3f3f3f3f;
15 const int MAXN = 2000000 + 10;
16 const int MAXM = 2000000 + 10;
17 
18 int num[MAXN],q[MAXN],rank[MAXN],head,tail,n,m;
19 
20 int main()
21 {
22     read(n), read(m);
23     for(register int i = 1;i <= n;++ i) read(num[i]);
24     
25     head = tail = 1, q[1] = num[1],rank[1] = 1;
26     
27     //维护单调递增队列 
28     for(register int i = 2;i <= m;++ i)
29     {
30         while(num[i] <= q[tail] && head <= tail) -- tail;
31         q[++tail] = num[i];
32         rank[tail] = i;
33     }
34     printf("%d ", q[head]);
35     for(register int i = m + 1;i <= n;++ i)
36     {
37         while(num[i] <= q[tail] && head <= tail) -- tail;
38         while(i - rank[head] + 1 > m && head <= tail)++ head;
39         q[++tail] = num[i];
40         rank[tail] = i;
41         printf("%d ", q[head]);
42     }
43     putchar(\n);
44     
45     head = tail = 1, q[1] = num[1],rank[1] = 1;
46     
47     //维护单调递减队列 
48     for(register int i = 2;i <= m;++ i)
49     {
50         while(num[i] >= q[tail] && head <= tail) -- tail;
51         q[++tail] = num[i];
52         rank[tail] = i;
53     }
54     printf("%d ", q[head]);
55     for(register int i = m + 1;i <= n;++ i)
56     {
57         while(num[i] >= q[tail] && head <= tail) -- tail;
58         while(i - rank[head] + 1 > m && head <= tail)++ head;
59         q[++tail] = num[i];
60         rank[tail] = i;
61         printf("%d ", q[head]);
62     }
63     return 0;
64 }
POJ2823

 

POJ2823 Sliding Window

标签:war   utc   cstring   ios   而不是   numbers   enter   sed   tab   

原文地址:http://www.cnblogs.com/huibixiaoxing/p/7469801.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!