解题思路:一只老鼠共有m的猫粮,给出n个房间,每一间房间可以用f[i]的猫粮换取w[i]的豆,问老鼠最多能够获得豆的数量 sum

即每一间房间的豆的单价为v[i]=f[i]/w[i],要想买到最多的豆,一定是先买最便宜的,再买第二便宜的,再买第三便宜的

-----m的值为0的时候求得的sum即为最大值   所以先将v[i]从小到大排序。

 

FatMouse' Trade

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

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
#include<stdio.h>
void bubblesort(double v[],int w[],int f[],int n)
{
int i,j;
double t;
for(i=1;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
if(v[i]>v[j])
{
t=v[i];
v[i]=v[j];
v[j]=t; t=f[i];
f[i]=f[j];
f[j]=t; t=w[i];
w[i]=w[j];
w[j]=t;
}
}
}
}
int main()
{
int n,m,i,j,w[1000],f[1000];
double v[1000],sum;
while(scanf("%d %d",&m,&n)!=EOF&&(n!=-1)&&(m!=-1))
{
for(i=1;i<=n;i++)
{
scanf("%d %d",&w[i],&f[i]);
v[i]=f[i]*1.0/w[i];
}
bubblesort(v,w,f,n); sum=0;
for(i=1;i<=n&&v[i]<=m&&m>0;i++) //如果v[i]>m,则单价大于总价,不能进行交换,跳出循环
{ if(m>=f[i]) //总价能够买到该房间所有的豆
{
sum+=w[i];
m=m-f[i];
}
else
{
sum+=m*1.0/v[i]; //总价只能买到该房间部分的豆,说明买完这间房间的豆,总价也用完了
m=0;
}
}
printf("%.3lf\n",sum);
}
}

  

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

  1. HDU 1009 FatMouse' Trade(贪心)

    FatMouse' Trade Problem Description FatMouse prepared M pounds of cat food, ready to trade with the ...

  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(简单贪心)

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

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

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

  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. pc页面滚动的时候,背景图不动只是页面滚动

    代码如下,直接拷贝出去就能看效果: 用到的方法 background-attachment 属性设置背景图像是否固定或者随着页面的其余部分滚动. <!DOCTYPE html> <h ...

  2. PuTTY 命令行改进 有效解决 中文乱码

    PuTTY  是一个免费且跨平台的并支持SSH和Telnet 的客户端, 包括xterm 终端模拟器. 它由Simon Tatham 编写并维护. http://www.chiark.greenend ...

  3. Python中用绘图库绘制一条蟒蛇

    一..构思设计蟒蛇的长度颜色等 首先,我们来构思一个简单的蟒蛇.让它的颜色为黄色,形状为一条正在爬行的蟒蛇. 二..准备绘图库 Python中有一个绘图库叫turtle我们先引入它. import t ...

  4. win10卸载瑞星

    下载了一个软件,没有注意就不小心装上了瑞星这个流氓软件 百度N种办法并不能解决~ 我试过正常卸载.试过WIN自带卸载.试过重装再卸载 最后采取最傻瓜最暴力的办法 ctrl+alt+delete 打开任 ...

  5. 【BZOJ3065】带插入区间k小值

    题意: 带插入,修改的区间k小值在线查询 原序列长度<=35000,插入个数<=35000,修改个数<=70000,0<=权值<=70000 题解: Orz vfleak ...

  6. 使用maven创建springMVC时返回页面报错

    这是由于你的 Maven 编译级别是 jdk1.5 或以下,而你导入了 jdk1.6 以上的依赖包 解决办法: <build> <finalName></finalNam ...

  7. Mysql学习总结(36)——Mysql查询优化

    从事前端开发的都知道,页面显示的数据一定要及时的呈现,否则会影响用户体现.那么导致页面加载数据慢或者显示滞后的原因又是什么呢? 拿自己之前做项目经历给大家讲讲吧,之前做后台,当时的项目实时性都非常高, ...

  8. Redis,Memcache的区别和具体应用场景

    1. Memcached简介 Memcached是以LiveJurnal旗下Danga Interactive公司的Bard Fitzpatric为首开发的高性能分布式内存缓存服务器.其本质上就是一个 ...

  9. Ruby按照换行符进行分割

    Ruby按照换行符进行分割 string.split(/\n/)

  10. CF 372B Counting Rectangles is Fun [dp+数据维护]

    题意,给出一个n行m列的矩阵 里面元素是0或者1 给出q个询问 a,b,c,d 求(a,b)到(c,d)有多少个由0组成的矩形 我们定义 watermark/2/text/aHR0cDovL2Jsb2 ...