YT02-简单数学课后题-1001 FatMouse' Trade-(5.31日-烟台大学ACM预备队解题报告)
2015-06-01 15:07
393 查看
FatMouse' Trade
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 126 Accepted Submission(s) : 30
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
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. Allintegers 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计145-李晓凯
大体意思:
•题目描述:
•有N个房间,你有M镑的猫粮,在第i个房间你最多可以花费F[i]的猫粮来交换J[i]的豆子,交换可以按比例来,不一定全部交换,能在多个房间交换。求M镑最多能交换多少豆子。
•
•输入描述:
•输入数据有多组,每行有两个整数M和N,接下有N行,每行有两个整数J[i]和F[i]。当M=-1和N=-1时结束。所有整数不会大于1000。
•
•输出描述:
•输出能还得最大最大豆子数,保留三位小数。
解析:
•这是一个简单的贪心算法,部分背包问题,用结构体数组,以J/F比值升序排序即可。然后判断每个房间能不能全拿,如果能就M=M-F[i],sum+=J[i],不能就按照比例来取,在不能的时候Mouse用尽M的时候,这时候break就ok了,注意百分比计算的时候的强制转换。
• 比如:5个猫食,3个房间,第1个房间2个猫食换7个JavaBean,第2个房间3个猫食换4个JavaBean,第3个房间2个猫食换5个JavaBean;最大量的JavaBean是7+5+1*(4/3)=13.333。
•简单的贪心策略:每次以最小量的猫食换最大量的JavaBean,即每次取(J[i]/F[i])最大的J[i]将使换得的JavaBean量最大。
#include <iostream> #include <cstdio> #include <algorithm> #include <cmath> using namespace std; struct Fun{ int CatFood,JavaBean; double ratio; }; Fun t[10000],temp; bool c(Fun a,Fun b){ return a.ratio>b.ratio; } int main(){ int m,n,i,j; while(scanf("%d%d",&m,&n)&&(m!=-1||n!=-1)){ i=0; while(i<n){ cin>>t[i].JavaBean>>t[i].CatFood; t[i].ratio=(double)t[i].JavaBean/t[i++].CatFood; } sort(t,t+n,c); double sum=0; for(i=0;i<n&&m!=0;i++){ if(m>=t[i].CatFood){ sum+=t[i].JavaBean; m-=t[i].CatFood; } else { sum+=m*((double)t[i].JavaBean/t[i++].CatFood); break; } } printf("%.3f\n",sum); } return 0; }
相关文章推荐
- Android UI 中常用技巧总结
- cJSON: 一个用c写的一个简单好用的JSON解析器
- 《Entity Framework 6 Recipes》中文翻译系列 (35) ------ 第六章 继承与建模高级应用之TPH继承映射中使用复合条件
- MYSQL错误: NO.1130 ERROR的解决
- 简单的php验证码生成
- makefile 基础
- Java中final关键字总结
- 第十三周项目一动物这样叫(3)
- 第十三周阅读程序3
- Android LoaderCallbacks
- 第12周项目1-2
- YT02-简单数学课堂题-1006 人见人爱A^B -(5.31日-烟台大学ACM预备队解题报告)
- Vijava 学习笔记CustomizationWinOptions 和CustomizationSysprepRebootOption
- Html-Css-div透明层剧中
- DB2 autoincretment(抄袭)
- DB2数据类型(抄袭)
- Session, Cookie, Web.config中的cookieless总结
- java的StringBuffer类使用
- 银行卡识别
- erlang crypto block_encrypt