zoukankan      html  css  js  c++  java
  • 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;
    }
    

      

  • 相关阅读:
    多表查询+多对多 三表连查+子查询
    几个重要的关键字where+group by +having +order by + limit
    拷贝表 *** 与******
    一对一关系的补充
    几种基本的约束和外键(一对一 多对多 多对一)级联关系
    创建表的完整语法 数字类型(整型 浮点型) 字符型 时间和日期类型 集合和枚举类型
    随记Litter note
    视图 触发器 事务(重要) 存储过程 内置函数 流程控制 索引
    luogu P2774 方格取数问题
    luogu P4014 分配问题
  • 原文地址:https://www.cnblogs.com/111qqz/p/4378438.html
Copyright © 2011-2022 走看看