力扣1792.最大平均通过率
-
每个班级加上一个人以后得通过率增量不同
- 将优先级最高的班级放队列顶,每次操作即可
-
class Solution { public: struct Radio{ int pass; int total; //满足该条件 oth的优先级更高 bool operator < (const Radio& oth)const{ return (long long)(oth.total + 1)*oth.total * (total - pass) < (long long)(total + 1) * total * (oth.total - oth.pass); } }; double maxAverageRatio(vector<vector<int>>& classes, int extraStudents) { priority_queue<Radio> q; for(auto &c:classes) q.push({c[0],c[1]}); for(int i=0;i<extraStudents;i++) { auto [pass,total] = q.top(); q.pop(); q.push({pass+1,total+1}); } double res = 0; for(int i=0;i<classes.size();i++) { auto [pass,total] = q.top(); q.pop(); res += 1.0 * pass / total; } return res / classes.size(); } };