HDU 1009 FatMouse' Trade(经典贪心)

FatMouse' Trade

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


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
 

Recommend
JGShining
先讲讲题目意思。讲的是老鼠为了到仓库里里偷吃仓粮(因为有猫守着)为猫准备了M镑猫食,仓库里有N个房间,每个房间里有一只猫,但是如果老鼠想吃每i-th个房间里的f份量仓粮的话,要给猫j份量的猫食,但是老鼠可以不吃完,按照仓粮乘以A%比例吃。。。问最后老鼠拿着M份猫粮最多能吃到多少仓粮. 
看到题目当时就懵了,A%是啥意思。。以为是输入值(但是也没有输入)一直未懂   
解题思路:    A%即是第I-TH房间老鼠能吃得到的仓粮和要给猫的猫粮比值即是。。=F/J   对所有房间的比值进行从大到排序即可  然后从大依次加上每个房间的能吃到的仓粮直至到N个房间并上猫食没有了即可,在这里处里如果剩于的M镑猫食少于当前房间需要的猫食,所以这里就只能取到M*(F/J)镑的猫粮了。。。。。。。
下面贴上自己的代码(为了熟悉结构体排序,采用多种排序方式,都AC过)
#include<iostream>
#include<algorithm>
#include<iomanip>
using namespace std;
struct mouse
{
	int f;
	int j;
	double v;
}cat[1000];
//用于qsort比较函数
int  cmp(const void *a,const void *b)
{
	struct mouse *aa=(mouse*)a;
	struct mouse *bb=(mouse*)b;
   if(aa->v<bb->v)//为降序的话要改为小于
	   return 1;
   else return -1;
}
//用于STL中的比较函数 >表示从大到小,<表示从小到大
bool cmp1(struct mouse a,struct mouse b)
{
   return a.v>b.v;
}

int main()
{
	int m,n;
	while(cin>>m>>n)
	{
        if(m==-1&&n==-1)
			break;
		//输入仓粮和猫粮
		for(int i=0;i<n;i++)
		{
			cin>>cat[i].f>>cat[i].j;
			cat[i].v=(double)cat[i].f/cat[i].j;
		}
		//整个冒泡排序试试
	  /* 
	   for(i=1;i<=n;i++)
		{
			for(int j=0;j<n-i;j++)
			{
				if(cat[j].v<cat[j+1].v)
				{
					double t=cat[j].v;
					  cat[j].v=cat[j+1].v;
					  cat[j+1].v=t;

					int k=cat[j].f;
					  cat[j].f=cat[j+1].f;
					  cat[j+1].f=k;

					  k=cat[j].j;
					  cat[j].j=cat[j+1].j;
					  cat[j+1].j=k;
				}
			}
		}
		*/
        //整个qsort试试
		/*
		 qsort(cat,n,sizeof(cat[0]),cmp);
		*/
		//整个STL里面的QOSORT试试
		sort(cat,cat+n,cmp1);

		//MOUSE最多能吃的仓粮
		for(double sum=i=0;m&&i<n;i++)
		{
			if(cat[i].j<=m)
			{ 
               sum+=cat[i].f;
			   m-=cat[i].j;
			}
			else
			{
                sum+=m*cat[i].v;
				m=0;
			}
		}
		//输出MOUSE 最多能吃的仓粮
		cout<<fixed<<setprecision(3)<<sum<<endl;

	}
	return 0;
}

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值