hdu 1009 FatMouse' Trade(贪心)

题目来源:hdu 1009 FatMouse’ Trade

FatMouse’ Trade

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 54581 Accepted Submission(s): 18299

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

题目大意:

输入m,n,m可以看做总容量,n组数据,每组数据输入J、F;J可看做该物品的总价值,F可看做该物品所占空间为F。所求的是m空间所能装的最大价值是多少,保留三位小数。

题目分析:

此题意在考查贪心思想,想要获得最大价值,我们就要首先取单位价值最大的,将其取完或者取满空间,若空间较大,则再取剩余的单位价值最大的物品。由此可知,此题需要对物品的单位价值进行排序,即(J/F)值,然后使用贪心即可。

AC代码:

#include<stdio.h>
#include<algorithm>
using namespace std;
struct node{        //用结构体存放该物品的价值、体积、单位价值
    int j,f;
    double v;
};
node edge[1010];
int cmp(node a,node b)  //对物品的单位价值从大到小进行排序
{
    return a.v>b.v;
}
int main()
{
    int m,n;
    while(scanf("%d%d",&m,&n)!=EOF&&m!=-1&&n!=-1)
    {
        for(int i=0;i<n;i++)            //输入每件物品的价值及体积,并计算单位体积
        {
            scanf("%d%d",&edge[i].j,&edge[i].f);
            edge[i].v=(double)edge[i].j/edge[i].f;
        }
        sort(edge,edge+n,cmp);  //进行排序
        double ans=0;           //用来计算最大总价值
        for(int i=0;i<n;i++)
        {
            if(m>=edge[i].f)    //若空间较大,则取完此物品,剩余空间为m减去该物品的体积
            {
                m-=edge[i].f;
                ans+=edge[i].j; //ans计算当前所取的最大价值
            }
            else
            {
                ans+=edge[i].v*m;   //空间不足以装完此物品时,就装满剩余空间
                break;              //此时所装价值为单位价值与所装空间之积,已无法继续装,故跳出
            }
        }
        printf("%.3lf\n",ans);
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

hdu 1009 FatMouse' Trade(贪心)

时间: 2024-10-09 00:54:00

hdu 1009 FatMouse' Trade(贪心)的相关文章

【贪心专题】HDU 1009 FatMouse&#39; Trade (贪心选取)

链接:click here~~ 题意:老鼠准备了M磅猫食,准备拿这些猫食跟猫交换自己喜欢的食物.有N个房间,每个房间里面都有食物.你可以得到J[i]单位的食物,但你需要付出F[i]单位的的猫食. 计算M磅猫食可以获得最多食物的重量. [解题思路]贪心算法,求最优解.将J[i]/F[i]的值从大到小排列,每次取最大的,局部最优,达到全局最优,从而获得最大值. 代码: // 贪心策略,优先选择投资最大的房间,每选择一次,交换次数依次减少,最后的次数用于价值最小的 //注意精度转化:1.0*(int

贪心/hdu 1009 FatMouse&#39; Trade

题意 有n种物品,每一种需要不同的消费,现在手里有m块钱,求问最多可以买多少 分析 贪心 把每一种物品的价格算出来,然后sort一下,按照价格从便宜到贵排序,能买多少买多少,买买买! Accepted Code 1 /* 2 PROBLEM:hdu1009 3 AUTHER:Nicole Lam 4 MEMO:贪心 5 */ 6 7 #include<cstdio> 8 #include<algorithm> 9 using namespace std; 10 11 12 stru

HDU 1009 FatMouse&#39; Trade (贪心算法)

题意:就是老鼠要用猫粮换粮食,第i个房间一些东西,要用东西去换,可以不全换.问给定的猫粮最多能换多少粮食. 析:贪心算法.我们先算出来每个房间物品的平均价格是多少,肯定越低越好,并且如果能全换就全换,如果不能, 肯定是最后一次了,就把剩下全部换了,看看能换多少.求和. 代码如下: #include <iostream> #include <cstdio> #include <algorithm> #include <queue> #include <v

HDU 1009.FatMouse&#39; Trade【贪心算法】【8月16】

FatMouse' Trade 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 req

HDU 1009 FatMouse&#39; Trade题解

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] pound

HDU 1009 FatMouse&#39; Trade

FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 59851    Accepted Submission(s): 20095 Problem Description FatMouse prepared M pounds of cat food, ready to trade with the cats gu

[2016-02-04][HDU][1009][FatMouse&#39; Trade]

[2016-02-04][HDU][1009][FatMouse' Trade] FatMouse' Trade Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u Submit Status Description FatMouse prepared M pounds of cat food, ready to trade with the cats guarding the warehouse

HDU ACM 1009 FatMouse&#39; Trade

分析:贪心,每次优先取需要食物的重量和猫食的重量相比最大的,所以先要按比值进行排序,注意J[i]和B[i]要声明为实型,声明为整形就挂了. #include<iostream> #include<algorithm> using namespace std; struct FAT { double f,j; double avg; } fat[1005]; bool cmp(FAT a,FAT b) { if(a.avg<b.avg) return true; else re

杭电oj 1009 FatMouse&#39; Trade

Tips:本题采用贪心算法,类似于背包问题,关键在于读入数据之后,将数据按 J[i]/F[i] 从大到小排列即可. 1 /**本程序主要采用贪心算法思想,类似于背包问题*/ 2 #include<stdio.h> 3 #include<string.h> 4 int main() 5 { 6 int M,N; 7 while(scanf("%d %d",&M,&N)) 8 { 9 if(M == -1 && N == -1) 10