824 Greedy Mouse

来源:互联网 发布:淘宝联盟怎么做高佣 编辑:程序博客网 时间:2024/05/29 09:55


Greedy Mouse

时间限制:1000 ms  |  内存限制:65535 KB
难度:3
描述

A fat mouse prepared M pounds of cat food,ready to trade with the cats guarding the warehouse containing his

favorite food:peanut. The warehouse has N rooms.The ith room containsW[i] pounds of peanut and requires 

F[i] pounds of cat food. Fatmouse does not have to trade for all the peanut in the room,instead,he may get 

 W[i]*a% pounds of peanut if he pays F[i]*a% pounds of cat food.The mouse is a stupid mouse,so can you tell 

him the maximum amount of peanut he can obtain.

输入
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 W[i] and F[i] respectively. The test case is terminated by two -1. All integers are not greater than 1000.
输出
For each test case, print in a single line a real number accurate up to 3 decimal places, which is the maximum amount of penaut that FatMouse can obtain.
样例输入
5 37 24 35 220 325 1824 1515 10-1 -1
样例输出
13.33331.500

#include<iostream>#include<cstdio>#include<algorithm>using namespace std;int main(){    int i,j,m,n;    int w[1001],f[1001];    double max,v[1001];    while(scanf("%d%d",&m,&n)!=EOF && !((m==-1)&&(n==-1)))    {        max=0;        for(i=0;i<n;i++)        {            scanf("%d%d",&w[i],&f[i]);            v[i]=1.0*w[i]/f[i];        }        for(i=0;i<n-1;i++)            for(j=i+1;j<n;j++)                if(v[i]<v[j])                {                    swap(w[i],w[j]);                    swap(f[i],f[j]);                    swap(v[i],v[j]);                }        for(i=0;i<n;i++)        {            if(m-f[i]<=0) { max+=1.0*m*v[i]; break; }            else            {                max+=w[i];                m-=f[i];            }        }        printf("%.3lf\n",max);    }}


0 0
原创粉丝点击