分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 教程案例

bzoj 1012 [JSOI2008]最大数maxnumber

发布时间:2023-09-06 02:02责任编辑:郭大石关键词:暂无标签

题目: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;}

bzoj 1012 [JSOI2008]最大数maxnumber

原文地址:https://www.cnblogs.com/Narh/p/9248678.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved