-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem_1792_maxAverageRatio.cc
More file actions
59 lines (52 loc) · 1.48 KB
/
Copy pathProblem_1792_maxAverageRatio.cc
File metadata and controls
59 lines (52 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <functional>
#include <iostream>
#include <queue>
#include <vector>
#include "UnitTest.h"
using namespace std;
class Solution
{
public:
// classes[i]=[passi, totali]
// 第i个班级总共有totali个学生
// 其中只有 passi 个学生可以通过考试
// 额外有extraStudents个聪明的学生
// 给这extraStudents个学生每人都安排一个班级,使得所有班级的平均通过率最大
// 返回在安排这extraStudents个学生去对应班级后的最大平均通过率
double maxAverageRatio(vector<vector<int>>& classes, int extraStudents)
{
priority_queue<std::tuple<double, int, int>> q;
// 如果再来一人,班级通过率提升多少
auto diff = [](int x, int y) -> double { return (double) (x + 1) / (y + 1) - (double) x / y; };
double ans = 0;
for (auto& c : classes)
{
int x = c[0];
int y = c[1];
ans += (double) x / y;
q.push({diff(x, y), x, y});
}
for (int _ = 0; _ < extraStudents; _++)
{
auto [d, x, y] = q.top();
q.pop();
ans += d;
q.push({diff(x + 1, y + 1), x + 1, y + 1});
}
return ans / classes.size();
}
};
void testMaxAverageRatio()
{
Solution s;
vector<vector<int>> c1 = {{1, 2}, {3, 5}, {2, 2}};
vector<vector<int>> c2 = {{2, 4}, {3, 9}, {4, 5}, {2, 10}};
EXPECT_EQ_DOUBLE(0.78333, s.maxAverageRatio(c1, 2));
EXPECT_EQ_DOUBLE(0.53485, s.maxAverageRatio(c2, 4));
EXPECT_SUMMARY;
}
int main()
{
testMaxAverageRatio();
return 0;
}