POJ 2823 Sliding Window(单调队列)

Sliding Window
Time Limit: 12000MS Memory Limit: 65536K
Total Submissions: 56848 Accepted: 16323
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 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
题意;就是i从第k个开始,求i之前k个数(包括i)的最大值和最小值。
思路:其实这跟求单调队列一样只是这里多了一个判定必须是前k个的最大小值。
单调递减队列是这么一个队列,它的头元素一直是队列当中的最大值,而且队列中的值是按照递减的顺序排列的。我们可以从队列的末尾插入一个元素,可以从队列的两端删除元素。
代码:
1》 用单调队列:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
#include<algorithm>
#define fo(i,a,b) for(int i=a;i<=b;i++)
#define fd(i,a,b) for(int i=a;i>=b;i--)
#define inf 0x3f3f3f3f
#define ll long long
#define N 1000001
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
int k,a[N],n,ans[N],ans1[N];
struct node
{
    int num;
    int index;//记录下标
}p[N];
void getmin()
{
    int head=1,tail=0;
    for(int i=1;i<k;i++)//前k-1个数不存在队首下标比i前面k-1个数还要小的情况
    {
        while(head<=tail&&a[i]<p[tail].num) tail--;//如果加入的数比队列前的数小则删除队首元素然后再加入队列
        tail++;
        p[tail].num=a[i];
        p[tail].index=i;
    }
    for(int i=k;i<=n;i++)
    {
        while(head<=tail&&a[i]<p[tail].num) tail--;
        tail++;
        p[tail].num=a[i];
        p[tail].index=i;
        while(i-p[head].index>=k) head++;//如果队首元素不在所查询的下标内就删除
        ans[i-k]=p[head].num;//将求出的最小值存起来方便之后的输出
    }
}
void getmax()//与前面的求最小值类似
{
    int head=1,tail=0;
    for(int i=1;i<k;i++)
    {
        while(head<=tail&&p[tail].num<a[i]) tail--;
        tail++;
        p[tail].num=a[i];
        p[tail].index=i;
    }
    for(int i=k;i<=n;i++)
    {
        while(head<=tail&&p[tail].num<a[i]) tail--;
        tail++;
        p[tail].num=a[i];
        p[tail].index=i;
        while(i-p[head].index>=k) head++;
        ans1[i-k]=p[head].num;
    }
}
int main()
{
    while(~scanf("%d%d",&n,&k))
    {
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
        }
        getmin();getmax();
        for(int i=0;i<=n-k;i++)
        {
            printf("%d ",ans[i]);
        }
        printf("\n");
        for(int i=0;i<=n-k;i++)
        {
            printf("%d ",ans1[i]);
        }
        printf("\n");
    }
}


2》 用优先队列
代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
#include<algorithm>
#define fo(i,a,b) for(int i=a;i<=b;i++)
#define fd(i,a,b) for(int i=a;i>=b;i--)
#define inf 0x3f3f3f3f
#define ll long long
#define N 1000001
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
int n,k,maxx[N],minn[N],a[N];
int t=0,t1=0;
struct cmp1//定义比较算子
{
    bool operator() (const int a1,const int a2)
    {
        return a[a1]>a[a2];//较大的优先出列
    }
};
struct cmp2
{
    bool operator() (const int a1,const int a2)
    {
        return a[a1]<a[a2];//较小的优先出列
    }
};
priority_queue<int,vector<int>, cmp1> q;
priority_queue<int,vector<int>,cmp2 > p;
void judge()
{
    for(int i=0; i<k; i++)//前k个元素入队
    {
        p.push(i);
        q.push(i);
    }
    minn[t++]=a[q.top()];//最小的出队
    maxx[t1++]=a[p.top()];//最大的出队
    for(int i=k; i<n; i++)
    {
        p.push(i);
        q.push(i);
        while(i-q.top()>=k) q.pop();//如果队首元素不在范围内就删除队首元素
        while(i-p.top()>=k) p.pop();
        minn[t++]=a[q.top()];//最小的出队
        maxx[t1++]=a[p.top()];//最大的出队
    }
}
int main()
{
    while(~scanf("%d%d",&n,&k))
    {
        for(int i=0; i<n; i++)
        {
            scanf("%d",&a[i]);
        }
        judge();
        for(int i=0; i<=n-k; i++)
            printf("%d ",minn[i]);
        printf("\n");
        for(int i=0; i<=n-k; i++)
            printf("%d ",maxx[i]);
        printf("\n");
    }
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值