题目: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; }
本文深入探讨了单调栈和二分搜索算法在解决特定类型问题中的应用,通过一个具体的编程挑战实例,展示了如何结合这两种算法来优化解决方案。文章详细解释了代码实现过程,包括变量定义、输入处理、算法流程及输出结果,为读者提供了理解和实践单调栈与二分搜索算法的清晰路径。
646

被折叠的 条评论
为什么被折叠?



