poj 2823 Sliding Window(单调队列)

Sliding Window
Time Limit: 12000MS
Memory Limit: 65536K
Total Submissions: 50906
Accepted: 14640
Case Time Limit: 5000MS

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

大致题意:还是解释输入输出。第一行输入两个数n,k,第二行输入n个数。现在有一个长度为k的窗口,从第一个数开始滑动,每次只有窗口范围中的数字可见。输出两行,第一行是每次窗口中的最小数,第二行是每次窗口中的最大数。


思路:单调队列。当时还是不知道什么是单调队列==于是百度。。。百科里的例题就是这个,恩,还参考过这个解题报告(http://blog.csdn.net/justmeh/article/details/5844650)的简单例子才彻底明白怎么维护一个单调队列,下面引用百科里的话:进队时,将进队的元素为e,从队尾往前扫描,直到找到一个不大于e的元素d,将e放在d之后,舍弃e之后的所有元素;如果没有找到这样一个d,则将e放在队头(此时队列里只有这一个元素)。 然后后面的就没怎么看了==这题基本上就用到这句话,然后有了思路之后我是用stl的deque做的这题(一开始用queue写的差不多了才发现不能删掉最后的数。。。于是重新改),但是看到很多大神直接用个数组模拟就好了orz。。。以后有机会的话也试试看数组好了

#include <iostream>
#include <cstdio>
#include <deque>
#include <algorithm>
#define N 1000000
using namespace std;
typedef pair<int,int> mp;
deque<mp> fmax;
deque<mp> fmin;
int rmax[N],rmin[N];

int main()
{
    int n,k,i,num;
    mp t1,t2;
    scanf("%d %d",&n,&k);
    scanf("%d",&num);
    t1.first=0;
    t1.second=num;
    fmax.push_back(t1);
    fmin.push_back(t1);
    if(k==1){rmin[0]=rmax[0]=t1.second;}
    for(i=1;i<n;i++)
    {
        scanf("%d",&num);
        t1.first=i;
        t1.second=num;
        t2=fmax.front();
        if(t2.first<i-k+1)fmax.pop_front();
        t2=fmin.front();
        if(t2.first<i-k+1)fmin.pop_front();
        while(!fmax.empty()&&fmax.back().second<num)fmax.pop_back();
        while(!fmin.empty()&&fmin.back().second>num)fmin.pop_back();
        fmax.push_back(t1);
        fmin.push_back(t1);
        if(i>=k-1)//这里这么写i不一定是从k-1开始,而有可能是从一个大于k-1的数开始的,而k=1就是这种情况。。所以前面要多算一个!!就是这个地方WA了==
        {
            rmax[i-k+1]=fmax.front().second;
            rmin[i-k+1]=fmin.front().second;
        }
    }
    printf("%d",rmin[0]);
    for(i=1;i<n-k+1;i++)printf(" %d",rmin[i]);
    printf("\n%d",rmax[0]);
    for(i=1;i<n-k+1;i++)printf(" %d",rmax[i]);
    printf("\n");
    return 0;
}

























  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值