-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsolution.cpp
90 lines (69 loc) · 2.15 KB
/
solution.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
#include <algorithm>
#include <iostream>
#include <queue>
#include <sstream>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
class Table {
unordered_map<int, int> whoHasWhat;
priority_queue<pair<int, int>> biggest;
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> smallest;
int center;
public:
Table(int center, vector<int>& arr) {
this->center = center;
for (int i = 0; i < arr.size(); i++) {
whoHasWhat[i] = arr[i];
smallest.push({arr[i], i});
biggest.push({arr[i], -i});
}
}
pair<int, int> pushToCenter(int newElement) {
while (whoHasWhat[smallest.top().second] != smallest.top().first) {
smallest.pop();
}
auto top = smallest.top();
smallest.pop();
whoHasWhat[top.second] = this->center;
smallest.push({this->center, top.second});
biggest.push({this->center, -top.second});
this->center = newElement;
return {top.first, top.second + 1};
}
int pop_from_center(int newElement) {
while (whoHasWhat[-biggest.top().second] != biggest.top().first) {
biggest.pop();
}
auto top = biggest.top();
biggest.pop();
int previousCenterValue = this->center;
this->center = top.first;
whoHasWhat[-top.second] = newElement;
smallest.push({newElement, -top.second});
biggest.push({newElement, top.second});
return previousCenterValue;
}
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, m, center;
cin >> n >> m >> center;
vector<int> arr(n);
for (int i = 0; i < n; i++)
cin >> arr[i];
Table table = Table(center, arr);
for (int i = 0; i < m; i++) {
int q_type, newElement;
cin >> q_type >> newElement;
if (q_type == 1) {
auto ret_val = table.pushToCenter(newElement);
cout << ret_val.first << " " << ret_val.second << "\n";
} else {
cout << table.pop_from_center(newElement) << "\n";
}
}
return 0;
}