Sliding Window

来源:互联网 发布:深入浅出node.js百度云 编辑:程序博客网 时间:2024/05/23 01:57
B - Sliding Window
Time Limit:12000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
Submit Status

Description

An array of size n ≤ 10 6 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 -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

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 nintegers 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
AC代码:
#include<iostream>#include<algorithm>#include<cstring>#include<cstdio>using namespace std;#define T 1000100*50int L=1;struct node{int ma,mi;int sta,end;void newnode(int sta=0,int end=0){this->sta = sta;this->end = end;L = ma = mi =0;}}tree[T];void build(int sta,int end,int L){if(::L<L)::L=L;tree[L].newnode(sta,end);if(sta!=end){int mid = (sta+end)>>1;build(sta,mid,L*2);build(mid+1,end,L*2+1);}}void update(int t,int pos,int val){if(tree[t].sta==tree[t].end){tree[t].mi = val;tree[t].ma = val;return;}if(pos<=tree[t*2].end){update(t*2,pos,val);}else{update(t*2+1,pos,val);}tree[t].ma = max(tree[t*2].ma,tree[t*2+1].ma);tree[t].mi = min(tree[t*2].mi,tree[t*2+1].mi);}int query(int t,int sta,int end,int f){if(tree[t].sta==sta&&tree[t].end==end){if(f) return tree[t].ma;return tree[t].mi;}if(end<=tree[t*2].end)return query(t*2,sta,end,f);else if(sta>= tree[t*2+1].sta)return query(t*2+1,sta,end,f);else{int left = query(t*2,sta,tree[t*2].end,f);int right = query(t*2+1,tree[t*2+1].sta,end,f);if(f) return max(left,right);return min(left,right);}}int main(){/*freopen("input.txt","r",stdin);*/int n,m,i,j,k;scanf("%d%d",&n,&m);build(1,n,1);for(i=1;i<=n;++i){scanf("%d",&k);update(1,i,k);}for(j=0;j<=1;++j){for(i=1;i<=n-m+1;++i)printf("%d ",query(1,i,i+m-1,j));printf("\n");}return 0;}


MLE代码:
#include<iostream>#include<algorithm>#include<cstring>#include<cstdio>using namespace std;#define T 1000100struct node{int ma,mi;int sta,end;node* L,*R;node(int sta=0,int end=0){this->sta = sta;this->end = end;ma = mi =0;L = R = NULL;}~node(){if(L)delete L;if(R)delete R;L = R = NULL;}}*tree;node* build(int sta,int end){node* t = new node(sta,end);if(sta!=end){int mid = (sta+end)>>1;t->L = build(sta,mid);t->R = build(mid+1,end);}return t;}void update(node* t,int pos,int val){if(t->sta==t->end){t->mi = val;t->ma = val;return;}if(pos<=t->L->end){update(t->L,pos,val);}else{update(t->R,pos,val);}t->ma = max(t->L->ma,t->R->ma);t->mi = min(t->L->mi,t->R->mi);}int query(node* t,int sta,int end,int f){if(t->sta==sta&&t->end==end){if(f) return t->ma;return t->mi;}if(end<=t->L->end)return query(t->L,sta,end,f);else if(sta>= t->R->sta)return query(t->R,sta,end,f);else{int left = query(t->L,sta,t->L->end,f);int right = query(t->R,t->R->sta,end,f);if(f) return max(left,right);return min(left,right);}}int main(){/*freopen("input.txt","r",stdin);*/int n,m,i,j,k;scanf("%d%d",&n,&m);tree = build(1,n);for(i=1;i<=n;++i){scanf("%d",&k);update(tree,i,k);}for(j=0;j<=1;++j){for(i=1;i<=n-m+1;++i)printf("%d ",query(tree,i,i+m-1,j));printf("\n");}delete tree;return 0;}


0 0
原创粉丝点击