http://acm.hdu.edu.cn/showproblem.php?pid=1009
开始贪心专题。
题意:一共有n个房子,每个房子里有老鼠喜欢吃的javabeans,但是每个房间里的javabeans的价格不一样。老鼠用m元,问m元最多可以卖多少javabeans,其中每个房间里的javabeans可以被分割。
先求单价,然后排个序就行了。
#include <iostream>
#include <algorithm>
using namespace std;
#define N 1005
struct warehouse_node{
double j,f,unit_price;
};
warehouse_node warehouse[N];
bool cmp(warehouse_node a,warehouse_node b){
if (a.unit_price>=b.unit_price)
return true;
else return false;
}
int main(){
#ifndef ONLINE_JUDGE
freopen("1009in.txt","r",stdin);
#endif
int n,i;
double max,m;
while (scanf("%lf%d",&m,&n)!=EOF){
if (m==-1&&n==-1)
break;
for (i=0;i<n;i++){
scanf("%lf%lf",&warehouse[i].j,&warehouse[i].f);
warehouse[i].unit_price=warehouse[i].j/warehouse[i].f;
}
sort(warehouse,warehouse+n,cmp);
max=0;
for (i=0;i<n&&m;i++){
if (m>=warehouse[i].f){
m=m-warehouse[i].f;
max+=warehouse[i].j;
}
else {
max+=m/warehouse[i].f*warehouse[i].j;
m=0;
}
}
printf("%.3lf\n",max);
}
return 0;
}