-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExamRoom.cpp
More file actions
67 lines (67 loc) · 1.92 KB
/
ExamRoom.cpp
File metadata and controls
67 lines (67 loc) · 1.92 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
static int n;
class ExamRoom {
public:
struct comp{
bool operator()(const pair<int, int>& p1, const pair<int, int>& p2) const {
int x1 = p1.first == -1 || p1.second==n ? p1.second-p1.first-1 : (p1.second-p1.first) >> 1;
int x2 = p2.first == -1 || p2.second==n ? p2.second-p2.first-1 : (p2.second-p2.first) >> 1;
return (x1 == x2 && p1.first < p2.first) || x1 > x2;
}
};
set<pair<int,int>, comp> s;
ExamRoom(int N) {
n = N;
s.insert({-1, n});
}
int seat() {
auto it = s.begin();
auto [b,e] = *it;
int chair;
s.erase(it);
if (b == -1) {
chair = 0;
s.insert({0, e});
}
else if (e == n) {
chair = n - 1;
s.insert({b, n - 1});
}
else{
chair = b + ((e - b) >> 1);
s.insert({b, chair});
s.insert({chair, e});
}
return chair;
}
void leave(int p) {
if (p == 0){
auto it = find_if(s.begin(), s.end(), [](const pair<int,int>& pp){
return pp.first == 0;
});
auto [b,e] = *it;
s.erase(it);
s.insert({-1, e});
}
else if(p == n - 1){
auto it = find_if(s.begin(), s.end(), [&](const pair<int,int>& pp){
return pp.second == n-1;
});
auto [b,e] = *it;
s.erase(it);
s.insert({b, n});
}
else{
auto it = find_if(s.begin(), s.end(), [&](const pair<int,int>& pp){
return pp.first == p;
});
auto [_, e] = *it;
s.erase(it);
it = find_if(s.begin(), s.end(),[ &](const pair<int,int>& pp){
return pp.second == p;
});
auto [b, __] = *it;
s.erase(it);
s.insert({b, e});
}
}
};