题解 | #最小年龄的3个职工#定义个小根堆就行
最小年龄的3个职工
https://www.nowcoder.com/practice/a9fa482eb05149cdbad88689e3cb5f66
#include <iostream> #include <queue> using namespace std; struct Staff { int id; string name; int age; bool operator<(const Staff& other) const { if (age != other.age) return age > other.age; else if (id != other.id) return id > other.id; return name > other.name; } }; int main() { int n; cin >> n; priority_queue<Staff> pq; while (n--) { Staff staff; cin >> staff.id >> staff.name >> staff.age; pq.push(staff); } for (int i = 0; i < 3 && !pq.empty(); i++) { cout << pq.top().id << " " << pq.top().name << " " << pq.top().age << endl; pq.pop(); } return 0; }