注解
1、先按浓度排序,浓度从低到高。每次添加一瓶新的,算出溶质,计算添加该瓶之后,浓度是否超过要求。如果没有超过,就添加然后继续循环,否则,就结束循环,输出答案。
2、简单的贪心和排序的应用。
代码
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int C;
scanf("%d", &C);
for(int i=0; i<C; i++){
int n, V, W;
scanf("%d %d %d", &n, &V, &W);
int a[n];
for(int j=0; j<n; j++){
scanf("%d", &a[j]);
}
sort(a, a+n);
int cnt = 0;
double ans = 0;
double now = 0;
for(int j=0; j<n; j++){
now += a[j]*V;
if(now/(V*(j+1))<=W){
ans = now/(V*(j+1));
cnt++;
}
else{
break;
}
}
printf("%d %.2lf\n", cnt*V, ans/100.0);
}
return 0;
}