现在请求你维护一个数列要求提供以下两种操作:1、 查询操作。语法:Q L功能:查询当前数列中末尾L个数中的最大的数并输出这个数的值。限制:LL不超过当前数列的长度。L0L02、 插入操作。语法:A n功能:将nn加上tt其中tt是最近一次查询操作的答案如果还未执行过查询操作则t=0t=0并将所得结果对一个固定的常数DD取模将所得答案插入到数列的末尾。限制:nn是整数可能为负数并且在长整
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
int M, D;
cin >> M >> D;
vector<int> sequence;
int lastQuery = 0;
for (int i = 0; i < M; i++) {
string operation;
cin >> operation;
if (operation == "Q") {
int L;
cin >> L;
int start = max(0, (int)sequence.size() - L);
int maxNum = *max_element(sequence.begin() + start, sequence.end());
cout << maxNum << endl;
} else if (operation == "A") {
int n;
cin >> n;
lastQuery = (lastQuery + n) % D;
sequence.push_back(lastQuery);
}
}
return 0;
}
``
原文地址: https://www.cveoy.top/t/topic/h6uy 著作权归作者所有。请勿转载和采集!