Sliding Window poj 2823

题目链接:http://poj.org/problem?id=2823

Sliding Window

Time Limit: 12000MS

Memory Limit: 65536K

Total Submissions: 29378

Accepted: 8734

Case Time Limit: 5000MS

Description

An array ofsize n ≤ 106 is given to you. There is asliding window of size k which is moving from the very left ofthe array to the very right. You can only see the k numbers inthe 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 is3.

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 andminimum values in the sliding window at each position. 

Input

The input consistsof two lines. The first line contains two integers n and k whichare the lengths of the array and the sliding window. There are n integersin the second line. 

Output

There are twolines in the output. The first line gives the minimum values in the window ateach position, from left to right, respectively. The second line gives themaximum 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

 

题意:这是一个滑动窗口的题目,可以用线段树来解决,但是我这里是用双端队列实现,引入一个次小(大)值,计算当当前最小(大)值无效后,取代的下一个最小(大)值

代码:

#include <iostream>
#include<stdio.h>
#include<deque>

#define maxn 1000005
using namespace std;
int a[maxn];
int main()
{
    int max,min;
    int nextmax,nextmin;
    int i,j;
    int n,k;
    int index=0;//当前最值的位置
    deque<int> de;

    scanf("%d%d",&n,&k);
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
    //求最小值和第二小的值,当最小值在范围之外,那么就将第二小的值的取代最小值
    max=min=nextmax=nextmin=a[0];
    index=0;//最大(小)的下标位置
    int indexNext=0;//第二大(小)的下标位置
    int flag=0;
    //滑动窗口开始滑动(最小值)
    for(i=0;i<n;i++)
    {
        de.push_back(i);//插入下标
        if(min>=a[i])
        {//并且最小值的下标还有效在区间内
            min=a[i];
            index=i;
            flag=0;
        }
        else
        {
            if(a[i]>min)
            {
                if((a[i]<nextmin && flag==1) || flag==0)
                {//得到第二小的值
                    nextmin=a[i];
                    indexNext=i;
                    flag=1;
                }

            }
            //如果最小的值在范围之外,那么久将第二小的值取代最小值
            if((i-index)>=k)
            {
                index =indexNext ;
                min=nextmin;
                flag=0;
				indexNext++;
				nextmin = a[indexNext];
				for(j=index+1;j<=i;j++)
				{
					if(a[j]<=nextmin)
					{
						nextmin = a[j];
						indexNext = j;
						flag=1;
					}
				}
            }
        }

        if(i>=(k-1))
        {
			if(i==(n-1))
			{
				printf("%d",min);
				de.pop_front();
			}
			else
			{

				printf("%d ",min);
				de.pop_front();
			}
        }
    }

    index=0;
    indexNext=0;
    flag=0;
    printf("\n");

        //滑动窗口开始滑动(最大值)
    for(i=0;i<n;i++)
    {
        de.push_back(i);//插入下标
        if(max<=a[i])
        {//并且最小值的下标还有效在区间内
            max=a[i];
            index=i;
            flag=0;
        }
        else
        {
            if(a[i]<max)
            {
                if((a[i]>nextmax && flag==1) || flag==0)
                {
                    nextmax=a[i];
                    indexNext=i;
                    flag=1;
                }

            }
            if((i-index)>=k)
            {
                index =indexNext ;
                max=nextmax;
				flag=0;
				indexNext++;
				nextmax = a[indexNext];
				for(j=index+1;j<=i;j++)
				{
					if(a[j]>=nextmax)
					{
						nextmax = a[j];
						indexNext = j;
						flag=1;
					}
				}
            }
        }

        if(i>=(k-1))
        {
			if(i==(n-1))
			{
				printf("%d",max);
				de.pop_front();
			}
			else
			{
				printf("%d ",max);
				de.pop_front();
			}

        }

    }
	printf("\n");

    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

疯的世界

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值