(hdu step 1.3.1)FatMouse' Trade(在收入需要一定的付出的情况下求最大收入)

题目:

FatMouse‘ Trade

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5092 Accepted Submission(s): 1530
 

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


Recommend

JGShining

题目大意:

老鼠准备了M磅猫食,准备拿这些猫食跟猫交换自己喜欢的食物。有N个房间,每个房间里面都有食物。你可以得到J[i]但你需要付出F[i]的猫食。要你计算你有M磅猫食可以获得最多食物的重量。而且这里可以不必每一组都全换,可以按比例换。。。例如最后你只剩5块钱猫食。但是目前的一个选择是话10块猫食就能换取6个粮食。那么这时候你用5块猫食就能换取3个粮食

题目分析:

这是一道简单的贪心题。对于这种获得收入的同时需要付出一些的情况下,计算最大收入。这种题一般是根据

收入和付出的比例来排一下序。然后根据这个比例从高到低进行选择

代码如下:

/*
 * a.cpp
 *
 *  Created on: 2015年1月28日
 *      Author: Administrator
 */

#include <iostream>
#include <cstdio>
#include <algorithm>

using namespace std;

const int maxn = 10005;

struct W {
	double get;//收入
	double pay;//付出的猫食
	double ave;//收入/付出比
} w[maxn];

bool cmp(W a, W b) {
	if (a.ave > b.ave) {
		return true;
	}

	return false;
}

int main() {
	int n;
	double m;
	while (scanf("%lf%d", &m, &n), n != -1 && m != -1) {
		int i;
		for (i = 0; i < n; ++i) {
			scanf("%lf %lf", &w[i].get, &w[i].pay);
			w[i].ave = w[i].get / w[i].pay;
		}

		sort(w, w + n, cmp);//贪心,对w按照get/pay进行降序排序

		double sum = 0;
		i = 0;
//		while(m >= 0){//不知道为什么这种写法就是不行.
//			if (m >= w[i].pay) {
//							sum = sum + w[i].get;
//							m = m - w[i].pay;
//						} else {
//							sum = sum + w[i].ave * m;
//							break;
//						}
//			i++;
//		}

		for (i = 0; i <= n - 1; i++) {
			if (m >= w[i].pay) {//如果当前剩余的猫食还足够的话
				sum = sum + w[i].get;//那就把那个房间的粮食全部买下
				m = m - w[i].pay;//并且手上见去相应的猫食
			} else {//如果现在手上的猫食已经不够
				sum = sum + w[i].ave * m;//那么就按比例拿去一定的猫食
				break;
			}
		}

		printf("%.3lf\n", sum);
	}

	return 0;
}

(hdu step 1.3.1)FatMouse' Trade(在收入需要一定的付出的情况下求最大收入)

时间: 2024-10-10 13:14:11

(hdu step 1.3.1)FatMouse' Trade(在收入需要一定的付出的情况下求最大收入)的相关文章

(hdu step 4.2.7)逃离迷宫(在有转弯次数的限制的情况下,判断一个点是否能到另一个点)

题目: 逃离迷宫 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 264 Accepted Submission(s): 85   Problem Description 给定一个m × n (m行, n列)的迷宫,迷宫中有两个位置,gloria想从迷宫的一个位置走到另外一个位置,当然迷宫中有些地方是空地,gloria可以穿越,有些地方是障碍

(hdu step 3.2.4)FatMouse&#39;s Speed(在第一关键字升序的情况下,根据第二关键字来求最长下降子序列)

在写题解之前给自己打一下广告哈~..抱歉了,希望大家多多支持我在CSDN的视频课程,地址如下: http://edu.csdn.net/course/detail/209 题目: FatMouse's Speed Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 1034 Accepted Submission(s): 526   Proble

(hdu step 6.1.2)Eddy&#39;s picture(在只给出二维坐标点的情况下,求让n个点连通的最小费用)

题目: Eddy's picture Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 172 Accepted Submission(s): 126   Problem Description Eddy begins to like painting pictures recently ,he is sure of himself to be

(hdu step 6.1.7)Connect the Cities(在有的路已经修建好的情况下,求让n个点连通的最小费用)

题目: Connect the Cities Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 391 Accepted Submission(s): 139   Problem Description In 2100, since the sea level rise, most of the cities disappear. Though

(hdu step 5.1.4)Farm Irrigation(在两个节点合并有限制条件的情况下,求集合的个数)

题目: Farm Irrigation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 202 Accepted Submission(s): 104   Problem Description Benny has a spacious farm land to irrigate. The farm land is a rectangle,

(hdu step 6.1.6)Jungle Roads(在索引为字符索引的情况下,求让n个点连通的最小费用)

题目: Jungle Roads Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 126 Accepted Submission(s): 118   Problem Description The Head Elder of the tropical island of Lagrishan has a problem. A burst of

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

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