1012: [JSOI2008]最大数maxnumber 单调栈+二分
来源:互联网 发布:安知玉如意好看吗 编辑:程序博客网 时间:2024/06/11 11:49
1012: [JSOI2008]最大数maxnumber
Time Limit: 3 Sec Memory Limit: 162 MB
Submit: 10305 Solved: 4509
[Submit][Status][Discuss]
Description
现在请求你维护一个数列,要求提供以下两种操作:1、 查询操作。语法:Q L 功能:查询当前数列中末尾L
个数中的最大的数,并输出这个数的值。限制:L不超过当前数列的长度。2、 插入操作。语法:A n 功能:将n加
上t,其中t是最近一次查询操作的答案(如果还未执行过查询操作,则t=0),并将所得结果对一个固定的常数D取
模,将所得答案插入到数列的末尾。限制:n是非负整数并且在长整范围内。注意:初始时数列是空的,没有一个
数。
Input
第一行两个整数,M和D,其中M表示操作的个数(M <= 200,000),D如上文中所述,满足D在longint内。接下来
M行,查询操作或者插入操作。
Output
对于每一个询问操作,输出一行。该行只有一个数,即序列中最后L个数的最大数。
Sample Input
5 100
A 96
Q 1
A 97
Q 1
Q 2
Sample Output
96
93
Time Limit: 3 Sec Memory Limit: 162 MB
Submit: 10305 Solved: 4509
[Submit][Status][Discuss]
Description
现在请求你维护一个数列,要求提供以下两种操作:1、 查询操作。语法:Q L 功能:查询当前数列中末尾L
个数中的最大的数,并输出这个数的值。限制:L不超过当前数列的长度。2、 插入操作。语法:A n 功能:将n加
上t,其中t是最近一次查询操作的答案(如果还未执行过查询操作,则t=0),并将所得结果对一个固定的常数D取
模,将所得答案插入到数列的末尾。限制:n是非负整数并且在长整范围内。注意:初始时数列是空的,没有一个
数。
Input
第一行两个整数,M和D,其中M表示操作的个数(M <= 200,000),D如上文中所述,满足D在longint内。接下来
M行,查询操作或者插入操作。
Output
对于每一个询问操作,输出一行。该行只有一个数,即序列中最后L个数的最大数。
Sample Input
5 100
A 96
Q 1
A 97
Q 1
Q 2
Sample Output
96
93
96
利用单调栈存下可能的最大值,每次查询时就可以二分查找单调栈得出答案了。
#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;const int maxm = 200005;#define ll long longtypedef struct{ll xx;int id;}H;H p[maxm];ll a[maxm], dp[maxm];int main(){int i, j, k, m, len = 0, id = 0, sum = 0;char ch[2];ll n = 0, d, ans = 0, mx = 0, now, x;scanf("%d%lld", &m, &d);for (i = 1;i <= m;i++){scanf("%s %lld", &ch, &x);if (ch[0] == 'A'){sum++;now = (ans + x) % d;while (p[len].xx <= now&&len > 0)len--;p[++len].xx = now;p[len].id = sum;continue;}int l = 1, r = len;if (l == r){ans = p[len].xx;printf("%lld\n", ans);continue;}x = sum - x + 1;while (r - l > 1){int mid = (r + l) / 2;if (p[mid].id >= x){ans = p[mid].xx;r = mid;}elsel = mid;}if (p[r].id >= x)ans = p[r].xx;if (p[l].id >= x)ans = p[l].xx;printf("%lld\n", ans);}return 0;}
阅读全文
0 0
- 1012: [JSOI2008]最大数maxnumber 单调栈+二分
- bzoj 1012: [JSOI2008]最大数maxnumber(单调栈+二分)
- 【单调队列】1012: [JSOI2008]最大数maxnumber
- [JSOI2008] [BZOJ1012] 最大数maxnumber - 单调栈
- [BZOJ1012][JSOI2008]最大数maxnumber-单调栈
- BZOJ 1012 JSOI2008 最大数maxnumber 单调栈+二分 / 线段树
- BZOJ 1012: [JSOI2008]最大数maxnumber 单调栈
- [BZOJ 1012][JSOI2008]最大数maxnumber:线段树|单调栈
- BZOJ 1012 [JSOI2008]最大数maxnumber (单调栈)
- bzoj 1012: [JSOI2008]最大数maxnumber 二分
- [JSOI2008]最大数maxnumber(栈&二分查找)
- bzoj1012: [JSOI2008]最大数maxnumber[单调队列+二分]
- [BZOJ]1012 [JSOI2008]最大数maxnumber (线段树维护区间最值 || 单调栈+二分查找)
- BZOJ 1012[jsoi2008] 最大数maxnumber 线段树(或者是单调队列+二分)
- BZOJ 1012([JSOI2008]最大数maxnumber-单调队列)
- 【BZOJ 1012】 [JSOI2008]最大数maxnumber(单调队列做法)
- BZOJ 1012: [JSOI2008]最大数maxnumber 单调队列/线段树
- BZOJ1012: [JSOI2008]最大数maxnumber(单调栈)
- 最长公共子序列LCS-DP
- TCP的三次握手四次分手详解
- input标签的method属性 post与get的区别
- 通过nginx访问静态文件配置
- Qt实现TCP通信简例
- 1012: [JSOI2008]最大数maxnumber 单调栈+二分
- printf开启关闭宏-方便调试
- AttributeError: module 'urllib' has no attribute 'urlopen'
- webService开发02
- 关于新建项目整体框架的思考
- POJ
- epoll多路复用
- 酷狗音乐API
- 第三本书 脚本学习1