HDU 1009 贪心问题

FatMouse‘ Trade

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

Total Submission(s): 46205 Accepted Submission(s): 15482

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克的猫粮,想去跟猫兑换食物,但是老鼠喜欢的食物在仓库的不同层,每层的兑换率都不一样。求的是最多能兑换到多少食物。

5 3

7 2

4 3

5 2

这个案例代表有老鼠有5克猫粮 ,食物一共放在不同的三层 ,第一层有7克食物,需要2克猫粮兑换,兑换率为7/2;第二层第三层类似。

所以要贪心的话,就要先选出兑换率最大的一层开始兑换。上代码上解析。

#include <stdio.h>
#define wbx 50000
int a[wbx]; //wbx代表第wbx层,a[wbx]代表第wbx层的食物。
int b[wbx]; //b[wbx]第wbx层的猫粮。
double p[wbx];// p代表第wbx层的兑换率。
#include <string.h>
#include <algorithm>
using namespace std;
void solve()
{
	int m,n;
	while(scanf("%d%d",&m,&n)&&m!=-1 &&n!=1)
	{
		int i,k;
		double sum=0;
		for(i=1;i<=n;i++)
		{
			scanf("%d%d",&a[i],&b[i]);//输入每层的食物和猫粮
			p[i]=double(a[i]*1.0/b[i]);  //算出每层的兑换率
		}
		while(m!=0 &&n!=0)  //m=0或者n=0代表着结束,跳出循环。
		{
			double max=-999;
			for(i=1;i<=n;i++)
			{
				if(p[i]>max)
				{
					max=p[i];//每次都先找出每层兑换率最大的。
					k=i;//把值赋给k。
				}
			}
			if(m-b[k]>=0)// 如果猫粮大于等于这层的猫粮
			{
				m-=b[k];//  减去这层的猫粮。
				sum+=a[k];// 得到这层所有食物。
				p[k]=0;// 这层兑换完了,让它的兑换率为0。
			}
			else
			{
				sum+=m*p[k];// 如果剩余猫粮已经小于想兑换的这层的猫粮,那就用剩下的猫粮兑换所有的食物。
				m=0;//猫粮为0,跳出循环。
			}
		}
		sum=double(sum);
		printf("%.3lf\n",sum);//sum就是最优解。。
	}
}
int main()
{
	solve();//问题解决。。
	return 0;
}
时间: 2024-10-12 14:42:15

HDU 1009 贪心问题的相关文章

hdu 1009 贪心基础题

B - 贪心 基础 Crawling in process... Crawling failed 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 contai

HDU 1009贪心

代码如下.需要说明的是,之前一直WA,发现对这题来说,难点不是贪心,是对浮点数的处理,做题经验不足会导致一直不能AC.在代码第43行,用1.0*m*x[i].a/x[i].b就能AC,但是如果直接用x[i].re*m则会WA.其实计算re只是为了排序,在最后计算结果的时候如果还用这个浮点数就会导致多个浮点误差累积,使误差会更大.而直接用m*x[i].a/x[i].b这样做,由于先算的是整数之间的乘法,然后再算一次除法,那么得到浮点数的误差就只有一次,而不是上一种情况的多次误差累积. 1 #inc

hdu 1009 贪心入门

Mycode: compile error,i don't know why? #include <iostream> #include<algorithm> #include<cstdio> #define maxn 10005 using namespace std; struct room { int j; int f; double val; }; struct room r[3001]; bool compare(struct room a,struct ro

hdu 1009 贪心

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

【贪心专题】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(贪心)

题目来源: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

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