FatMouse' Trade

来源:互联网 发布:网络爬虫用什么语言 编辑:程序博客网 时间:2024/06/10 22:25
Problem Description
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] pounds of JavaBeans and requires F[i] pounds of cat food. FatMouse does not have to trade for all the JavaBeans in the room, instead, he may get J[i]* a% pounds of JavaBeans if he pays F[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] and F[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 37 24 35 220 325 1824 1515 10-1 -1
 

Sample Output
13.33331.500
 

Author
CHEN, Yue
 

Source
ZJCPC2004
翻译:老鼠有m磅的猫粮,想用它来引开猫去拿它喜欢的食物,javabean,有n个房间,若要引开猫,必须付出相应的猫食f[i],他不想每次都付出所有的f[i],若它付出f[i]a%,则得到j[i]a%。求老鼠能吃到的做多的JavaBean.
j[i]/f[i]的比例要大。j[i]/f[i]的比例越大,证明在这个房间,小鼠付出得到的收获最有价值。
#include<stdio.h>#include<algorithm>using namespace std;struct node{    int j,f;    double bili;}mouse[1005];int cmp(node x,node y){    return x.bili>y.bili;}int main(){    int m,n,i;    while(~scanf("%d%d",&m,&n))    {        if(m==-1&&n==-1)        break;        for(i=0;i<n;i++)        {            scanf("%d%d",&mouse[i].j,&mouse[i].f);            mouse[i].bili=(double)mouse[i].j/mouse[i].f;        }        stable_sort(mouse,mouse+n,cmp);        int sum=0,count=0,ans=0;        for(i=0;i<n;i++)        {            sum+=mouse[i].f;            if(sum<=m)            {                count+=mouse[i].j;                ans+=mouse[i].f;            }            else            {                sum=m-ans;                break;            }        }        double cont=(double)(mouse[i].j*(double)sum/mouse[i].f);        printf("%.3f\n",count+cont);    }    return 0;}

0 0