A. Valeriy and Deque

A. 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 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

扫描二维码关注公众号,回复: 9591872 查看本文章

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

Copy

5 3
1 2 3 4 5
1
2
10

Output

Copy

1 2
2 3
5 2

Input

Copy

2 0
0 0

Output

Copy

 

Note

  1. Consider all 10 steps for the first test in detail:
  2. [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

题意:

         给你一个序列,然后每次操作都是从这个序列拿出前2个数,大的放序列头,小的放序列尾,问你第n次操作后,前2个数是什么

思路:

        很明显,当我们的序列头为最大时,剩下的操作只是一个循环,我们用一个双向队列就能解决了。

#include<iostream>
#include<deque>
#include<memory.h>
#include<stdio.h>
#include<map>
#include<string.h>
#include<algorithm>
#include<vector>
#include<math.h>
#include<stack>
#include<queue>
#include<set>
using namespace std;
typedef long long LL;
deque<LL>q;
LL n,m,Max=0,temp,ci=0,a,b,aa[200100],bb[200100],arr[200100];
int main()
{
	cin>>n>>m;
	for(int i=1;i<=n;i++)
	{
		scanf("%lld",&temp);
		q.push_back(temp);
		Max = max(Max,temp);
		if(i == 1)
			aa[0]=temp;
		else if(i ==2)
			bb[0]=temp;
	}
	while(1)
	{
		a = q.front();
		q.pop_front();
		b = q.front();
		q.pop_front();
		if(a == Max)
		{
			q.push_front(b);
			q.push_front(a);
			break;
		}
		else
		{
			ci++;
			aa[ci] = a;
			bb[ci] = b;
			if(a>b)
			{
				q.push_front(a);
				q.push_back(b);
			}
			else
			{
				q.push_back(a);
				q.push_front(b);
			}
		}
	}
	for(int i=1;i<=n;i++)
	{
		arr[i]=q.front();
		q.pop_front();
	}
	for(int i=1;i<=m;i++)
	{
		cin>>temp;
		if(temp<=ci)
			cout<<aa[temp]<<' '<<bb[temp]<<endl;
		else
		{
			temp-=ci;
			temp%=(n-1);
			if(temp!=0)
				cout<<Max<<' '<<arr[temp+1]<<endl;
			else
				cout<<Max<<' '<<arr[n]<<endl;
		}
	}
	return 0;
}
发布了133 篇原创文章 · 获赞 8 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/fbher/article/details/93424166
今日推荐