C. Valeriy and Deque
time limit per test
6 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Recently, on the course of algorithms and data structures, Valeriy learned how to use a deque. He built a deque filled with nn elements. The ii-th element is aiai (ii = 1,2,…,n1,2,…,n). He gradually takes the first two leftmost elements from the deque (let's call them AA and BB, respectively), and then does the following: if A>BA>B, he writes AA to the beginning and writes BB to the end of the deque, otherwise, he writes to the beginning BB, and AA 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][2,3,4,5,1], on the operation he will write B=3B=3 to the beginning and A=2A=2 to the end, so he will get [3,4,5,1,2][3,4,5,1,2].
The teacher of the course, seeing Valeriy, who was passionate about his work, approached him and gave him qq queries. Each query consists of the singular number mjmj (j=1,2,…,q)(j=1,2,…,q). It is required for each query to answer which two elements he will pull out on the mjmj-th operation.
Note that the queries are independent and for each query the numbers AA and BB 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 nn and qq (2≤n≤1052≤n≤105, 0≤q≤3⋅1050≤q≤3⋅105) — the number of elements in the deque and the number of queries. The second line contains nn integers a1a1, a2a2, ..., anan, where aiai (0≤ai≤109)(0≤ai≤109) — the deque element in ii-th position. The next qqlines contain one number each, meaning mjmj (1≤mj≤10181≤mj≤1018).
Output
For each teacher's query, output two numbers AA and BB — the numbers that Valeriy pulls out of the deque for the mjmj-th operation.
Examples
input
Copy
代码语言:javascript复制5 3
1 2 3 4 5
1
2
10
output
Copy
代码语言:javascript复制1 2
2 3
5 2
input
Copy
代码语言:javascript复制2 0
0 0
output
Copy
代码语言:javascript复制
Note
- Consider all 10 steps for the first test in detail:
- [1,2,3,4,5][1,2,3,4,5] — on the first operation, AA and BB are 11 and 22, respectively. So, 22 we write to the beginning of the deque, and 11 — to the end. We get the following status of the deque: [2,3,4,5,1][2,3,4,5,1].
- [2,3,4,5,1]⇒A=2,B=3[2,3,4,5,1]⇒A=2,B=3.
- [3,4,5,1,2][3,4,5,1,2]
- [4,5,1,2,3][4,5,1,2,3]
- [5,1,2,3,4][5,1,2,3,4]
- [5,2,3,4,1][5,2,3,4,1]
- [5,3,4,1,2][5,3,4,1,2]
- [5,4,1,2,3][5,4,1,2,3]
- [5,1,2,3,4][5,1,2,3,4]
- [5,2,3,4,1]⇒A=5,B=2[5,2,3,4,1]⇒A=5,B=2.
思路:模拟双端队列题目,比B题简单...
代码语言:javascript复制#include<bits/stdc .h>
#define rg register long long
#define inf 21474899993647
#define min(a,b) (a<b?a:b)
#define max(a,b) (a>b?a:b)
#define ll long long
#define maxn 100005
#define endl "n"
#define N 6000
const double eps=1e-8;
using namespace std;
inline ll read()
{
char ch=getchar();
ll s=0,w=1;
while(ch<48||ch>57)
{
if(ch=='-')
w=-1;
ch=getchar();
}
while(ch>=48&&ch<=57)
{
s=(s<<1) (s<<3) (ch^48);
ch=getchar();
}
return s*w;
}
inline void write(ll x)
{
if(x<0)
putchar('-'),x=-x;
if(x>9)
write(x/10);
putchar(x 48);
}
ll n=read(),t=read();
map<ll,pair<ll,ll>>p;
deque<ll>q;
int main()
{
for(rg i=1;i<=n;i )
{
ll x=read();
q.push_back(x);
}
for(rg i=1;i<=300000;i )
{
ll a=q.front();
q.pop_front();
ll b=q.front();
q.pop_front();
if(a>b)
{
q.push_front(a);
q.push_back(b);
}
else
{
q.push_front(b);
q.push_back(a);
}
p[i]=make_pair(a,b);
}
for(rg i=1;i<=t;i )
{
ll x=read();
if(x>2*n-2)
{
ll k=x-(2*n-2);
x=(k-1)%(n-1) 1 n-1 ;
}
cout<<p[x].first<<" "<<p[x].second<<endl;
}
return 0;
}