poj 2823 Sliding Window

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

 
 1 //43796K    8766MS    C++    1712B    2013-10-13 21:42:22
 2 /*
 3 
 4     题意:
 5         有n个数字,求每连续的m个中的最大最小数,并分别输出
 6     
 7     线段树:
 8         线段树小变形,不过我的做法好像耗的时空有点大= =、 
 9         query()中加了一个flag判断询问的是最大值还是最小值 
10 
11 */
12 #include<stdio.h>
13 #include<string.h>
14 #include<stdlib.h>
15 #define N 1000005
16 #define inf 0x7ffffff
17 struct node{
18     int l;
19     int r;
20     int nmax;
21     int nmin;
22 }tree[4*N];
23 int p[N],n,m;
24 int ansmax[N];
25 int ansmin[N];
26 int Max(int a,int b)
27 {
28     return a>b?a:b;
29 }
30 int Min(int a,int b)
31 {
32     return a<b?a:b;
33 }
34 void build(int l,int r,int id)
35 {
36     tree[id].l=l;
37     tree[id].r=r;
38     if(l==r){
39         tree[id].nmax=p[l];tree[id].nmin=p[l];
40         return;
41     }
42     int mid=(l+r)/2;
43     build(l,mid,2*id);
44     build(mid+1,r,2*id+1);
45     tree[id].nmax=Max(tree[2*id].nmax,tree[2*id+1].nmax);
46     tree[id].nmin=Min(tree[2*id].nmin,tree[2*id+1].nmin);
47 }
48 int query(int l,int r,int id,bool flag)
49 {
50     if(tree[id].l>r || tree[id].r<l){
51         return flag?inf:-inf;
52     }
53     if(tree[id].l>=l && tree[id].r<=r){
54         return flag?tree[id].nmax:tree[id].nmin;
55     }
56     int mid=(tree[id].l+tree[id].r)/2;
57     if(mid>=r) return query(l,r,2*id,flag);
58     else if(mid<l) return query(l,r,2*id+1,flag);
59     else{
60         if(flag)
61             return Max(query(l,mid,2*id,flag),query(mid+1,r,2*id+1,flag));
62         else return Min(query(l,mid,2*id,flag),query(mid+1,r,2*id+1,flag));
63     }
64 }
65 int main(void)
66 {
67     while(scanf("%d%d",&n,&m)!=EOF)
68     {
69         for(int i=1;i<=n;i++) 
70             scanf("%d",&p[i]);
71         build(1,n,1);
72         for(int i=0;i<=n-m;i++){
73             ansmax[i]=query(i+1,i+m,1,true);
74             ansmin[i]=query(i+1,i+m,1,false);
75         }
76         for(int i=0;i<=n-m;i++)
77             printf(i==n-m?"%d\n":"%d ",ansmin[i]);
78         for(int i=0;i<=n-m;i++)
79             printf(i==n-m?"%d\n":"%d ",ansmax[i]);
80     }
81     return 0;
82 }

 

posted @ 2013-10-13 21:47  heaventouch  阅读(123)  评论(0编辑  收藏  举报