-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathsolution.cpp
36 lines (36 loc) · 1005 Bytes
/
solution.cpp
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
typedef pair<int,int> pii;
class Solution {
public:
double maxAverageRatio(vector<vector<int>>& classes, int ext) {
auto func=[](const pii &A,const pii &B){
return 1.0*(A.first+1)/(A.second+1)-1.0*(A.first)/(A.second)
<1.0*(B.first+1)/(B.second+1)-1.0*(B.first)/(B.second);
};
priority_queue<pii,vector<pii>,decltype(func)> pq(func);
int one=0;
for (vector<int> &i:classes) {
if (i.at(0)==i.at(1)) {
one++;
continue;
}
pq.push({i.at(0),i.at(1)});
}
if (pq.empty()) {
return 1;
}
while (ext!=0) {
auto [x,y]=pq.top();
pq.pop();
pq.push({x+1,y+1});
ext--;
}
double ans=0;
while (!pq.empty()) {
auto [x,y]=pq.top();
pq.pop();
ans+=1.0*x/y;
}
ans+=one;
return 1.0*ans/classes.size();
}
};