FatMouse' Trade

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
 
Answer
老鼠手上有n的猫粮,猫手上有m份的鼠粮(其实是绿豆...),这m份绿豆分别给出绿豆的量和价格,求老鼠用n的猫粮最多换到多少绿豆。
先根据单价(每份绿豆)排序,然后从单价小的开始贪心往后,最后输出保留三位小数。
 
#include <cstdio>
#include <iostream>
#include <string>
#include <sstream>
#include <cstring>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#include <map>
#define PI acos(-1.0)
#define ms(a) memset(a,0,sizeof(a))
#define msp memset(mp,0,sizeof(mp))
#define msv memset(vis,0,sizeof(vis))
using namespace std;
//#define LOCAL
struct Node
{
int num;//绿豆数量
int need;//需要猫粮
}t;
int cmp(Node n1,Node n2)//单价从小到大
{
return 1.0*n1.need/n1.num<1.0*n2.need/n2.num;
}
int main()
{
#ifdef LOCAL
freopen("in.txt", "r", stdin);
#endif // LOCAL
ios::sync_with_stdio(false);
int n,m;//猫粮,组数
vector<Node> v;
while(cin>>n>>m&&n!=-)
{
v.clear();
double ans=;
for(int i=;i<m;i++)
cin>>t.num>>t.need,v.push_back(t);
sort(v.begin(),v.end(),cmp);
for(int i=,siz=v.size();i<siz;i++)
{
t=v[i];
if(t.need<=n)ans+=t.num,n-=t.need;
else {ans+=n/(1.0*t.need/t.num);break;}
}
printf("%.3lf\n",ans);
}
return ;
}

HDU 1009 FatMouse' Trade(贪心)的更多相关文章

  1. HDU 1009 FatMouse' Trade(简单贪心 物品可分割的背包问题)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1009 FatMouse' Trade Time Limit: 2000/1000 MS (Java/O ...

  2. HDU 1009 FatMouse' Trade(简单贪心)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1009 FatMouse' Trade Time Limit: 2000/1000 MS (Java/O ...

  3. hdu 1009:FatMouse' Trade(贪心)

    FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  4. HDU 1009 FatMouse' Trade【贪心】

    解题思路:一只老鼠共有m的猫粮,给出n个房间,每一间房间可以用f[i]的猫粮换取w[i]的豆,问老鼠最多能够获得豆的数量 sum 即每一间房间的豆的单价为v[i]=f[i]/w[i],要想买到最多的豆 ...

  5. HDOJ.1009 FatMouse' Trade (贪心)

    FatMouse' Trade 点我挑战题目 题意分析 每组数据,给出有的猫粮m与房间数n,接着有n行,分别是这个房间存放的食物和所需要的猫粮.求这组数据能保证的最大的食物是多少? (可以不完全保证这 ...

  6. Hdu 1009 FatMouse' Trade

    FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  7. Hdu 1009 FatMouse' Trade 2016-05-05 23:02 86人阅读 评论(0) 收藏

    FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tot ...

  8. 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) ...

  9. [题解]hdu 1009 FatMouse' Trade(贪心基础题)

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

随机推荐

  1. Latex 使用小技巧

    Latex引用多篇参考文献 连续引用参考文献时中间中破折号连起来:[1,2,3,4]—>[1-4] 这是只需要在文档开始加入下面语句命令: \usepackage[numbers,sort&am ...

  2. linux shell脚本学习xargs命令使用详解

    作用是将参数列表转换成小块分段传递给其他命令,以避免参数列表过长的问题 xargs是给命令传递参数的一个过滤器,也是组合多个命令的一个工具.它把一个数据流分割为一些足够小的块,以方便过滤器和命令进行处 ...

  3. JDBC连接sql server数据库操作

    1.首先,先创建一个连接数据库的工具类: package gu.db.util; import java.sql.Connection; import java.sql.DriverManager; ...

  4. 我是如何给discuz模板做语法高亮的

    本人一直做ASP.NET开发,近期接到任务要用Discuz开发一个社区,第一次接触PHP,PHP灵活的语法,天生的模块化,各种语法糖深深的震惊了我,我从内心深处感受到了PHP是最牛逼的语言!!! 好了 ...

  5. VS2012及以上版本 程序打包部署详解

    引用:  http://blog.csdn.net/zhang_xinxiu/article/details/9099757 程序编写测试完成后接下来我们要做的是打包部署程序,但VS2012让人心痛的 ...

  6. RedHat9.0下载地址

    RedHat下载:http://archive.download.redhat.com/pub/redhat/linux/9/en/iso/i386/

  7. wpf 遍历listview 时 传入指定类型 得到指定类型控件info

    private ChildType FindVisualChild<ChildType>(DependencyObject obj) where ChildType : Dependenc ...

  8. reinterpret_cast应用

    reinterpret_cast 的一个实际用途是在哈希函数中,即,通过让两个不同的值几乎不以相同的索引结尾的方式将值映射到索引. #include <iostream> using na ...

  9. Quartz+log4net实现控制台程序定时运行,并且记录日志

    c# 控制台程序定时运行,并记录日志. 组件Quartz + log4net Quartz定时运行程序,log4net记录日志. 首先建立控制台程序"QuartzTest" 安装Q ...

  10. C#中使用like和in参数传值

    like 参数string strSql = "select * from Person.Address where City like '%'+ @add + '%'";SqlP ...