bzoj 1012 [JSOI2008]最大数maxnumber

本文介绍了一道关于单调栈的应用题目,通过使用C++实现单调栈和lower_bound算法来解决该问题。文章提供了完整的代码示例,并展示了如何在算法中高效地处理查询和插入操作。

题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1012

单调栈水题。可以用lower_bound。

但输入不要char ch; cin>>ch。会TLE。(为什么?)

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=2e5+5;
int n,m,mod,stack[N],a[N],top,lst;
int main()
{
    scanf("%d%d",&m,&mod);
    char ch[5];int x;
    while(m--)
    {
        scanf("%s%d",ch,&x);
        if(ch[0]=='A')
        {
            (x+=lst)%=mod;a[++n]=x;
            while(top&&x>=a[stack[top]])top--;
            stack[++top]=n;
        }
        if(ch[0]=='Q')
        {
            int k=lower_bound(stack+1,stack+top+1,n-x+1)-stack;
            lst=a[stack[k]];
            printf("%d\n",lst);
        }
    }
    return 0;
}
lower_bound
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int N=2e5+5;
int n,m,mod,stack[N],ps[N],top,lst;
int main()
{
    scanf("%d%d",&m,&mod);
    char ch[5];int x;
    while(m--)
    {
        scanf("%s%d",ch,&x);
        if(ch[0]=='A')
        {
            (x+=lst)%=mod;
            while(top&&x>=stack[top])top--;
            stack[++top]=x;ps[top]=++n;
        }
        if(ch[0]=='Q')
        {
            int l=1,r=top;
            while(l<=r)
            {
                int mid=((l+r)>>1);
                if(ps[mid]>n-x)lst=mid,r=mid-1;
                else l=mid+1;
            }
            lst=stack[lst];
            printf("%d\n",lst);
        }
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/Narh/p/9248678.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值