码迷,mamicode.com
首页 > 其他好文 > 详细

HDU FatMouse' Trade

时间:2015-05-26 14:28:55      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:

FatMouse‘ Trade

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 69   Accepted Submission(s) : 18

Font: Times New Roman | Verdana | Georgia

Font Size:

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 3
7 2
4 3
5 2
20 3
25 18
24 15
15 10
-1 -1

Sample Output

13.333
31.500

Author

CHEN, Yue

Source

ZJCPC2004
  这题的描述主要是:老鼠有m镑猫粮准备和猫换javebean。现在有n间房子,里面各有若干javebean可供老鼠兑换,兑换的条件是每个房间有各自所需的猫粮。例如5 3 7 2 4 3 5 2这组数据表示老鼠有5镑的猫粮,共有3间房子,每间房子依次有7,4,5镑的javebean各需要2,3,2猫粮换。我们要求的是用5镑换最多的javebean。我们可以知道用2镑换第一间房子的javebean用2镑换第3间房子的javebean,最后用1镑换第二间房子(1*(4/3)的javebean,一共13.333。
AC代码:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;
struct Fun{
    int  CatFood,JavaBean;
    double ratio;
};
Fun t[10000],temp;
bool c(Fun a,Fun b){
   return a.ratio>b.ratio;
}
int main(){
    int m,n,i,j;
    while(scanf("%d%d",&m,&n)&&(m!=-1||n!=-1)){
        i=0;
        while(i<n){
           cin>>t[i].JavaBean>>t[i].CatFood;
            t[i].ratio=(double)t[i].JavaBean/t[i++].CatFood;
        }
        sort(t,t+n,c);
        double sum=0;
        for(i=0;i<n&&m!=0;i++){
                if(m>=t[i].CatFood){
                sum+=t[i].JavaBean;
                m-=t[i].CatFood;
            }
            else {
                sum+=m*((double)t[i].JavaBean/t[i++].CatFood);
                 break;
            }
        }
        printf("%.3f\n",sum);
    }
    return 0;
}

这里用结构体来储存数据,然后调用sort();进行排序计算出最优解。//因为有个参数写错导致一直WA,我找了半天没找到orz,细心啊!
sort用法sort(n,n+m,comp);n是数组名,m是数组长度,comp是自定义函数按照函数规定来进行排序。
运行结果:
技术分享
 

HDU FatMouse' Trade

标签:

原文地址:http://blog.csdn.net/zp___waj/article/details/46006533

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!