You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

38 lines
1023 B

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include <bits/stdc++.h>
using namespace std;
const int N = 110;
//金币的结构体
struct coin {
int m, v; //m:重量 v:价值
} a[N];
//比较器
bool cmp(const coin &x, const coin &y) {
return x.v * y.m > y.v * x.m; //这个技巧有点意思,防止出现浮点数
}
int main() {
int n, t, c, i;
float ans = 0;
scanf("%d%d", &n, &t);
//剩余重量
c = t;
//输入
for (i = 1; i <= n; i++) scanf("%d%d", &a[i].m, &a[i].v);
//排序
sort(a + 1, a + n + 1, cmp);
//能装尽装
for (i = 1; i <= n; i++) {
if (a[i].m > c)break; //如果不能完整装下,就退出
c -= a[i].m; //装的下的话可容纳的重量减少a[i].m
ans += a[i].v; //装的下的话价值增加a[i].v
}
//如果还有金币,而且还有剩余空间。此时装入剩余空间那么多的金币
if (i < n && c > 0)ans += 1.0 * c * a[i].v / a[i].m;
printf("%.2lf", ans);
return 0;
}