Codeforces Round #569 (Div. 2)ios
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.app
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].ide
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.ui
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.this
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.spa
Inputcode
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).blog
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:
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次操做後的前兩個數是什麼。
思路:現學了一下deque,感受還行(我比較菜,之前還不會orzorz),經過觀察樣例一的解釋咱們能夠發現,
當雙端隊列的隊首通過幾回操做後變成最大值時,後面再操做n-1次後會循環出現相同的結果,
由於最大值在隊首時進行操做必定是把第二個數放到隊尾,瞭解了這種狀況以後,
咱們只須要預處理出不循環的前一部分解和循環的後一部分解就能夠直接輸出了......
1 #include<iostream>
2 #include<cstring>
3 #include<cstdio>
4 #include<cmath>
5 #include<algorithm>
6 #include<map>
7 #include<set>
8 #include<vector>
9 #include<queue>
10 using namespace std; 11 #define ll long long
12 const int mod=1e9+7; 13 const int inf=1e9+7; 14
15 deque<int>d;//雙端隊列
16
17 int main() 18 { 19 ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); 20
21 d.clear(); 22
23 int n,op; 24 cin>>n>>op;//n個數,op個操做
25
26 int maxx=-1;//找到最大值
27 int num; 28 for(int i=0;i<n;i++) 29 { 30 cin>>num;//輸入
31 if(num>maxx) 32 maxx=num;//刷新最大值
33 d.push_back(num);//尾部導入雙端隊列
34 } 35
36 if(op==0)//特判一波
37 return 0; 38
39 vector<pair<int,int> >v1;//這個是存無循環的前一部分解
40 vector<pair<int,int> >v2;//這個是存後面循環的後一部分解
41
42 v1.clear(); 43 v2.clear(); 44
45 int a,b; 46
47 while(d[0]!=maxx)//先預處理找出前一部分解
48 { 49 a=d[0];//a是第一個數
50 b=d[1];//b是第二個數
51
52 d.pop_front(); 53 d.pop_front(); 54 //按題意對雙端隊列操做一波
55 if(a>b) 56 { 57 d.push_front(a); 58 d.push_back(b); 59 } 60 else
61 { 62 d.push_front(b); 63 d.push_back(a); 64 } 65
66 v1.push_back({a,b});//將解導入v1數組
67
68 } 69
70 //for(int i=0;i<v1.size();i++)//能夠看一看前一部分解 71 // cout<<v1[i].first<<" "<<v1[i].second<<endl;
72
73 ll int xunhuan=n-1;//後一部分的循環節長度
74
75 ll int m=v1.size();//前一部分解的長度
76
77 for(int i=0;i<xunhuan;i++)//處理出循環節
78 { 79 a=d[0]; 80 b=d[1]; 81 d.pop_front(); 82 d.pop_front(); 83
84 if(a>b) 85 { 86 d.push_front(a); 87 d.push_back(b); 88 } 89 else
90 { 91 d.push_front(b); 92 d.push_back(a); 93 } 94
95 v2.push_back({a,b}); 96 } 97
98 //for(int i=0;i<v2.size();i++)//能夠看一看後一部分循環解的部分 99 // cout<<v2[i].first<<" "<<v2[i].second<<endl;
100
101 ll int caozuo; 102
103 for(int i=0;i<op;i++) 104 { 105 cin>>caozuo; 106
107 if(caozuo>=1&&caozuo<=v1.size())//是前一部分的解
108 { 109 cout<<v1[caozuo-1].first<<" "<<v1[caozuo-1].second<<endl; 110 } 111 else
112 { 113 caozuo-=v1.size();//這裏要減掉再膜循環節,%%%
114 caozuo%=xunhuan; 115 if(caozuo==0)//0-1越界了,特判一下orzorz
116 cout<<v2[v2.size()-1].first<<" "<<v2[v2.size()-1].second<<endl; 117 else
118 cout<<v2[caozuo-1].first<<" "<<v2[caozuo-1].second<<endl; 119 } 120
121 } 122
123 return 0; 124 }