zoukankan      html  css  js  c++  java
  • poj2823:单调队列入门题

    今天学习了一下单调队列这种数据结构,思想不是很难

    参考资料:http://www.cnblogs.com/Jason-Damon/archive/2012/04/19/2457889.html

    然后自己写成了类的模板形式,并做了例题poj2823

    代码如下:

    #include <iostream>
    #include <stdio.h>
    #include<string.h>
    #include<algorithm>
    #include<string>
    #include<ctype.h>
    using namespace std;
    #define maxn 10000010
    typedef struct Node
    {
        int val;
        int num;
    }node;
    typedef struct iqueue
    {
        node q[maxn];
        int l,r;
        void ini()
        {
            l=0;
            r=0;
        }
        node front()
        {
            return q[l];
        }
        node pop()
        {
            l++;
            return q[l-1];
        }
        void push(node x)
        {
            if(r==l)
            {
                q[r++]=x;
                return;
            }
            if(x.val>=q[l].val)
            {
                r=l;
                q[r++]=x;
                return;
            }
            while(r>=1&&x.val>=q[r-1].val)
            {
                r--;
            }
            q[r++]=x;
        }
    }Iqueue;
    typedef struct dqueue
    {
        node q[maxn];
        int l,r;
        void ini()
        {
            l=0;
            r=0;
        }
        node front()
        {
            return q[l];
        }
        node pop()
        {
            l++;
            return q[l-1];
        }
        void push(node x)
        {
            if(r==l)
            {
                q[r++]=x;
                return;
            }
            if(x.val<=q[l].val)
            {
                r=l;
                q[r++]=x;
                return;
            }
            while(r>=1&&(x.val<=q[r-1].val))
            {
                r--;
            }
            q[r++]=x;
        }
    }Dqueue;
    int big[1000010];
    int small[1000010];
    Iqueue qi;
    Dqueue qd;
    int main()
    {
        int n,k;
        while(scanf("%d%d",&n,&k)!=EOF)
        {
            node x;
            node s,b;
            int t=0;
            qi.ini();
            qd.ini();
            for(int i=1;i<=n;i++)
            {
                scanf("%d",&x.val);
                x.num=i;
                qi.push(x);
                qd.push(x);
                if(i>=k)
                {
                    b=qi.front();
                    while(b.num<=i-k)
                    {
                        qi.pop();
                        b=qi.front();
                    }
                    s=qd.front();
                    while(s.num<=i-k)
                    {
                        qd.pop();
                        s=qd.front();
                    }
                    big[t]=b.val;
                    small[t++]=s.val;
                }
            }
            for(int i=0;i<t;i++)
            {
                printf("%d",small[i]);
                if(i==t-1)
                    puts("");
                else
                    printf(" ");
            }
            for(int i=0;i<t;i++)
            {
                printf("%d",big[i]);
                if(i==t-1)
                    puts("");
                else
                    printf(" ");
            }
        }
        return 0;
    }
  • 相关阅读:
    flutter开发环境的搭建
    创建一个android项目
    android studio 安装与配置
    sentinel-dashboard.jar 安装
    三:nacos的配置中心
    二:nacos 的服务注册
    spring boot 在windows下的 批文件部署
    一:nacos 的安装与启动方式
    mysql 命令行安装方式
    Git 出现 Permission denied 时,重新生成ssh密钥
  • 原文地址:https://www.cnblogs.com/oneshot/p/4021063.html
Copyright © 2011-2022 走看看