bzoj1012最大数maxnumber——单调栈

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

单调栈水题;用了一下lower_bound二分。

代码如下:

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int m,d,len,a[200005],sta[200005],top,lst;
char s[5];
int main()
{
    scanf("%d%d",&m,&d);
    for(int i=1,x;i<=m;i++)
    {
        scanf("%s%d",&s,&x);
        if(s[0]=='A')
        {
            a[++len]=(x+lst)%d;
            while(top&&a[sta[top]]<a[len])top--;
            sta[++top]=len;
        }
        else
        {
            int k=lower_bound(sta+1,sta+top+1,len-x+1)-sta;//在栈中二分位置 
            lst=a[sta[k]];
            printf("%d\n",lst);
        }
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/Zinn/p/8969959.html
今日推荐