2134 Problem E FatMouse's Trade

問題 E: FatMouse's Trade

時間限制: 1 Sec  內存限制: 32 MB

題目描述

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. 

輸入

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.

輸出

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.

樣例輸入

4 2
4 7
1 3
5 5
4 8
3 8
1 2
2 5
2 4
-1 -1

樣例輸出

2.286
2.500

經驗總結

題意是這樣的,肥老鼠準備了M磅貓糧,用於跟貓做交易,讓貓看守存儲他最喜愛的食物Java豆。倉庫有N個房間,第i個房間有J[i]磅Java豆,需要F[i]磅貓糧。肥老鼠不用獲得所有房間的Java豆,相反,他每支付F[i]*a%磅的貓糧也許可以獲得j[i]*a%磅的Java豆,現在請你告訴他他可以獲得最多Java豆的數量。
其實可能就是讀英語比較困難,理解題目意思,那麼這題就很好做了,每個房間的回報率不同,先根據回報率排個序(回報率=J[i]/F[i] ,每磅貓糧可以獲得多少磅Java豆),然後貪心方式進行累加,最後輸出即可~

AC代碼

#include <cstdio>
#include <algorithm>
using namespace std;
struct room
{
	double javabean,food,profitrate;
}r[1010];
bool cmp(room a,room b)
{
	return a.profitrate>b.profitrate;
}
int main()
{
	double sumfood;
	int n;
	while(~scanf("%lf %d",&sumfood,&n))
	{
		if(n==-1)   break;
		for(int i=0;i<n;i++)
		{
			scanf("%lf %lf",&r[i].javabean,&r[i].food);
			if(r[i].food!=0)
				r[i].profitrate=r[i].javabean/r[i].food;
			else
				r[i].profitrate=1000;
		}
		sort(r,r+n,cmp);
		double lastp=0;
		for(int i=0;i<n;i++)
		{
			if(sumfood>=r[i].food)
			{
				sumfood-=r[i].food;
				lastp+=r[i].javabean;
			}
			else if(sumfood>0&&sumfood<r[i].food)
			{
				lastp+=sumfood/r[i].food*r[i].javabean;
				break;
			}
		}
		printf("%.3f\n",lastp);
	}
	return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章