FatMouse' Trade


Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 44470    Accepted Submission(s): 14872

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

 

Author

CHEN, Yue

 

Source

ZJCPC2004

题目大意:有N个房间,每一个房间存有FatMouse喜欢吃的食物。可是每一个房间

的食物都须要用对应的猫粮去换。

FatMouse 有M磅的猫粮,为它最多能换到多

少的食物。

思路:贪心方法。用结构体存每间房间的食物量和所需猫粮量。

按食物的单位价格(

即食物/猫粮的大小)进行排列,每次选单位价格最小的购买,知道M磅猫粮用完

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
struct warehouse
{
double j;
double f;
}a[1100];
bool cmp(warehouse a,warehouse b)
{
return a.f/a.g < b.f/b.g;
}
int main()
{
int N;
double M;
while(~scanf("%lf%d",&M,&N)&& (M!=-1||N!=-1))
{
memset(a,0,sizeof(a));
for(int i = 0; i < N; i++)
{
scanf("%lf%lf",&a[i].j,&a[i].f);
}
sort(a,a+N,cmp);
double sum = 0;
for(int i = 0; i < N; i++)
{
if(M <= 0.000001)
break;
if(M >= a[i].f)
{
sum += a[i].j;
M -= a[i].f;
}
else
{
sum += M*a[i].j/a[i].f;
M = 0;
}
}
printf("%.3lf\n",sum);
}
return 0;
}

HDU1009_FatMouse&#39; Trade【贪心】【水题】的更多相关文章

  1. LightOJ 1166 Old Sorting 置换群 或 贪心 水题

    LINK 题意:给出1~n数字的排列,求变为递增有序的最小交换次数 思路:水题.数据给的很小怎么搞都可以.由于坐标和数字都是1~n,所以我使用置换群求循环节个数和长度的方法. /** @Date : ...

  2. DP+贪心水题合集_C++

    本文含有原创题,涉及版权利益问题,严禁转载,违者追究法律责任 本次是最后一篇免费的考试题解,以后的考试题目以及题解将会以付费的方式阅读,题目质量可以拿本次作为参考 本来半个月前就已经搞得差不多了,然后 ...

  3. 【COGS】714. USACO 1.3.2混合牛奶(贪心+水题)

    http://cojs.tk/cogs/problem/problem.php?pid=714 在hzwer的刷题记录上,默默地先跳过2题T_T...求凸包和期望的..T_T那是个啥..得好好学习 看 ...

  4. Gym 101873K - You Are Fired - [贪心水题]

    题目链接:http://codeforces.com/gym/101873/problem/K 题意: 现在给出 $n(1 \le n \le 1e4)$ 个员工,最多可以裁员 $k$ 人,名字为 $ ...

  5. HDU 4334——Trouble——————【贪心&水题】

    Trouble Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  6. 2111 ACM 贪心 水题

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=2111 题意:知道背包容量和物品单价.体积.问能买到的最大价值? 注意:单价指的是单位体积的价格 思路:先把 ...

  7. Ride to Office(贪心水题)

    [题目链接] http://noi.openjudge.cn/ch0406/2404/ [算法] 一开始zz了,先按时间排序然后如果速度超过当前男主速度,且在男主到达目的地前超过男主则最终男主和这个人 ...

  8. hdu 1051:Wooden Sticks(水题,贪心)

    Wooden Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  9. 【UOJ#82】【UR #7】水题生成器(贪心)

    [UOJ#82][UR #7]水题生成器(贪心) 题面 UOJ 题解 把\(n!\)的所有约数搜出来,这个个数不会很多. 然后从大往小能选则选就好了. #include<iostream> ...

随机推荐

  1. 【bzoj1690】[Usaco2007 Dec]奶牛的旅行 分数规划+Spfa

    题目描述 作为对奶牛们辛勤工作的回报,Farmer John决定带她们去附近的大城市玩一天.旅行的前夜,奶牛们在兴奋地讨论如何最好地享受这难得的闲暇. 很幸运地,奶牛们找到了一张详细的城市地图,上面标 ...

  2. IDA 操作记录

    IDA 操作记录 1.加载文件 windows 下,用ida加载文件后,会在该文件同目录下生成几个文件,含义如下: .id0 : 二叉树数据库 .id1:    文件包含描述每个程序字节的标记 .na ...

  3. HDU——5667Sequence(矩阵快速幂+费马小定理应用)

    Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total S ...

  4. 刷题总结——探险(ssoj)

    题目: 国家探险队长 Jack 意外弄到了一份秦始皇的藏宝图,于是,探险队一行人便踏上寻宝之旅,去寻找传说中的宝藏. 藏宝点分布在森林的各处,每个点有一个值,表示藏宝的价值.它们之间由一些小路相连,小 ...

  5. 刷题总结——Cut the Sequence(POJ 3017 dp+单调队列+set)

    题目: Description Given an integer sequence { an } of length N, you are to cut the sequence into sever ...

  6. 【Eclpise】Eclipse中Tomcat启动失败或者是重启失败

    经常在Eclipse中遇到这样的问题,tomcat重启之后失败,而且也停止不了.最好的解决办法就是用DOS命令杀死进程. 比如下面这种情况: 1.查看进程ID  用windows的netstat查看信 ...

  7. Mac 下安装Ant

    转自:http://blog.csdn.net/crazybigfish/article/details/18215439 如果你不知道什么是ant,请不要浪费你的时间继续读下去了.或者你对ant是什 ...

  8. Memcached简单介绍

    Memcached简单介绍 简介:Memcached是一个自由开源的,高性能,分布式内存对象缓存系统.================================================= ...

  9. fetch格式

    fetch('url'+参数a, { method: "GET", body: json } .then(res => response.json()) .then(cons ...

  10. 理解webpack中的devTool的配置项

    2.1. eval  eval 会将每一个module模块,执行eval,执行后不会生成sourcemap文件,仅仅是在每一个模块后,增加sourceURL来关联模块处理前后对应的关系.在webpac ...