1070 Mooncake

给出仓库容量,不同种类月饼的库存和总价值,问把仓库装满后,最高的货值是多少。采购单价高的,若有空余,采购次高者,以此类推。

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
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

struct mooncake {
float price, amount, unitPrice;
};

int cmp(mooncake a, mooncake b) {
return a.unitPrice > b.unitPrice;
}

int main() {
int n, total;
cin >> n >> total;
vector<mooncake> cake(n);
for (int i = 0; i < n; i++) cin >> cake[i].amount;
for (int i = 0; i < n; i++) cin >> cake[i].price;
for (int i = 0; i < n; i++) {
cake[i].unitPrice = cake[i].price / cake[i].amount;
}
sort(cake.begin(), cake.end(), cmp);
float res = 0.0;
for (int i = 0; i < n; i++) {
if (total >= cake[i].amount) {
res += cake[i].price;
} else {
res += cake[i].unitPrice * total;
break;
}
total -= cake[i].amount;
}
printf("%.2f", res);

return 0;
}