-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDeQ.cpp
92 lines (76 loc) · 1.61 KB
/
DeQ.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include<iostream>
#include<deque>
#include<stack>
#include<vector>
#include<time.h>
#include<stdlib.h>
using namespace std;
int main() {
deque<int> q;
stack<int> min, max;
int n=150,i;
int e[n];
srand(unsigned (time(0)));
for(int i=0;i<n;i++)
{
e[i] = rand()%20;
}
for(i=0; i<n; i++) {
if(q.empty()) {
q.push_front(e[i]);
} else {
if(e[i] <= q.front() && min.empty()) {
q.push_front(e[i]);
} else if(e[i] < q.front() && min.top() > e[i]) {
while( !min.empty()&&min.top() > e[i] ) {
q.push_front(min.top());
min.pop();
}
min.push(e[i]);
} else if(e[i] < q.front() && min.top() <= e[i]) {
min.push(e[i]);
} else if(e[i] >= q.back() && max.empty()) {
q.push_back(e[i]);
} else if (e[i] > q.back() && max.top() < e[i]) {
while(!max.empty()&&max.top() < e[i]) {
q.push_back(max.top());
max.pop();
}
max.push(e[i]);
} else if (e[i] > q.back() && max.top() >= e[i]) {
max.push(e[i]);
}
else if(e[i] > q.front() && e[i] < q.back()) {
while(e[i] > q.front() && e[i] < q.back()) {
min.push(q.front());
q.pop_front();
max.push(q.back());
q.pop_back();
}
if(q.empty()) {
q.push_front(e[i]);
} else {
if(e[i] <= q.front()) {
q.push_front(e[i]);
} else {
q.push_back(e[i]);
}
}
}
}
}
while(!min.empty()) {
q.push_front(min.top());
min.pop();
}
while(!max.empty()) {
q.push_back(max.top());
max.pop();
}
int x = q.front();
while(!q.empty()) {
cout<<q.front()<<" ";
q.pop_front();
}
return 0;
}