Files
python/TangDou/LuoGuBook/P2240.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

38 lines
1023 B
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#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;
}