【LeetCode-1792】最大平均通过率

问题

一所学校里有一些班级,每个班级里有一些学生,现在每个班都会进行一场期末考试。给你一个二维数组 classes ,其中 classes[i] = [passi, totali] ,表示你提前知道了第 i 个班级总共有 totali 个学生,其中只有 passi 个学生可以通过考试。

给你一个整数 extraStudents ,表示额外有 extraStudents 个聪明的学生,他们 一定 能通过任何班级的期末考。你需要给这 extraStudents 个学生每人都安排一个班级,使得 所有 班级的 平均 通过率 最大 。

一个班级的 通过率 等于这个班级通过考试的学生人数除以这个班级的总人数。平均通过率 是所有班级的通过率之和除以班级数目。

请你返回在安排这 extraStudents 个学生去对应班级后的 最大 平均通过率。与标准答案误差范围在 10-5 以内的结果都会视为正确结果。

示例

输入: classes = [[1,2],[3,5],[2,2]], extraStudents = 2
输出: 0.78333
解释: 你可以将额外的两个学生都安排到第一个班级,平均通过率为 (3/4 + 3/5 + 2/2) / 3 = 0.78333 。

输入: classes = [[2,4],[3,9],[4,5],[2,10]], extraStudents = 4
输出: 0.53485

解答

class Solution {
public:
    typedef pair<int, int> PII;
    struct cmp {
        bool operator() (PII& a, PII& b) {
            double ares = (double)(a.first + 1) / (a.second + 1) - (double)a.first / a.second;
            double bres = (double)(b.first + 1) / (b.second + 1) - (double)b.first / b.second;
            return ares < bres;
        }
    };
    double maxAverageRatio(vector<vector<int>>& classes, int extraStudents) {
        double res = 0;
        priority_queue<PII, vector<PII>, cmp> pq;
        for (auto& v : classes)
            pq.emplace(v[0], v[1]);
        while (extraStudents--) {
            auto [x, y] = pq.top(); pq.pop();
            pq.emplace(x + 1, y + 1);
        }
        while (!pq.empty()) {
            auto [x, y] = pq.top(); pq.pop();
            res += (double)x / y;
        }
        return res / classes.size();
    }
};

重点思路

贪心+优先队列(大根堆)。每次向队列中添加一个“好学生”,队列排序标准为“添加一个学生后通过率上升大小”。

手写优先队列

class Solution {
public:
    typedef pair<int, int> PII;
    double maxAverageRatio(vector<vector<int>>& classes, int extraStudents) {
        Pqueue pq;
        for (auto& v : classes)
            pq.insert({v[0], v[1]});
        while (extraStudents--) {
            auto [x, y] = pq.pop_top();
            pq.insert({x + 1, y + 1});
        }
        double res = 0;
        while (!pq.empty()) {
            auto [x, y] = pq.pop_top();
            res += (double)x / y;
        }
        return res / classes.size();
    }
private:
    struct Pqueue {
        vector<PII> pq{{0, 0}};
        bool empty() {
            return pq.size() == 1;
        }
        void insert(PII num) {
            pq.push_back(num);
            swim(pq.size() - 1);
        }
        PII pop_top() {
            auto res = pq[1];
            swap(pq[1], pq.back());
            pq.pop_back();
            sink(1);
            return res;
        }
        bool is_prior(int i, int j) {
            auto a = pq[i], b = pq[j];
            double ares = (double)(a.first + 1) / (a.second + 1) - (double)a.first / a.second;
            double bres = (double)(b.first + 1) / (b.second + 1) - (double)b.first / b.second;
            return ares > bres;
        }
        void swim(int i) {
            while (i > 1 && is_prior(i, i / 2)) {
                swap(pq[i], pq[i / 2]);
                i /= 2;
            }
        }
        void sink(int i) {
            int n = pq.size();
            while (i * 2 < n) {
                int j = i * 2;
                if (j + 1 < n && is_prior(j + 1, j)) j++;
                if (is_prior(i, j)) break;
                swap(pq[i], pq[j]);
                i = j;
            }
        }
    };
};
posted @ 2021-03-15 00:40  tmpUser  阅读(185)  评论(0编辑  收藏  举报