A30362.宝藏
2025-09-14 11:05:37
发布于:江苏
2阅读
0回复
0点赞
疑似01背包,但是物品可以分割,所以是贪心
#include <iostream>
#include <iomanip>
#include <algorithm>
using namespace std;
int main() {
cout.flags(ios::fixed);
cout.precision(2);
int k; cin >> k;
while (k--) {
int w, s;
cin >> w >> s;
struct thing {
int w, v;
double xjb;
bool operator < (const thing &other) {
return xjb > other.xjb;
}
} things[105];
for (int i=1; i<=s; i++) {
cin >> things[i].w >> things[i].v;
things[i].xjb = (double)things[i].v/things[i].w;
}
sort(things+1, things+s+1);
double ans=0;
for (int i=1; i<=s; i++) {
if (w > things[i].w) {
w -= things[i].w;
ans += things[i].v;
}else {
ans += (double)w/things[i].w*things[i].v;
break;
}
}
cout << ans << endl;
}
return 0;
}
这里空空如也


有帮助,赞一个