单调队列经典题目 poj2823

Sliding Window
Time Limit: 12000MS Memory Limit: 65536K
Total Submissions: 36327 Accepted: 10763
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

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;

const int MAX =1000001;
//两个单调队列
int dp1[MAX]; //一个存单调递增
int dp2[MAX];  //一个存单调递减
int a[MAX];

inline bool scan_d(int &num)  //加速
{
    char in;
    bool IsN=false;
    in=getchar();
    if(in==EOF)
        return false;
    while(in!='-' && (in<'0' || in> '9')) in=getchar();
    if(in=='-') {IsN=true;num=0;}
    else num=in-'0';
    while(in=getchar(),in>='0' && in<='9')
    {
        num*=10,num+=in-'0';
    }
    if(IsN)
        num=-num;
    return true;
}

int main()
{
    int i,n,k,front1,front2,tail1,tail2,start,ans;

    while(cin>>n>>k)
    {
        for(i=0;i<n;i++)
            scan_d(a[i]);
        front1=0,tail1=-1;
        front2=0,tail2=-1;
        ans=start=0;
        for(i=0;i<k;i++)
        {
            while(front1<=tail1 && a[dp1[tail1]]<=a[i])
//当前元素大于单调递增队列的队尾元素的时候,队尾的元素依次弹出队列,
//直到队尾元素大于当前元素的时候,将当前元素插入队尾        
                --tail1;
            dp1[++tail1]=i; //只需要记录下标即可
            while(front2<=tail2 && a[dp2[tail2]] >=a[i])
//当前元素小于单调递减队列的队尾元素的时候,队尾的元素依次弹出队列,
//直到队尾元素小于当前元素的时候,将当前元素插入队尾                
                --tail2;
            dp2[++tail2]=i; //只需要记录下标即可
        }
        printf("%d ",a[dp2[front2]]);
        for(;i<n;i++)
        {
            while(front2<=tail2 && a[dp2[tail2]]>=a[i])
                --tail2;
            dp2[++tail2]=i;
            while(dp2[front2]<=i-k)
                ++front2;
            if(i!=n-1)
                printf("%d ",a[dp2[front2]]);
        }
        printf("%d\n",a[dp2[front2]]);
        
        //输出最大值
        printf("%d ",a[dp1[front1]]);
        for(i=k;i<n;i++)
        {
            while(front1<=tail1 && a[dp1[tail1]]<=a[i])
                --tail1;
            dp1[++tail1]=i;
            while(dp1[front1]<=i-k)
                ++front1;
            if(i!=n-1)
                printf("%d ",a[dp1[front1]]);
        }
        printf("%d\n",a[dp1[front1]]);
    }
    return 0;
}

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int inf=-1u>>1;
const int maxn=1e6;
struct node
{
    int val,tag;
    node(int v=0,int t=0):val(v),tag(t){}
    void pt() {printf("%d %d\n",val,tag);}
}q[2][maxn+1];

int h1,r1,h2,r2;
int key[maxn+1],n,k;
int a[maxn+1],b[maxn+1];

int main()
{
    q[0][0]=node(-inf,0);
    q[1][0]=node(+inf,0);
    int i;
    while(cin>>n>>k)
    {
        if(k>n) k=n;
        for(i=1;i<=n;i++) scanf("%d",key+i);
        h1=h2=1;
        r1=r2=0;
        for(i=1;i<k;i++)  //分别初始化
        {
            while(h1<=r1 && q[0][r1].val<key[i])r1--;
            q[0][++r1]=node(key[i],i);
            while(h2<=r2 && q[1][r2].val>key[i])r2--;
            q[1][++r2]=node(key[i],i);
        }

        for(i=k;i<=n;i++)
        {
            while(h1<=r1 && q[0][r1].val<key[i]) r1--;
            q[0][++r1]=node(key[i],i);
            while(h1<=r1 && q[0][h1].tag<=i-k) ++h1;
            a[i-k]=q[0][h1].val;

            while(h2<=r2 && q[1][r2].val>key[i]) r2--;
            q[1][++r2]=node(key[i],i);
            while(h2<=r2 && q[1][h2].tag<=i-k) ++h2;
            b[i-k]=q[1][h2].val;
        }
        for(i=0;i<n-k;i++) cout<<b[i]<<" ";
        cout<<b[i]<<endl;
        for(i=0;i<n-k;i++) cout<<a[i]<<" ";
        cout<<a[i]<<endl;
    }
    return 0;
}


 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值