### 题目

In an exam room, there are N seats in a single row, numbered 0, 1, 2, …, N-1.

When a student enters the room, they must sit in the seat that maximizes the distance to the closest person. If there are multiple such seats, they sit in the seat with the lowest number. (Also, if no one is in the room, then the student sits at seat number 0.)

Return a class ExamRoom(int N) that exposes two functions: ExamRoom.seat() returning an int representing what seat the student sat in, and ExamRoom.leave(int p) representing that the student in seat number p now leaves the room. It is guaranteed that any calls to ExamRoom.leave(p) have a student sitting in seat p.

Example 1:

Input: ["ExamRoom","seat","seat","seat","seat","leave","seat"], [,[],[],[],[],,[]]
Output: [null,0,9,4,2,null,5]
Explanation:
ExamRoom(10) -> null
seat() -> 0, no one is in the room, then the student sits at seat number 0.
seat() -> 9, the student sits at the last seat number 9.
seat() -> 4, the student sits at the last seat number 4.
seat() -> 2, the student sits at the last seat number 2.
leave(4) -> null
seat() -> 5, the student sits at the last seat number 5.


Note:

• 1 <= N <= 10^9
• ExamRoom.seat() and ExamRoom.leave() will be called at most 10^4 times across all test cases.
• Calls to ExamRoom.leave(p) are guaranteed to have a student currently sitting in seat number p.

### Max Heap管理空座位

    Gap             Gap
|<----->|   |<------------->|
X - - - - X X - - - - - - - - X X - - -


    Gap             Gap
|<----->|   |<------------->|
X - - - - X X - - - - - - - - X X - - -
|<-|        |<-----|
distance     distance


       Gap                                  Gap
|<------------>|                       |<----------->|
- - - - - - - - X X - - - - - - - - X X - - - - - - -
|------------->|                       |------------>|
distance                               distance


• seat()：O(logN)
• leave()： O(N)

#### 代码

class ExamRoom {

private class Gap {
private int lo, hi, dis;
private Gap(int lo, int hi) {
this.lo = lo;
this.hi = hi;
this.dis = (lo == 0 || hi == size - 1)? hi - lo : (hi - lo) / 2;
}

@Override
public boolean equals(Object o) {
if (!(o instanceof Gap)) return false;
return this.lo == ((Gap)o).lo;
}

}

private int size;
private PriorityQueue<Gap> gaps;

public ExamRoom(int n) {
size = n;
gaps = new PriorityQueue<Gap>((Gap a, Gap b) -> (a.dis != b.dis)? b.dis - a.dis : a.lo - b.lo);
}

public int seat() {
Gap gap = gaps.poll();
if (gap.lo == 0) {
if (gap.hi > 0) gaps.add(new Gap(1, gap.hi));
return 0;
}
if (gap.hi == size - 1) {
if (gap.lo < size - 1) gaps.add(new Gap(gap.lo, size - 2));
return size - 1;
}
int seat = gap.lo + (gap.hi - gap.lo) / 2;
if (seat - gap.lo > 0) gaps.add(new Gap(gap.lo, seat - 1));
if (gap.hi - seat > 0) gaps.add(new Gap(seat + 1, gap.hi));
return seat;
}

public void leave(int p) {
Iterator<Gap> ite = gaps.iterator();
int head = p, tail = p;
while (ite.hasNext()) {
Gap gap = ite.next();
if (gap.hi == p - 1) {
ite.remove();
} else if (gap.lo == p + 1) {
tail = gap.hi;
ite.remove();
}
} 