BZOJ1012: [JSOI2008]最大数maxnumber

BZOJ1012: [JSOI2008]最大数maxnumber

单调栈

维护一个单调下降的单调栈,栈里面维护的是下标

二分查找答案

/**************************************************************
    Problem: 1012
    User: solvit
    Language: C++
    Result: Accepted
    Time:468 ms
    Memory:2852 kb
****************************************************************/
 
#include<bits/stdc++.h>
 
using namespace std;
const int maxn = 200005;
int m,d,n,t,len,top;
char op;
int num[maxn],a[maxn];
int main()
{
    scanf("%d%d",&m,&d);
    getchar();
    for(int _ = 1; _ <= m; _++)
    {
        scanf("%c%d",&op,&n);
        getchar();
        if(op=='A')
        {
            n=(n+t)%d;
            num[++len]=n;
            while(top&&num[a[top]]<=n)top--;
            a[++top]=len;
        }
        else{
            int y=lower_bound(a+1,a+top+1,len-n+1)-a;
            t=num[a[y]];
            printf("%d\n",t=num[a[y]]);
        }
    }
 
    return 0;
}
View Code

 

听说暴力也可以~

/**************************************************************
    Problem: 1012
    User: solvit
    Language: C++
    Result: Accepted
    Time:456 ms
    Memory:2852 kb
****************************************************************/
 
#include<bits/stdc++.h>
 
using namespace std;
const int maxn = 200005;
int m,d,a[maxn],t,ans[maxn],l,p;
char op;
int main()
{
    scanf("%d%d",&m,&d);getchar();
    for(int _ = 1; _ <= m; _++)
    {
        scanf("%c%d",&op,&p);getchar();
        if(op=='A')
        {
            a[++t]=(l+p)%d;
            for(int i=t;i;i--)
                if(ans[i]<a[t])ans[i]=a[t];
                else break;
        }
        else{
            printf("%d\n",l=ans[t-p+1]);
        }
    }
    return 0;
}
View Code

 

posted on 2018-08-20 16:56  solvit  阅读(213)  评论(0编辑  收藏  举报

导航