-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy path17-20-map.cpp
216 lines (183 loc) · 4.95 KB
/
17-20-map.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#include <bits/stdc++.h>
#include "base.h"
using namespace std;
typedef __int128_t int128;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<pll> vpll;
typedef long double ld;
typedef vector<ld> vld;
typedef vector<bool> vb;
typedef vector<string> vs;
// const int mod = 1e9 + 7;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define rep1(i, n) for (ll i = 1; i <= (n); i++)
#define rrep(i, n) for (ll i = (n)-1; i >= 0; i--)
#define rrep1(i, n) for (ll i = (n); i >= 1; i--)
#define all(v) (v).begin(), (v).end()
#define ALL(A) A.begin(), A.end()
#define LLA(A) A.rbegin(), A.rend()
#define sz(x) (int)(x).size()
#define SZ(A) int((A).size())
#define CPY(A, B) memcpy(A, B, sizeof(A))
#define CTN(T, x) (T.find(x) != T.end())
#define PB push_back
#define MP(A, B) make_pair(A, B)
#define fi first
#define se second
template <class T>
using min_queue = priority_queue<T, vector<T>, greater<T>>;
template <class T>
using max_queue = priority_queue<T>;
int dir4[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
int dir8[8][2] = {{0, 1}, {1, 1}, {1, 0}, {1, -1},
{0, -1}, {-1, -1}, {-1, 0}, {-1, 1}};
template <class T>
inline void RST(T& A) {
memset(A, 0, sizeof(A));
}
template <class T>
inline void FLC(T& A, int x) {
memset(A, x, sizeof(A));
}
template <class T>
inline void CLR(T& A) {
A.clear();
}
template <class T>
T& chmin(T& a, T b) {
if (a == -1) {
a = b;
} else {
a = min(a, b);
}
return a;
}
template <class T>
T& chmax(T& a, T b) {
if (a == -1) {
a = b;
} else {
a = max(a, b);
}
return a;
}
constexpr int INF = 1 << 30;
constexpr ll INFL = 1LL << 60;
constexpr ll MOD = 1000000007;
constexpr ld EPS = 1e-12;
ld PI = acos(-1.0);
const double pi = acos(-1.0), eps = 1e-7;
const int inf = 0x3f3f3f3f, ninf = 0xc0c0c0c0, mod = 1000000007;
const int max3 = 2010, max4 = 20010, max5 = 200010, max6 = 2000010;
// LONG_MIN(10进制 10位), LONG_MAX(10进制 19位)
/*
unordered_map / unordered_set
lower_bound 大于等于
upper_bound 大于
reserve 预先分配内存
reverse(all(vec)) 反转
sum = accumulate(a.begin(), a.end(), 0ll);
__builtin_popcount 一的个数
vector / array : upper_bound(all(vec), v)
map: m.upper_bound(v)
区间个数: std::distance(v.begin(), it)
map/set distance 复杂度 O(N)
vector/数组 distance 复杂度 O(1)
vector 去重
sort(nums.begin(), nums.end());
nums.erase(unique(nums.begin(), nums.end()), nums.end());
size_t found=str.find(string/char/char*);
std::string::npos
排序,小于是升序:[](auto&a, auto&b){ return a < b; })
优先队列 priority_queue<Node>:top/pop/push/empty
struct Node {
Node(int t = 0) : t(t) {}
int t;
// 小于是最大堆,大于是最小堆
bool operator<(const Node& that) const { return this->t < that.t; }
};
srand((unsigned)time(NULL));
rand();
mt19937 gen{random_device{}()};
uniform_real_distribution<double> dis(min, max);
function<double(void)> Rand = [that = this]() { return that->dis(that->gen); };
*/
class MedianFinder {
map<int, int> left, right;
deque<int> mid;
int PopMin(map<int, int>& m) {
int ans = m.begin()->first;
m[ans]--;
if (m[ans] == 0) {
m.erase(ans);
}
return ans;
}
int PopMax(map<int, int>& m) {
int ans = m.rbegin()->first;
m[ans]--;
if (m[ans] == 0) {
m.erase(ans);
}
return ans;
}
public:
/** initialize your data structure here. */
MedianFinder() {}
void addNum(int num) {
if (mid.size() == 0) {
mid.push_back(num);
} else if (mid.size() == 1) {
if (mid.back() <= num) {
right[num]++;
mid.push_back(PopMin(right));
} else {
left[num]++;
mid.push_front(PopMax(left));
}
} else {
if (mid.back() <= num) {
right[num]++;
left[mid.front()]++;
mid.pop_front();
} else if (mid.front() >= num) {
left[num]++;
right[mid.back()]++;
mid.pop_back();
} else {
left[mid.front()]++;
mid.pop_front();
right[mid.back()]++;
mid.pop_back();
mid.push_back(num);
}
}
}
double findMedian() {
if (mid.size() == 1) {
return mid.front();
} else {
return (mid.front() + mid.back()) / 2.0;
}
}
};
/**
* Your MedianFinder object will be instantiated and called as such:
* MedianFinder* obj = new MedianFinder();
* obj->addNum(num);
* double param_2 = obj->findMedian();
*/
int main() {
printf("hello ");
// vector<double> ans = {1.00000,-1.00000,3.00000,-1.00000};
// vector<vector<int>> cars = {{1, 2}, {2, 1}, {4, 3}, {7, 2}};
// TEST_SMP1(Solution, getCollisionTimes, ans, cars);
return 0;
}