'commit'
This commit is contained in:
38
TangDou/LuoGuBook/P2240.cpp
Normal file
38
TangDou/LuoGuBook/P2240.cpp
Normal file
@@ -0,0 +1,38 @@
|
||||
#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;
|
||||
}
|
||||
Reference in New Issue
Block a user