-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleet102.cpp
More file actions
39 lines (34 loc) · 1015 Bytes
/
leet102.cpp
File metadata and controls
39 lines (34 loc) · 1015 Bytes
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
using info = tuple<double, int, int>;
info A[100000];
class Solution {
public:
static double maxAverageRatio(vector<vector<int>>& classes, int k) {
const int n = classes.size();
double sum = 0.0;
int i = 0;
for (auto& pq : classes) {
int p = pq[0], q = pq[1];
sum += (double)p/q;
double inc=(double)(q - p) / (q * (q + 1.0));
A[i++]={inc, p, q};
}
make_heap(A, A+n);
for (int i = 0; i < k; i++) {
pop_heap(A, A+n);
auto [r, p, q] = A[n-1];
if (r==0) break;// early stop
// Add the current inc to the sum
sum += r;
double r2= (double)(q - p) / ((q +1.0)* (q + 2.0));
A[n-1]={ r2, p+1, q+1};
push_heap(A, A+n);
}
return sum / n;
}
};
auto init = []() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
return 'c';
}();