zoukankan      html  css  js  c++  java
  • 最大数maxnumber bzoj1012 JSOI2008 单调队列

    现在请求你维护一个数列,要求提供以下两种操作:

    1、 查询操作。 语法:Q L

    功能:查询当前数列中末尾L个数中的最大的数,并输出这个数的值。

    限制:L不超过当前数列的长度。

    2、 插入操作。

    语法:A n

    功能:将n加上t,其中t是最近一次查询操作的答案(如果还未执行过查询操作,则t=0),并将所得结果对一个固定的常数D取模,将所得答案插入到数列的末尾。

    限制:n是非负整数并且在长整范围内。

    注意:初始时数列是空的,没有一个数。

     单调队列的模版题,摸鱼过题,直接上代码了

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<string>
    #include<iostream>
    using namespace std;
    template<class T> inline void read(T &_a){
        bool f=0;int _ch=getchar();_a=0;
        while(_ch<'0' || _ch>'9'){if(_ch=='-')f=1;_ch=getchar();}
        while(_ch>='0' && _ch<='9'){_a=(_a<<1)+(_a<<3)+_ch-'0';_ch=getchar();}
        if(f)_a=-_a;
    }
    
    int M,D,n,q[200001],r,t,loc[200001],cnt;
    char op;
    
    int main()
    {
        read(M); read(D);
        while(M--)
        {
            op=getchar();
            read(n);
            if(op=='A')
            {
                n+=t;
                n%=D;
                while(n>q[r]&&r) --r;
                q[++r]=n;
                loc[r]=++cnt;
            } else {
                int k=r;
                while(loc[k-1]+n>cnt&&k>1) --k;
                printf("%d
    ",q[k]);
                t=q[k];
            }
        }
        return 0;
    }
  • 相关阅读:
    PAT 05-树7 File Transfer
    PAT 05-树6 Path in a Heap
    PAT 10-2 删除字符串中的子串
    PAT 10-1 在字符串中查找指定字符
    PAT 10-0 说反话
    PAT 08-2 求矩阵的局部最大值
    PAT 07-3 求素数
    PAT 07-2 A+B和C
    PAT 07-0 写出这个数
    PAT 06-3 单词长度
  • 原文地址:https://www.cnblogs.com/jaywang/p/7754941.html
Copyright © 2011-2022 走看看