较为祥尽的deque用法:https://blog.csdn.net/u011630575/article/details/79923132
例题:http://codeforces.com/problemset/problem/1180/C
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 qq lines 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
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][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.
#include<iostream> #include<deque> #define ll long long using namespace std; deque <ll> q; ll n,m; ll x; ll maxn = 0; ll ans[300000 + 10]; ll anss[300000 + 10][2]; ll len2 = 1; void solve() { while(1) { ll a = q.front(); q.pop_front(); ll b = q.front(); q.pop_front(); if( a == maxn )//如果最大数在尾部,则剩余数字依次出现 { q.push_front(b); for(int i=1; i<n; i++) { b = q.front(); ans[i] = b; q.pop_front(); } break; } else//模拟操作 { anss[len2][0] = a; anss[len2][1] = b; len2++; if(a > b) { q.push_front(a); q.push_back(b); } else { q.push_front(b); q.push_back(a); } } } } int main() { cin>>n>>m; for(int i=0; i<n; i++) { cin>>x; q.push_back(x); maxn = max(x, maxn); } // puts("YEah"); solve(); // puts("YEah"); // cout<<len2<<endl; len2--; for(int i=0; i<m; i++) { cin>>x; // cout<<len2<<endl; if(x <= len2) cout<<anss[x][0]<<' '<<anss[x][1]<<endl; else { // cout<<len2<<endl; x = x - len2; // cout<<x<<endl; x =x % (n-1); // cout<<x<<endl; if(x == 0) x = n-1; // cout<<x<<endl; cout<<maxn<<' '<<ans[x]<<endl; } } return 0; }