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
题意:给你m克猫粮,n个房间分别可以用猫粮以J[i]/F[i]的比例换取至多J[i]克咖啡豆,问最多能换取多少咖啡豆
分析:先排下序再贪心

#include <stdio.h>
#include <algorithm>
using namespace std;
int n,u;
double m,ans;
struct room
{
double j,f,p;
} r[];
int cmp(room a,room b)
{
return a.p>b.p;
}
int main()
{
while(scanf("%lf%d",&m,&n)&&n!=-&&m!=-)
{
for(int i=;i<n;i++)
scanf("%lf%lf",&r[i].j,&r[i].f),
r[i].p=r[i].j/r[i].f;
sort(r,r+n,cmp);
u=ans=;
while(m&&u<n)
{
if(r[u].f>m) ans+=m*r[u].p,m=;
//剩下的猫粮不够把这个房间的咖啡豆换过来,那就能换多少换多少
else ans+=r[u].j,m-=r[u].f;
//够的话,这间房子全部咖啡豆换过来
u++;
}
// while(m>=r[u].f&&u<n) //换种写法
// {
// ans+=r[u].j;
// m-=r[u].f;
// u++;
// }
// if(u!=n) ans+=m*r[u].p;
printf("%.3lf\n",ans); }
return ;
}

 

【HDU 1009】FatMouse' Trade的更多相关文章

  1. 【数位dp】【HDU 3555】【HDU 2089】数位DP入门题

    [HDU  3555]原题直通车: 代码: // 31MS 900K 909 B G++ #include<iostream> #include<cstdio> #includ ...

  2. 【HDU 5647】DZY Loves Connecting(树DP)

    pid=5647">[HDU 5647]DZY Loves Connecting(树DP) DZY Loves Connecting Time Limit: 4000/2000 MS ...

  3. -【线性基】【BZOJ 2460】【BZOJ 2115】【HDU 3949】

    [把三道我做过的线性基题目放在一起总结一下,代码都挺简单,主要就是贪心思想和异或的高斯消元] [然后把网上的讲解归纳一下] 1.线性基: 若干数的线性基是一组数a1,a2,a3...an,其中ax的最 ...

  4. 【HDU 2196】 Computer(树的直径)

    [HDU 2196] Computer(树的直径) 题链http://acm.hdu.edu.cn/showproblem.php?pid=2196 这题可以用树形DP解决,自然也可以用最直观的方法解 ...

  5. 【HDU 2196】 Computer (树形DP)

    [HDU 2196] Computer 题链http://acm.hdu.edu.cn/showproblem.php?pid=2196 刘汝佳<算法竞赛入门经典>P282页留下了这个问题 ...

  6. 【HDOJ 1009】 CRB and String

    [HDOJ 1009] CRB and String 每组两个串s t 仅仅由小写字母组成 问从s能不能变成t 改变的操作为选一个字符 在后面加上一个与所选字符不同的字符 这样的操作能够做无数次 问能 ...

  7. 【HDU 5145】 NPY and girls(组合+莫队)

    pid=5145">[HDU 5145] NPY and girls(组合+莫队) NPY and girls Time Limit: 8000/4000 MS (Java/Other ...

  8. HDU 1009:FatMouse&#39; Trade(简单贪心)

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

  9. 【hdu 1043】Eight

    [题目链接]:http://acm.hdu.edu.cn/showproblem.php?pid=1043 [题意] 会给你很多组数据; 让你输出这组数据到目标状态的具体步骤; [题解] 从12345 ...

随机推荐

  1. QC学习二:QC使用中问题点汇总

    QC 使用中问题点汇总,包括以下方面: 1.不兼容IE7,IE8的问题(服务器端设置) 2.无法在Win 7下正常下载页面(客户端设置) 3.在QC中填写中文内容后无法正常提交到数据库(客户端设置) ...

  2. java 14-2 正则表达式的案例

    1.判断功能 String类的public boolean matches(String regex) 需求: 判断手机号码是否满足要求? 分析: A:键盘录入手机号码 B:定义手机号码的规则 136 ...

  3. win10自动更新彻底关闭

    http://app.techweb.com.cn/wp/2016-10-24/2418646.shtml

  4. 搞懂function(*args,**kwargs)

    给出一个例子: def foo(*args,**kwargs): print 'args=',args print 'kwargs=',kwargs print '------------------ ...

  5. CSS3实现10种Loading效果

    昨晚用CSS3实现了几种常见的Loading效果,虽然很简单,但还是分享一下,顺便也当是做做笔记…… 第1种效果: 代码如下: <div class="loading"> ...

  6. 23Spring_JdbcTemplate来实现单表的增删改查

    第一步建表:

  7. Bitbucket免费的私有仓库

    1.官网 https://bitbucket.org/ 2.介绍 知乎:http://www.zhihu.com/question/20053312 建议同时用Bitbucket和Github,理由如 ...

  8. C# 【无法修改XX返回值,因为它不是变量】

    using UnityEngine; using System.Collections; using System.Xml.Linq; using UnityEditor; using System; ...

  9. C# 版本的 计时器类:精确到微秒 秒后保留一位小数 支持年月日时分秒带单位的输出

    class TimeCount { // 临时变量,存放当前类能表示的最大年份值 ; /// <summary> /// 获取毫秒能表示的最大年份数 /// </summary> ...

  10. WCF4.0 –- RESTful WCF Services (1) (入门)

    WCF 很好的支持了 REST 的开发, 而 RESTful 的服务通常是架构层面上的考虑. 因为它天生就具有很好的跨平台跨语言的集成能力,几乎所有的语言和网络平台都支持 HTTP 请求,无需去实现复 ...