FatMouse' Trade -HZNU寒假集训
FatMouse' Trade
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
题解:我建立了一个结构体,然后将食物价值用r表示,然后把结构体排序,依次从最大的开始递减,可以保证价值最大。
#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;
struct food
{
double j;
double f;
double r;
}fo[];
bool compare(food a,food b)
{
return a.r>b.r;
}
int main()
{
int m,n;
while(scanf("%d %d",&m,&n),m!=-,n!=-)
{
for(int i=;i<n;i++)
{
scanf("%lf %lf",&fo[i].j,&fo[i].f);
fo[i].r=fo[i].j/fo[i].f;
}
sort(fo,fo+n,compare);
double ans=;
for(int i=;i<n;i++)
{
if(m>=fo[i].f)
{
ans+=fo[i].j;
m-=fo[i].f;
}
else
{
ans+=m*fo[i].r;
break;
}
}
printf("%.3lf\n",ans);
}
return ;
}
FatMouse' Trade -HZNU寒假集训的更多相关文章
- GlitchBot -HZNU寒假集训
One of our delivery robots is malfunctioning! The job of the robot is simple; it should follow a lis ...
- Wooden Sticks -HZNU寒假集训
Wooden Sticks There is a pile of n wooden sticks. The length and weight of each stick are known in a ...
- 今年暑假不AC - HZNU寒假集训
今年暑假不AC "今年暑假不AC?" "是的." "那你干什么呢?" "看世界杯呀,笨蛋!" "@#$%^&a ...
- 畅通工程-HZNU寒假集训
畅通工程 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇.省政府"畅通工程"的目标是使全省任何两个城镇间都可以实现交通(但不一定有直接的道路相连,只 ...
- 并查集模板题(The Suspects )HZNU寒假集训
The Suspects Time Limit: 1000MS Memory Limit: 20000KTotal Submissions: 36817 Accepted: 17860 Descrip ...
- Hdu 1009 FatMouse' Trade
FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- hdu 1009:FatMouse' Trade(贪心)
FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- Hdu 1009 FatMouse' Trade 分类: Translation Mode 2014-08-04 14:07 74人阅读 评论(0) 收藏
FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- 1009 FatMouse' Trade
FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
随机推荐
- 【Unity Shaders】Reflecting Your World —— Unity3D中的法线贴图和反射
本系列主要参考<Unity Shaders and Effects Cookbook>一书(感谢原书作者),同时会加上一点个人理解或拓展. 这里是本书所有的插图.这里是本书所需的代码和资源 ...
- python安装json的方法;以及三种json库的区别
python中的json解释库有好几个,不同版本使用方法不同. 常用有 json-py 与smiplejson 两个包 其中,json-py 包含json.py外,还有一个minjson,两者用法上有 ...
- (NO.00003)iOS游戏简单的机器人投射游戏成形记(八)
回到Xcode,新建ArmStatic类,继承于CCSprite.同样在初始化方法中打开用户交互. 下面添加触摸回调方法,touchBegan以及touchMoved方法和Arm基本相同,主要不同处在 ...
- 网站开发进阶(二十九)HTML特殊转义字符
HTML特殊转义字符 参考文献 http://tool.oschina.net/commons?type=2 美文美图
- OC语言(六)
四十六.block类型 用来封装代码,可以在任何时候执行. 与函数的区别:可以在运行时动态产生. block的标志:^ 有形参.有返回值 int (^sumblock)(int,int) = ^(in ...
- Leetcode_80_Remove Duplicates from Sorted Array II
本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/43835055 Follow up for "Re ...
- Unity3D学习笔记(一)GUI控件的调用
GUI控件:1.在Start中初始化,在OnGUI中调整.2.公有变量才会出现在Inspector面板.3.GUI控件的初始化和处理在OnGUI内完成.4.JavaScript的中文为UTF-8编码可 ...
- 《java入门第一季》之面向对象(static关键字)
/* static的特点:(它可以修饰成员变量,还可以修饰成员方法) A:随着类的加载而加载 回想main方法. B:优先于对象存在 C:被类的所有对象共享 举例:班级的学生应该共用同一个班级编号. ...
- rabbitMQ之AMQP协议
1.什么是AMQP协议 即高级消息队列协议,规范客户端与消息中间件服务器之间的通信,并能相互操作. 2.AMQP协议的作用 降低应用程序之间的耦合度,这样不同应用之间的集成的难度将变得更小,并开发出更 ...
- LM**项目开发感悟
LM**项目开发感悟 经过一个多月的项目开发,自己主要负责服务端业务逻辑的实现.服务端采用纯servlet完成,自己是在已有的项目架构上进行编程,对于所使用的架构,自己还没有认真的研究过,但明白其用到 ...