hdoj FatMouse' Trade 1009 (多重背包)

原创 2015年11月19日 12:16:25

FatMouse' Trade

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


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
 
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
struct zz
{
	int v;
	int w;
	double c;
}q[1010];
int cmp(zz a,zz b)
{
	return a.c>b.c;
}
int main()
{
	int n,m,i,j;
	while(scanf("%d%d",&m,&n),n!=-1&&m!=-1)
	{
		for(i=1;i<=n;i++)
		{
			scanf("%d%d",&q[i].v,&q[i].w);
			q[i].c=q[i].v*1.0/q[i].w;
		}
		sort(q+1,q+n+1,cmp);
		double sum=0;
		for(i=1;i<=n;i++)
		{
			if(q[i].w<=m)
			{
				sum+=q[i].v;
				m-=q[i].w;
			}
			else
			{
				sum+=m*q[i].c;
				break;
			}
		}
		printf("%.3lf\n",sum);
	}
	return 0;
}

相关文章推荐

zoj 2109 || hdoj1009 FatMouse' Trade(贪心)

题意: M  N J[i]  F[i] fatmouse有M克猫粮(类似吧),现在有个房间,F[i]  pounds 猫粮 可以换 J[i] pounds javabeans. (hdoj的d...
  • cqlf__
  • cqlf__
  • 2011年11月28日 07:53
  • 1463

【ACM】HDOJ 1009 FatMouse' Trade

贪心算法一个应用

ACM--猫鼠交易--贪心--HDOJ 1009--FatMouse' Trade

HDOJ题目地址:传送门 FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/3...

HDOJ 1009 FatMouse' Trade

FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)...
  • kdwycz
  • kdwycz
  • 2013年03月14日 15:17
  • 587

FatMouse' Trade<hdoj1009>

Problem Description FatMouse prepared M pounds of cat food, ready to trade with the cats guarding t...

HDOJ 1009 FatMouse' Trade 贪心算法

首先先看懂题目,然后对题目要有深入理解,知道考查的是贪心算法。 题意:老鼠要去用猫食去换java豆,每个房间有一定的豆和猫食,J[i] and F[i] ,这既是每个房间的上限,也是兑换比率。 所...

HDUOJ-1009 FatMouse' Trade(贪心)

采用贪心的思考问题方法即“做出的是在某种意义上的局部最优解”

HDU ACM 1009 FatMouse' Trade

原题描述Problem Description FatMouse prepared M pounds of cat food, ready to trade with the cats guard...

杭电1009--FatMouse' Trade

这是一道简单的贪心题,题意为Fatmouse想要尽可能多得守护自己喜欢的JavaBean,所以,i房间得用F[i]d的cat food换得守护J[i]的JavaBean,当JavaBean不足以换的所...

HDU1009 FatMouse' Trade

题目大意:         一直老鼠想吃咖啡豆,这些咖啡豆由一只猫来看守,它需要用它有的猫粮来换。        读入两个整数m,n分别代表老鼠有的猫粮的总数,和放着咖啡豆的房间数。接下来n行,每...
  • fyy607
  • fyy607
  • 2015年08月25日 11:10
  • 421
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:hdoj FatMouse' Trade 1009 (多重背包)
举报原因:
原因补充:

(最多只允许输入30个字)