FatMouse prepared M pounds of cat food, ready to trade with the cats guarding the warehouse containing his favorite food, JavaBean.
The warehouse has N rooms. The i-th room contains J i i pounds of JavaBeans and requires F i i pounds of cat food. FatMouse does not have to trade for all the JavaBeans in the room, instead, he may get J i i * a% pounds of JavaBeans if he pays F i i * a% pounds of cat food. Here a is a real number. Now he is assigning this homework to you: tell him the maximum amount of JavaBeans he can obtain.
The warehouse has N rooms. The i-th room contains J i i pounds of JavaBeans and requires F i i pounds of cat food. FatMouse does not have to trade for all the JavaBeans in the room, instead, he may get J i i * a% pounds of JavaBeans if he pays F i i * a% pounds of cat food. Here a is a real number. Now he is assigning this homework to you: tell him the maximum amount of JavaBeans he can obtain.
-
Input
-
The input consists of multiple test cases. Each test case begins with a line containing two non-negative integers M and N. Then N lines follow, each contains two non-negative integers J
i
i
and F
i
i
respectively. The last test case is followed by two -1's. All integers are not greater than 1000.
Output
-
For each test case, print in a single line a real number accurate up to 3 decimal places, which is the maximum amount of JavaBeans that FatMouse can obtain.
Sample Input
5 3 7 2 4 3 5 2 20 3 25 18 24 15 15 10 -1 -1
-
Sample Output
13.333 31.500
一只老鼠有M磅的猫粮,另外有一只猫控制了老鼠的N个房间,这些房间里面放了老鼠爱吃的绿豆,给出每个房间的绿豆数量,和这个房间的绿豆所需要的猫粮数,现在要求老鼠用这M磅的猫粮最多能换到多少它爱吃的绿豆?
贪心题,由于所有的绿豆都是一样的,所以如果老鼠想要换到最多的绿豆,便可以换猫控制的房间里面最便宜的绿豆,也就是说先换取单位数量的绿豆所需要最少的猫粮的房间里的绿豆,这样就可以保证换到的绿豆是最多的。具体实现可以用一个结构体,里面保存每个房间里面有的绿豆的数量和换取这个房间的绿豆时所需要的猫粮的数量和换取这个房间的 单位重量的绿豆所需要的猫粮数(以下简称单价),然后再按照单价升序给这些结构体排一次序,这时就可以从最便宜的绿豆开始换了。
#include
#include
#include
using namespace std;
struct Room//struct必须在cmp前
{
int J;
int F;
double JF;
}room[10005];//3个数相关联
bool cmp(const Room a, const Room b)
{
return a.JF > b.JF; //>为升序,<为降序
}
int main()
{
int M, N;
while (cin >> M >> N&&(N!=-1)&&(M!=-1))
{
for (int i = 0; i < N;i++)
{
cin >> room[i].J >> room[i].F;
room[i].JF = (double)room[i].J / room[i].F;
}
sort(room, room + N, cmp); //结构体排序(若数列从1开始,则需在起末位置各+1
double ans=0;
for (int i = 0; i < N; i++) //挨个房间换豆子直到不够换某一个房间所有豆子,按比例换并退出
{
if ((M - room[i].F) >= 0)
{
ans += room[i].J;
M = M - room[i].F;
}
else
{
ans += M*room[i].JF;
break;
}
}
printf("%.3lf\n", ans);
}
return 0;
}