CF1179A.Valeriy and Deque

普及/提高-

通过率:0%

时间限制:6.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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 aia_i (ii = 1,2,…,n1, 2, \ldots, 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 \gt 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 mjm_j (j=1,2,…,q)(j = 1, 2, \ldots, q). It is required for each query to answer which two elements he will pull out on the mjm_j-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.

最近,在算法与数据结构课程中,瓦列里学会了如何使用双端队列(deque)。他构建了一个包含 nn 个元素的双端队列。第 ii 个元素为 aia_i(其中 i=1,2,…,ni = 1, 2, \ldots, n)。他逐步从双端队列最左侧取出前两个元素(分别记为 AA 和 BB),然后执行如下操作:若 A>BA > B,则将 AA 插入双端队列头部,并将 BB 插入双端队列尾部;否则,将 BB 插入双端队列头部,并将 AA 插入双端队列尾部。我们将这一系列动作称为一次操作。

例如,若双端队列为 [2,3,4,5,1][2, 3, 4, 5, 1],在一次操作中,他将 B=3B = 3 插入头部,A=2A = 2 插入尾部,从而得到新双端队列 [3,4,5,1,2][3, 4, 5, 1, 2]。

课程教师见瓦列里热情投入,便走近他,并向他提出了 qq 个查询。每个查询仅包含一个整数 mjm_j(其中 j=1,2,…,qj = 1, 2, \ldots, q)。对每个查询,需回答:在第 mjm_j 次操作中,他将从双端队列中取出哪两个元素。

注意:各查询相互独立;对每个查询,应按实际从双端队列中取出的顺序输出 AA 和 BB。

双端队列(deque)是一种数据结构,表示一个元素列表,允许在列表两端进行新元素的插入或已有元素的删除。

输入格式

The first line contains two integers nn and qq (2≤n≤1052 \leq n \leq 10^5, 0≤q≤3⋅1050 \leq q \leq 3 \cdot 10^5) — the number of elements in the deque and the number of queries. The second line contains nn integers a1a_1, a2a_2, ..., ana_n, where aia_i (0≤ai≤109)(0 \leq a_i \leq 10^9) — the deque element in ii-th position. The next qq lines contain one number each, meaning mjm_j (1≤mj≤10181 \leq m_j \leq 10^{18}).

第一行包含两个整数 nn 和 qq(2≤n≤1052 \leq n \leq 10^5,0≤q≤3⋅1050 \leq q \leq 3 \cdot 10^5)—— 分别表示双端队列中的元素个数和查询次数。
第二行包含 nn 个整数 a1a_1, a2a_2, ..., ana_n,其中 aia_i(0≤ai≤1090 \leq a_i \leq 10^9)表示双端队列中第 ii 个位置的元素。
接下来的 qq 行每行包含一个整数,表示 mjm_j(1≤mj≤10181 \leq m_j \leq 10^{18})。

输出格式

For each teacher's query, output two numbers AA and BB — the numbers that Valeriy pulls out of the deque for the mjm_j-th operation.

对于每位教师的查询,输出两个数 AA 和 BB —— 即 Valeriy 在第 mjm_j 次操作中从双端队列中取出的两个数。

输入输出样例

  • 输入#1

    5 3
    1 2 3 4 5
    1
    2
    10

    输出#1

    1 2
    2 3
    5 2
  • 输入#2

    2 0
    0 0

    输出#2

说明/提示

Consider all 10 steps for the first test in detail:1. [1,2,3,4,5][1, 2, 3, 4, 5] — on the first operation, AA and BB are 11 and 22, 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]$.
  1. [2,3,4,5,1]⇒A=2,B=3[2, 3, 4, 5, 1] \Rightarrow A = 2, B = 3.
  2. [3,4,5,1,2][3, 4, 5, 1, 2]
  3. [4,5,1,2,3][4, 5, 1, 2, 3]
  4. [5,1,2,3,4][5, 1, 2, 3, 4]
  5. [5,2,3,4,1][5, 2, 3, 4, 1]
  6. [5,3,4,1,2][5, 3, 4, 1, 2]
  7. [5,4,1,2,3][5, 4, 1, 2, 3]
  8. [5,1,2,3,4][5, 1, 2, 3, 4]
  9. [5,2,3,4,1]⇒A=5,B=2[5, 2, 3, 4, 1] \Rightarrow A = 5, B = 2.

详细考虑第一个测试用例的全部 10 步:

  1. [1,2,3,4,5][1, 2, 3, 4, 5] — 在第一次操作中,AA 和 BB 分别为 11 和 22。
    因此,将 22 插入双端队列(deque)的前端,将 11 插入双端队列的尾端。
    得到双端队列的当前状态为:[2,3,4,5,1][2, 3, 4, 5, 1]。
  2. [2,3,4,5,1]⇒A=2,B=3[2, 3, 4, 5, 1] \Rightarrow A = 2, B = 3。
  3. [3,4,5,1,2][3, 4, 5, 1, 2]
  4. [4,5,1,2,3][4, 5, 1, 2, 3]
  5. [5,1,2,3,4][5, 1, 2, 3, 4]
  6. [5,2,3,4,1][5, 2, 3, 4, 1]
  7. [5,3,4,1,2][5, 3, 4, 1, 2]
  8. [5,4,1,2,3][5, 4, 1, 2, 3]
  9. [5,1,2,3,4][5, 1, 2, 3, 4]
  10. [5,2,3,4,1]⇒A=5,B=2[5, 2, 3, 4, 1] \Rightarrow A = 5, B = 2。

输入解题思路,AI测评打分。不知道怎么写?

首页