有k人攜帶價值v_i的禮物到達,共開m次門。第i次開門時有t_i人到達,放入p_i人。所有人到達後再開一次門放入剩餘所有人。規則:禮物價值高者優先,價值相同先到先進。q次詢問,輸出第n個進場者的名字。m可為0。
id、value、name,id體現到達順序)。a時間、b放入人數)。set(或priority_queue)作為候選池,按value降序、id升序排序。b人記入答案序列。struct Person {
int id, value;
char name[220];
bool operator<(const Person& o) const {
return value == o.value ? id < o.id : value > o.value; // 大根堆語義
}
};
struct DoorEvent {
int time, count;
bool operator<(const DoorEvent& o) const { return time < o.time; }
};
sort(people by arrival time)
sort(events by time)
idx = 0
for each event e:
while idx < n && people[idx].arrival <= e.time:
candidate_set.insert(people[idx++])
repeat e.count times:
if candidate_set not empty:
ans.push_back(*candidate_set.begin())
candidate_set.erase(begin())
// 最終開門
while candidate_set not empty:
ans.push_back(*candidate_set.begin())
candidate_set.erase(begin())
// 回答查詢
for each query q:
print ans[q-1].name
O(n log n + m log m)O(n log n)O(1)O((n+m) log n),滿足n ≤ 150000約束。m=0時無開門事件,需直接進入最終開門邏輯。set迭代器失效:erase(begin())安全,但erase(it++)需謹慎。