首页 技术 正文
技术 2022年11月18日
0 收藏 596 点赞 3,507 浏览 1295 个字

1012: [JSOI2008]最大数maxnumber

Time Limit: 3 Sec  Memory Limit: 162 MB
Submit: 14142  Solved: 6049
[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

题目链接:

    http://www.lydsy.com/JudgeOnline/problem.php?id=1012

Solution

  比较简单的数据结构题。。。

  可以发现如果i<j并且a [ i ] < a [ j ] ,那么第i个数肯定不会成为答案。。

  这就符合了单调栈的性质。。

  然后询问只是对于最后几个数,并不是全局询问,于是二分位置。。。

代码

#include<iostream>
#include<cstdio>
#define LL long long
using namespace std;
int M;
LL t,D,a[300000],b[300000],cnt;
char c;
void add(LL x,int js){
x=(x+t)%D;
a[++cnt]=x;
b[cnt]=js;
for(int i=cnt-1;i>=1;i--){
if(a[i]>x) break;
else {
a[i]=x;b[i]=js;cnt--;
}
}
}
void check(LL x,int js){
x=js-x+1;
int head=1,tail=cnt;
while(head<tail){
int mid=(head+tail)>>1;
if(b[mid]>=x) tail=mid;
else head=mid+1;
}
t=a[head];
printf("%lld\n",t);
}
int main(){
LL x;
int js;
t=0;cnt=0;js=0;
scanf("%d%lld",&M,&D);
for(int i=1;i<=M;i++){
c=getchar();
while(c!='Q'&&c!='A') c=getchar();
scanf("%lld",&x);
if(c=='A') {js++;add(x,js);}
if(c=='Q') check(x,js);
}
return 0;
}

  

  

This passage is made by Iscream-2001.

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,075
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,551
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,399
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,176
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,811
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,892