题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2570
用贪心算法,浓度最小的优先选择,凑出最大体积。
discuss里面一堆人说精度问题,其实压根没有问题,搞了半天我看到自己输出的是数量*100而不是数量*体积V,无语。。。
代码如下:
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
const int maxn = 100 + 5;
int C, n, V;
double p[maxn], W;
int main(void) {
scanf("%d", &C);
while (C--) {
scanf("%d %d %lf", &n, &V, &W);
for (int i = 0; i < n; i++)
scanf("%lf", &p[i]);
sort(p, p + n);
int ans = 0;
double w = 0, v = 0;
for (int i = 0; i < n; i++) {
w += p[i]; v++; ans++;
if (w - W * v > 0) {
w -= p[i]; v--;
ans--;
break;
}
}
printf("%d ", ans * V);
if (ans == 0)printf("0.00\n");
else printf("%.2lf\n", w / v / 100.0);
}
return 0;
}