Chinaunix首页 | 论坛 | 博客
  • 博客访问: 343522
  • 博文数量: 122
  • 博客积分: 5000
  • 博客等级: 大校
  • 技术积分: 1191
  • 用 户 组: 普通用户
  • 注册时间: 2009-10-24 11:12
文章分类

全部博文(122)

文章存档

2010年(122)

我的朋友

分类: C/C++

2010-04-05 20:00:12

一、问题描述

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

 

二、解题思路

首先想到的是用堆,用一个最大堆求窗口内的最大值,用一个最小堆求窗口内的最小值。窗口移动一个位置时,将窗口第一个数换成窗口右边的新数,然后重新调整成堆,堆顶元素即是最大值(或最小值)。

a[]保存数组数据,D[]表示最大堆,X[]表示最小堆。实现时需要解决的问题:

1、对于数组中的元素a[i]需要知道在两个堆中该元素的位置,以便在窗口移动时交换元素;

2、堆调整过程中,堆中元素会进行交换,交换时为了保证1的信息要进行更新。为了进行更新,需要知道堆中的元素D[i]X[i])在数组a中的位置。

3、调整堆时要从哪个元素开始调整,调整多少个元素。

对于上述三个问题的解决方法:

1、用两个数组DL[]XL[]来保存元素在堆中的位置。DL[i]表示元素a[i]在最大堆中的下标,XL[i]表示元素a[i]在最小堆中的位置。

2、通过定义堆元素的结构来解决这个问题。

struct Heap

{

                  int x;//堆元素值

                  int ID;//堆元素在数组a[]中的下标

};

3、通过堆的调整算法可以知道,如果换出堆中的元素d,那么需要从d/2位置开始调整直到根元素。

三、代码

 

#include<algorithm>
using namespace std;
int a[1000011];//数组数据
struct Heap
{
    int x;//堆元素值
    int ID;//堆元素在数组a[]中的下标
};
Heap D[1000011];//最大堆
Heap X[1000011];//最小堆

int DL[1000011];//DL[i]表示数组a[i]在D中的下标
int XL[1000011];//XL[i]表示数组a[i]在X中的下标

int OutMin[1000011];//最小值
int OutMax[1000011];//最大值

int n,k;
void HeapAdjust(Heap H[] ,int s,int m,int L[],bool isMax)
{
    Heap rc=H[s];
    int j;
    for(j=s*2;j<=m;j*=2)
    {
        if(isMax)//用于判断是调整为最大堆还是最小堆
        {        
            if(j<m && H[j].x < H[j+1].x)
                j++;
            if(rc.x > H[j].x)break;
            H[s]=H[j];
            L[ H[j].ID ]=s;
            s=j;
        }
        else
        {
            if(j<m && H[j].x > H[j+1].x)
                j++;
            if(rc.x < H[j].x)break;
            H[s]=H[j];
            L[ H[j].ID ]=s;
            s=j;
        }
    }
    H[s]=rc;
    L[rc.ID]=s;
}
void MakeHeap(Heap H[],int length,int L[],bool isMax)
{
    for(int i=length/2;i>0;--i)
    {
        HeapAdjust(H,i,length,L,isMax);
    }
}
int main()
{
    int i;
    int s;
    scanf("%d%d",&n,&k);
    if(k>n)
        k=n;
    for(i=1;i<=k;++i)
    {
        scanf("%d",&a[i]);

        D[i].x=a[i];
        D[i].ID=i;
        DL[i]=i;

        X[i].x=a[i];
        X[i].ID=i;
        XL[i]=i;
    }
    for(i=k+1;i<=n;++i)
    {
        scanf("%d",&a[i]);
    }
    //最大堆和最小堆
    MakeHeap(D,k,DL,true);
    MakeHeap(X,k,XL,false);
    OutMax[0]=D[1].x;
    OutMin[0]=X[1].x;
    s=k/2;
    for(i=k+1;i<=n;++i)
    {        
        int j;
        //计算最大值
        int d=DL[i-k];
        D[d].x=a[i];
        D[d].ID=i;
        DL[i]=d;
        j=d > s ? d/2 : d;
        for(;j>0;j/=2)
        {
            HeapAdjust(D,j,k,DL,true);
        }
        //计算最小值
        int dd=XL[i-k];
        X[dd].x=a[i];
        X[dd].ID=i;
        XL[i]=dd;
        j=dd >s ? dd/2 : dd;
        for(;j>0;j/=2)
        {
            HeapAdjust(X,j,k,XL,false);
        }
        //保存结果
        OutMax[i-k]=D[1].x;
        OutMin[i-k]=X[1].x;

    }
    //输出结果
    for(i=0;i<=(n-k);++i)
    {
        printf("%d%c", OutMin[i], (i < n - k) ? ' ' : '\n');

    }
    for(i=0;i<=(n-k);++i)
    {
        printf("%d%c", OutMax[i], (i < n - k) ? ' ' : '\n');
    }
    return 0;
}


阅读(804) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~