-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmin_queue.cpp
More file actions
74 lines (59 loc) · 1.23 KB
/
min_queue.cpp
File metadata and controls
74 lines (59 loc) · 1.23 KB
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
#include <bits/stdc++.h>
using namespace std;
#define INF (1<<29)
struct minqueue{
private:
int add = 0;
stack<pair<int, int>>sl, sr;
void push(int x, stack<pair<int, int>>&s){
if (s.size() > 0) s.push({x, std::min(x, s.top().second)});
else s.push({x, x});
}
void move(){
if (sl.size() == 0){
while(sr.size()) {
push(sr.top().first, sl);
sr.pop();
}
}
}
public:
void push(int x){
push(x-add, sr);
}
void pop(){
move();
if (sl.size()) sl.pop();
}
int top(){
move();
if (sl.size()) return sl.top().first+add;
return 0;
}
int min(){
move();
int res = INF;
if (sl.size()) res = std::min(res, sl.top().second+add);
if (sr.size()) res = std::min(res, sr.top().second+add);
return res;
}
int size(){
return sl.size()+sr.size();
}
void increase(int x){
add += x;
}
};
int main(){
minqueue q;
q.push(1);
q.push(2);
q.push(3);
q.increase(100);
q.push(7);
while(q.size()){
cout << q.top() << " " << q.min() << endl;
q.pop();
}
return 0;
}