zoukankan      html  css  js  c++  java
  • 【BZOJ 1012】 [JSOI2008]最大数maxnumber

    1012: [JSOI2008]最大数maxnumber

    Time Limit: 3 Sec  Memory Limit: 162 MB
    Submit: 5960  Solved: 2579
    [Submit][Status][Discuss]

    Description

    现在请求你维护一个数列,要求提供以下两种操作: 1、 查询操作。语法:Q L 功能:查询当前数列中末尾L个数中的最大的数,并输出这个数的值。限制:L不超过当前数列的长度。 2、 插入操作。语法:A n 功能:将n加上t,其中t是最近一次查询操作的答案(如果还未执行过查询操作,则t=0),并将所得结果对一个固定的常数D取模,将所得答案插入到数列的末尾。限制:n是非负整数并且在长整范围内。注意:初始时数列是空的,没有一个数。

    Input

    第一行两个整数,M和D,其中M表示操作的个数(M <= 200,000),D如上文中所述,满足(0

    Output

    对于每一个查询操作,你应该按照顺序依次输出结果,每个结果占一行。

    Sample Input

    5 100
    A 96
    Q 1
    A 97
    Q 1
    Q 2

    Sample Output

    96
    93
    96
     
    二分查找+单调栈。
    把一个元素插入单调栈,维护单调栈的不上升的单调性。
    我们可以想象,如果想查找的编号在栈中找不到表示什么?表示当前编号到序列末尾不是最大值。(想一想,为什么?)
    找不到我们可以在栈中寻找一个比当前编号大的最接近当前编号的元素。
    如果找到就更简单了,直接输出即可。
    #include<cstdio>
    #include<cstring>
     
    using namespace std;
     
    const int MAXN = 200005;
    const int INF = 0x7fffffff;
     
    int stn[MAXN];
    int num[MAXN];
    int top;
    int m;
    int d;
    int t;
    int q; /* 查询编号 */
    int n;
     
    int binarySearch(int l, int r)
    {
        if (l == r)
        {
            if (num[l] >= q) return stn[l];
            else return stn[l + 1]; 
        }
        int mid = (l + r) >> 1;
        if (num[mid] >= q) return binarySearch(l, mid);
        else return binarySearch(mid + 1, r);
    }
     
    int main()
    {
        scanf("%d%d", &m, &d);
        memset(stn, 0, sizeof(stn));
        stn[top = 1] = INF;
        num[top = 1] = 0;
        int t = 0;
        n = 0;
        while (m--)
        {
            char cmd[3];
            int x;
            scanf("%s%d", cmd, &x);
            if (cmd[0] == 'A')
            {
                x = (x + t) % d;
                while (stn[top] < x) top--;
                stn[++top] = x;
                num[top] = ++n;
            }
            else
            {
                q = n - x + 1;
                t = binarySearch(1, top);
                printf("%d
    ", t);
            }
        }
        return 0;
    }
  • 相关阅读:
    游戏中调用SDK提供的支付接口 头文件的包含
    sdk支付结果 调用游戏中的回调
    escplise 下新添加c++代码的处理
    rapidjson的read和write的sample
    cocos2d-x 3.0 场景切换特效汇总
    Eclipse 打开文件所在位置
    eclipse及其Java环境搭理
    rust cargo build一直出现 Blocking waiting for file lock on package cache
    rust随笔
    cmake 安装一个目录下的图片 到另一个目录文件中去
  • 原文地址:https://www.cnblogs.com/albert7xie/p/4760155.html
Copyright © 2011-2022 走看看