POJ2823:Sliding Window

点击打开题目链接


Sliding Window

Time Limit: 12000MS   Memory Limit: 65536K
Total Submissions: 30481   Accepted: 9089
Case Time Limit: 5000MS

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 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


=====================================题目大意=====================================


一个长度为K的滑窗在有着N个数字的窗户上由左向右每次滑动一个位置,输出每个位置时滑窗上数字的最小值与最大值。


=====================================算法分析=====================================


一、线段树。


二、单调队列。


=======================================代码=======================================


一、线段树




#include<stdio.h>
#include<string.h>

#define Lson(n)  (n<<1  )
#define Rson(n)  (n<<1|1)
#define MID(L,R) ((L)+(R)>>1)
#define MIN(a,b) ((a)<(b)?(a):(b))
#define MAX(a,b) ((a)>(b)?(a):(b))

const int MAXN=1000005;

int N,K,Num[MAXN];

struct node { int max,min; }SegmTree[MAXN<<2],Ans[MAXN];

void Build(int L,int R,int N)
{
	if(L==R)
	{
		Ans[L].min=SegmTree[N].min=Num[L];
		Ans[L].max=SegmTree[N].max=Num[L];
	}
	else
	{
		int M=MID(L,R);
        Build(L,M,Lson(N));
        Build(M+1,R,Rson(N));
        SegmTree[N].max=MAX(SegmTree[Lson(N)].max,SegmTree[Rson(N)].max);
        SegmTree[N].min=MIN(SegmTree[Lson(N)].min,SegmTree[Rson(N)].min);
	}
}

void Query(int L,int R,int N,int Q)
{
    if(!(Q+K-1<L||R<Q))
    {
        if(Q<=L&&R<=Q+K-1)
        {
            Ans[Q].max=MAX(Ans[Q].max,SegmTree[N].max);
            Ans[Q].min=MIN(Ans[Q].min,SegmTree[N].min);
        }
        else
        {
            int M=MID(L,R);
            Query(L,M,Lson(N),Q);
            Query(M+1,R,Rson(N),Q);
        }
    }
}

int main()
{
	while(scanf("%d%d",&N,&K)==2)
	{
	    for(int i=0;i<N;++i)
	    {
	        scanf("%d",&Num[i]);
	    }
		Build(0,N-1,1);
		for(int i=0;i+K-1<N;++i)
		{
		    Query(0,N-1,1,i);
		}
		for(int i=0;i+K-1<N;++i)
		{
		    printf("%d%c",Ans[i].min,i+K<N?' ':'\n');
		}
		for(int i=0;i+K-1<N;++i)
		{
		    printf("%d%c",Ans[i].max,i+K<N?' ':'\n');
		}
	}
	return 0;
}


二、单调队列




#include<stdio.h>

const int MAXN=1000005;

int N,K,num[MAXN];

int maxq_node[MAXN],maxq_head,maxq_tail,max_ans[MAXN];

int minq_node[MAXN],minq_head,minq_tail,min_ans[MAXN];

int main()
{
    while(scanf("%d%d",&N,&K)==2)
    {
        for(int i=0;i<N;++i)
        {
            scanf("%d",&num[i]);
        }
        minq_head=maxq_head=0;
        minq_tail=maxq_tail=0;
        for(int i=0;i<N;++i)
        {
            while(minq_head<minq_tail&&num[i]<num[minq_node[minq_tail-1]]) --minq_tail;
            minq_node[minq_tail++]=i;
            while(maxq_head<maxq_tail&&num[i]>num[maxq_node[maxq_tail-1]]) --maxq_tail;
            maxq_node[maxq_tail++]=i;
            if(i-K+1>=0)     
            {
                min_ans[i-K+1]=num[minq_node[minq_head]];
                while(minq_head<minq_tail&&minq_node[minq_head]<=i-K+1) ++minq_head;
                max_ans[i-K+1]=num[maxq_node[maxq_head]];
                while(maxq_head<maxq_tail&&maxq_node[maxq_head]<=i-K+1) ++maxq_head;
            }
        }
        for(int i=0;i<=N-K;++i)
        {
            printf("%d%c",min_ans[i],i<N-K?' ':'\n');
        }
        for(int i=0;i<=N-K;++i)
        {
            printf("%d%c",max_ans[i],i<N-K?' ':'\n');
        }
    }
    return 0;
}

你可能感兴趣的:(数据结构,线段树,单调队列)