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 qlines 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.[2,3,4,5,1]⇒A=2,B=3.
3.[3,4,5,1,2]
4.[4,5,1,2,3]
5.[5,1,2,3,4]
6.[5,2,3,4,1]
7.[5,3,4,1,2]
8.[5,4,1,2,3]
9.[5,1,2,3,4]
10.[5,2,3,4,1]⇒A=5,B=2.
题意:题目意思是给你n个数放双端队列里面,有一个操作:取出前两个数,大的数放前面,小的或者一样大的放后面,问你经过m次操作后的前两个数是什么。
可以发现,当经过几次操作后双端队列的对队首就会变成最大值,再就是n-1次循环一波。
因为大的会留在队首小的放在队尾。
#include<iostream>
#include<stdio.h>
#include<cstring>
#include<queue>
using namespace std;
const int maxx = 3e5+10;
int a[maxx],b[maxx]; //队首不为最大值是队列的前两项
int c[maxx]; // 队首为最大值时队列的第二项
int main()
{
int n,op;
while(cin>>n>>op)
{
int i;
deque<int> q; //创建双端队列
int mmax = 0;
for(i=0; i<n; i++)
{
int x;
scanf("%d",&x);
q.push_back(x);
if(x>mmax) // 寻找最大值
mmax = x;
}
i = 0;
while(1)
{
int x = q.front();
if(x==mmax)
break;
q.pop_front();
int y = q.front();
q.pop_front();
a[++i] = x;
b[i] = y;
if(x>y)
{
q.push_front(x);
q.push_back(y);
}
else
{
q.push_front(y);
q.push_back(x);
}
}
int u = i;
q.pop_front(); // 将队首的最大值弹出
i = 1;
while(!q.empty())
{
c[i] = q.front(); // 将n-1次循环存入数组
q.pop_front();
i++;
}
c[0] = c[i-1]; // 当( 循环次数-u)%(n-1)=0的时候应该输出下标为0的一项。
while(op--)
{
long long int t;
scanf("%lld",&t);
if(t>u)
{
t = (t-u)%(n-1);
printf("%d %d\n",mmax,c[t]);
}
else
{
printf("%d %d\n",a[t],b[t]);
}
}
}
return 0;
}