您的位置:首页 > 其它

Sliding Window poj 2823

2013-03-23 19:37 288 查看
题目链接: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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: