zoukankan      html  css  js  c++  java
  • 单调队列 poj2823

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

    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 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 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     范围在区间【1,le6】    

    下一行有n个数, 

    依次输出每一个区间长度为k的           最小值

      另一行依次输出区间长度为k的    最大值

     

     

     

    解: 

      显然这里用的是优先队列,又名单调队列。

      顾名思义就是单调递增,或者是单调递减的队列。

     

        至于单调队列最入门的用法,自然是求数组中的最小值或者最大值,这里求最大值为例。

    #include<queue>
    int main()
    {
        int a[15];
        for (int i=1;i<=10;i++){
            a[i]=rand()%100+1;
        }
        for (int i=1;i<=10;i++){
            printf("a[%d]=%d
    ",i,a[i]);
        }
        queue<int>q;
        while (!q.empty())
            q.pop();
        q.push(a[1]);
        for (int i=2;i<=10;i++){
            if (q.front()<a[i]){
                q.pop();
                q.push(a[i]);
            }
        }
        cout<<"最大值为:"<<q.front()<<endl;
    }

     

     

    回到正题,对于上题。

    AC代码:   (神奇的地方:用G++提交会超时,用C++提交能AC)

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cstdlib>
    #include<cmath>
    #include<queue>
    #include<stack>
    #include<map> 
    #include<algorithm>
    #define Max(a,b) ((a)>(b)?(a):(b))
    #define Min(a,b) ((a)<(b)?(a):(b))
    #define Mem0(x) memset(x,0,sizeof(x))
    #define Mem1(x) memset(x,-1,sizeof(x))
    #define MemX(x) memset(x,0x3f,sizeof(x))
    using namespace std;
    typedef long long ll;
    const int inf=0x3f3f3f;
    const double pi=acos(-1.0);
    
    const int MAX=1e6+10;
    struct s{
        int val,id;
    };
    s minq[MAX],maxq[MAX];
    int maxhead,maxtail,minhead,mintail;
    int maxans[MAX],minans[MAX];
    int cnt;
     
    int main()
    {
        int n,k,tmp;
        scanf("%d%d",&n,&k);//    cin>>n>>k;
        cnt=0;
        maxhead=maxtail=minhead=maxtail=0;
        for(int i=0;i<k;i++){
            scanf("%d",&tmp);//cin>>tmp;
            while(maxhead<maxtail&&maxq[maxtail-1].val<=tmp)//递减队列 
                maxtail--;
            maxq[maxtail].val=tmp;
            maxq[maxtail++].id=i;
            
            while(minhead<mintail&&minq[mintail-1].val>=tmp)//递增队列
                mintail--;
            minq[mintail].val=tmp;
            minq[mintail++].id=i;    
        }
        
        for (int i=k;i<n;i++){
            maxans[cnt]=maxq[maxhead].val;
            minans[cnt++]=minq[minhead].val;
            scanf("%d",&tmp);//cin>>tmp;
            while (minhead<mintail&&i-minq[minhead].id>=k)
                minhead++;
            while (minhead<mintail&&minq[mintail-1].val>=tmp)
                mintail--;
            minq[mintail].val=tmp;
            minq[mintail].id=i;
            mintail++;
    
            while (maxhead < maxtail && i - maxq[maxhead].id >= k) ++maxhead;
            while (maxhead < maxtail && maxq[maxtail - 1].val <= tmp) --maxtail;
            maxq[maxtail].val = tmp;
            maxq[maxtail].id = i;
            ++maxtail;        
        }
        minans[cnt] = minq[minhead].val;
        maxans[cnt] = maxq[maxhead].val;
        ++cnt;
        for (int i=0;i<cnt;i++){
            if (i==0)
                printf("%d",minans[i]);
            else
                printf(" %d",minans[i]);
        }
        cout<<endl;
        for (int i=0;i<cnt;i++){
            if (i==0)
                printf("%d",maxans[i]);
            else
                printf(" %d",maxans[i]);
        }
        cout<<endl;
        return 0;
    }

     

  • 相关阅读:
    Sublime Text 无法使用Package Control或插件安装失败的解决方法
    phpstorm破解
    require
    在线支付
    解决华为手机用rem单位,内容超出屏幕宽度问题
    JS如何判断是不是iphoneX
    iPhoneX页面安全区域与内容重叠问题
    .NET 大数据量并发解决方案
    js 弹出div窗口 可移动 可关闭
    colgroup 整行变色
  • 原文地址:https://www.cnblogs.com/q1204675546/p/10800118.html
Copyright © 2011-2022 走看看