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; }
听说暴力也可以~
/************************************************************** 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; }