HDU1009 FatMouse' Trade 【贪心】

FatMouse‘ Trade

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

Total Submission(s): 42786    Accepted Submission(s): 14274

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个物品和每个物品的重量及价值,单个物品可以任意切分,求背包能获得的最大装载价值。

题解:可以求出每个物品的单位重量价值,排序,每次选择单位价值最大的装包即可。

#include <stdio.h>
#include <algorithm>
#define maxn 1002
using std::sort;

struct Node{
	int v, c;
	double val;
} arr[maxn];

bool cmp(Node a, Node b)
{
	return a.val > b.val;
}

int main()
{
	//freopen("in.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);
	int m, n, i, a, b;
	double ans;
	while(scanf("%d%d", &m, &n), m >= 0 && n >= 0){
		for(i = 0; i < n; ++i){
			scanf("%d%d", &a, &b);
			arr[i].val = a * 1.0 / b;
			arr[i].v = a; arr[i].c = b;
		}
		ans = 0;
		sort(arr, arr + n, cmp);
		for(i = 0; i < n; ++i){
			if(m >= arr[i].c){
				m -= arr[i].c; ans += arr[i].v;
			}else{
				ans += m * arr[i].val; break;
			}
		}
		printf("%.3lf\n", ans);
	}
	return 0;
}

HDU1009 FatMouse' Trade 【贪心】

时间: 2024-09-30 07:31:06

HDU1009 FatMouse' Trade 【贪心】的相关文章

HDU1009 FatMouse&#39; Trade

题目大意: 一直老鼠想吃咖啡豆,这些咖啡豆由一只猫来看守,它需要用它有的猫粮来换. 读入两个整数m,n分别代表老鼠有的猫粮的总数,和放着咖啡豆的房间数.接下来n行,每行两个数j[i],f[i],分别代表这个房间中的总的咖啡豆的数量和需要的猫粮数,求出这时老鼠可获得的最大的猫粮数,知道接收到-1,-1结束处理. 解题思路: 典型的贪心问题中的分数背包问题.由于可以取得房间中的咖啡豆的任意部分,所以转化为分数背包为题,求出每个房间的咖啡豆的单价,我是求出了单位重量的猫粮可以换取的咖啡豆,这样我觉得可

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

FatMouse&#39; Trade (贪心)

1 #include <iostream> 2 #include <stdio.h> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 7 8 using namespace std; 9 10 struct node 11 { 12 double J; // JavaBean 13 double F; // Food 14 double rate; // 性价比 1

HDU1009:FatMouse&#39; Trade(初探贪心,wait)

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 Jii pounds of JavaBeans and requires Fii pounds of cat food. FatMous

贪心/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

FatMouse&#39; Trade hdu1009

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1009 FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 66570    Accepted Submission(s): 22635 Problem Description FatMouse prepared M

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

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

ZOJ 2109 FatMouse&#39; Trade (背包 dp + 贪心)

链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1109 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

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

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