FatMouse‘ Trade
5 3 7 2 4 3 5 2 20 3 25 18 24 15 15 10 -1 -1
13.333 31.500
#include<cstdio>
#include<algorithm>
using namespace std;
struct ss{
double val,wei,vval;
};
bool cmp(ss x,ss y){
if(x.vval>y.vval) return true;
else return false;
}
int main(){
int n,m;
while(scanf("%d%d",&m,&n)!=EOF&&(n!=-1&&m!=-1)){
ss f[1010];
for(int i=0;i<n;i++){
scanf("%lf %lf",&f[i].wei,&f[i].val);
f[i].vval=f[i].wei/f[i].val;
}
sort(f,f+n,cmp);
double Max=0;
for(int i=0;i<n&&m>0.000001;i++){
if(m>=f[i].val){
Max+=f[i].wei;
m-=f[i].val;
}
else{
Max+=m*f[i].vval;
m=0;
}
}
printf("%.3f\n",Max);
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
HDU 1009.FatMouse' Trade【贪心算法】【8月16】
原文地址:http://blog.csdn.net/a995549572/article/details/47703117