#include<<deque的使用
内部函数的调用
[begin(), end()()) ;//begin:容器起始位置 end最后一个元素下一个位置
[rbegin(), rend()) ;//反向迭代器rbegin在end位置,rend在begin
[cbegin(), cend()) ;//const迭代器,与begin和end位置相同,但不能修改其空间内容
[crbegin(), crend()) ;//const反向迭代器,与crbegin在cend位置,crend在cbegin位置
- deque的容量操作
size();//返回deque有效元素的个数
empty();//检测deque是否为空,是返回true,否则返回false
resize();//将deque中的元素改变到sz,多处的空间用value填充
- deque的元素访问操作
operator[];//返回deque中n位置上元素的引用
front();//返回deque中首元素的引用
back();//返回deque中最后一个元素的引用
- deque的修改操作
push_back() ;
pop_back() ;//deque的尾插和尾删
push_front() ;
pop_front() ;//deque任意位置插入和删除
insert(pos, value) ;
erase(pos); 删除deque头部元素
swap() ;交换两个deque中的内容
clear() ;将deque中的元素清
运用deque的例题
- 题目链接
Valeriy and Deque
… Valeriy and Deque …
Recently, on the course of algorithms and data structures, Valeriy learned how to use a deque. He built a deque filled with n elements. The i-th element is ai (i = 1,2,…,n). He gradually takes the first two leftmost elements from the deque (let’s call them A and B, respectively), and then does the following: if A>B, he writes A to the beginning and writes B to the end of the deque, otherwise, he writes to the beginning B, and A writes to the end of the deque. We call this sequence of actions an operation.
For example, if deque was [2,3,4,5,1], on the operation he will write B=3 to the beginning and A=2 to the end, so he will get [3,4,5,1,2].
The teacher of the course, seeing Valeriy, who was passionate about his work, approached him and gave him q queries. Each query consists of the singular number mj (j=1,2,…,q). It is required for each query to answer which two elements he will pull out on the mj-th operation.
Note that the queries are independent and for each query the numbers A and B should be printed in the order in which they will be pulled out of the deque.
Deque is a data structure representing a list of elements where insertion of new elements or deletion of existing elements can be made from both sides.
Input
The first line contains two integers n and q (2≤n≤105, 0≤q≤3⋅105) — the number of elements in the deque and the number of queries. The second line contains n integers a1, a2, …, an, where ai (0≤ai≤109) — the deque element in i-th position. The next q lines contain one number each, meaning mj (1≤mj≤1018).
Output
For each teacher’s query, output two numbers A and B — the numbers that Valeriy pulls out of the deque for the mj-th operation.
Examples
Input
5 3
1 2 3 4 5
1
2
10
Output
1 2
2 3
5 2
Input
2 0
0 0
Output
Note
Consider all 10 steps for the first test in detail:
[1,2,3,4,5] — on the first operation, A and B are 1 and 2, respectively.
So, 2 we write to the beginning of the deque, and 1 — to the end.
We get the following status of the deque: [2,3,4,5,1].
[2,3,4,5,1]⇒A=2,B=3.
[3,4,5,1,2]
[4,5,1,2,3]
[5,1,2,3,4]
[5,2,3,4,1]
[5,3,4,1,2]
[5,4,1,2,3]
[5,1,2,3,4]
[5,2,3,4,1]⇒A=5,B=2.
#include<iostream>
#include<algorithm>
#include<deque>
using namespace std;
const int N = 100010;
typedef long long ll;
deque<ll>deq;
ll a[N];
ll n,m,maxn=0,num=0;
struct node{
ll l,r;
}r[N];
void Solve()
{
while(true)
{
ll x=deq.front();deq.pop_front();
ll y=deq.front();deq.pop_front();
if(x!=maxn)
{
num++;
r[num].l=x,r[num].r=y;
if(x>y)
{
deq.push_front(x);
deq.push_back(y);
}
else
{
deq.push_front(y);
deq.push_back(x);
}
}
else
{
deq.push_front(y);
for(int i=1;i<=n-1;i++)
{
y=deq.front();deq.pop_front();
a[i]=y;
}
break;
}
}
}
int main()
{
cin>>n>>m;
for(int i=1;i<=n;i++)
{
ll x;
cin>>x;
deq.push_back(x);
maxn=max(x,maxn);
}
Solve();
while(m--)
{
ll c;
cin>>c;
if(c<=num) cout<<r[c].l<<" "<<r[c].r<<endl;
else
{
c-=num;
c=c%(n-1);
if(c==0) c=n-1;
cout<<maxn<<" "<<a[c]<<endl;
}
}
return 0;
}