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. 【bzoj3170】[Tjoi 2013]松鼠聚会 旋转坐标系

    题目描述 有N个小松鼠,它们的家用一个点x,y表示,两个点的距离定义为:点(x,y)和它周围的8个点即上下左右四个点和对角的四个点,距离为1.现在N个松鼠要走到一个松鼠家去,求走过的最短距离. 输入 ...

  2. 【Luogu】P3211XOR和路径(高斯消元)

    题目链接 唉我个ZZ…… 首先考虑到异或是可以每一位分开算的 好的以后再碰见位运算题我一定先往按位开车上想 然后设f[i]为从i点出发到终点是1的概率 高斯消元解方程组即可. #include< ...

  3. php错误报告

    ; This directive controls whether or not and where PHP will output errors, ; notices and warnings to ...

  4. QBXT 二月五号整理

    给你一列数, 询问和最大的子串. N<=10^6 // N <=10^6 #include<cstdio> #include<iostream> using nam ...

  5. mongodb window安装学习

    https://blog.csdn.net/u011692780/article/details/81223525 教程:http://www.runoob.com/mongodb/mongodb-t ...

  6. linux内核中打印栈回溯信息 - dump_stack()函数分析【转】

    转自:http://blog.csdn.net/jasonchen_gbd/article/details/45585133 版权声明:本文为博主原创文章,转载请附上原博链接.   目录(?)[-] ...

  7. 标准C程序设计七---56

    Linux应用             编程深入            语言编程 标准C程序设计七---经典C11程序设计    以下内容为阅读:    <标准C程序设计>(第7版) 作者 ...

  8. 26深入理解C指针之---不规则数组与指针

    一.不规则数组:每一行的列数不相等 1.复合字面量: 1).复合字面量是一种C构造 2).外形和数组声明差不多,写法与类型转换一样,(int[3]){10, 20, 30,} 3).将多个复合字面量可 ...

  9. BZOJ——1601: [Usaco2008 Oct]灌水

    http://www.lydsy.com/JudgeOnline/problem.php?id=1601 Time Limit: 5 Sec  Memory Limit: 162 MBSubmit:  ...

  10. HDU3625 Examining the Rooms

    @(HDU)[Stirling數] Problem Description A murder happened in the hotel. As the best detective in the t ...