hdu 1009

FatMouse' Trade
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 49098 Accepted Submission(s): 16485

 

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.


 

 

简单贪心....

需要注意的是数据是非负,所以有0的情况要考虑周全,基本都要特殊处理。

多WA了三次,不知道为什么交C++可以过,交G++就不行。

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <iomanip>

using namespace std;

int main()
{
    int  m,n,f[1500],j[1500];
    double scale[1500];
    double ans,sum;
    while(scanf("%d %d",&m,&n)!=EOF&&(m!=-1))
    {  ans=0;
      //  if (m==0)
         memset(scale,0,sizeof(scale));
         memset(f,0,sizeof(f));
         memset(j,0,sizeof(j));
       //  bool flag=false;
       for (int i=1;i<=n;i++)
        {
            scanf("%d %d",&j[i],&f[i]);
            if (f[i]!=0)
            scale[i]=(double)j[i]*1.0/f[i];
            else if (j[i]!=0)
            {
                  ans=ans+j[i];

            }
        }
       // if (flag) {cout<<fixed<<setprecision(3)<<ans<<endl;continue;}
        for (int i=1;i<=n-1;i++)
          for (int k=i+1;k<=n;k++)
          if (scale[i]<scale[k])
        {
            swap(scale[i],scale[k]);
            swap(j[i],j[k]);
            swap(f[i],f[k]);
        }
        sum=0;
       int i=1;
        while (m>=sum&&i<=n)
        {
          ans=ans+j[i];
          sum=sum+f[i];
          i++;
        }
        i--;
        ans=ans-j[i];
        sum=sum-f[i];
        ans=ans+(m-sum)*scale[i];
        cout<<fixed<<setprecision(3)<<ans<<endl;


    }
    return 0;
}

  

posted @ 2015-03-30 16:33  111qqz  阅读(141)  评论(0编辑  收藏  举报