HDU 1009 FatMouse' Trade【贪心】
解题思路:一只老鼠共有m的猫粮,给出n个房间,每一间房间可以用f[i]的猫粮换取w[i]的豆,问老鼠最多能够获得豆的数量 sum
即每一间房间的豆的单价为v[i]=f[i]/w[i],要想买到最多的豆,一定是先买最便宜的,再买第二便宜的,再买第三便宜的
-----m的值为0的时候求得的sum即为最大值 所以先将v[i]从小到大排序。
FatMouse' TradeTime 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【贪心】的更多相关文章
- HDU 1009 FatMouse' Trade(贪心)
FatMouse' Trade Problem Description FatMouse prepared M pounds of cat food, ready to trade with the ...
- HDU 1009 FatMouse' Trade(简单贪心 物品可分割的背包问题)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1009 FatMouse' Trade Time Limit: 2000/1000 MS (Java/O ...
- HDU 1009 FatMouse' Trade(简单贪心)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1009 FatMouse' Trade Time Limit: 2000/1000 MS (Java/O ...
- hdu 1009:FatMouse' Trade(贪心)
FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- HDOJ.1009 FatMouse' Trade (贪心)
FatMouse' Trade 点我挑战题目 题意分析 每组数据,给出有的猫粮m与房间数n,接着有n行,分别是这个房间存放的食物和所需要的猫粮.求这组数据能保证的最大的食物是多少? (可以不完全保证这 ...
- Hdu 1009 FatMouse' Trade
FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- 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 ...
- 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) ...
- [题解]hdu 1009 FatMouse' Trade(贪心基础题)
Problem Description FatMouse prepared M pounds of cat food, ready to trade with the cats guarding th ...
随机推荐
- 如何快速在命令提示符(cmd)中打开指定的文件夹路径!
按住shift键然后 右击,如图:
- 【前端分享】jQuery.lazyload详解(转)
jQuery实现图片延迟加载,不知道是否可以节省带宽呢?有人知道吗?这究竟只是一个视觉特效还是真的能延迟加载减少服务器的请求呢? <script type="text/javascri ...
- Codeforces Round #468 (Div. 2, based on Technocup 2018 Final Round)B. World Cup
The last stage of Football World Cup is played using the play-off system. There are n teams left in ...
- 1520dc更新与NUll语句用法
update dbo.dc_出租信息set 出租类型 = 1where 出租类型='普通楼上房' update dbo.dc_出租信息set 朝向 = 5where 朝向 is NULL
- There are no packages available for installation. Sublime3解决方法
最近在学习Vue,在配置sublime3的时候,想要高亮vue的语法,下载点插件 Package Control的时候,总报 There are no packages available for ...
- 3.3、Ansible命令参数详解
0.ansible 命令参数详解: [root@localhost ~]# ansible Usage: ansible <host-pattern> [options] Options: ...
- Tensorflow原理通用
使用 TensorFlow, 你必须明白 TensorFlow: 使用图 (graph) 来表示计算任务.在被称之为 会话 (Session) 的上下文 (context) 中执行图.使用 tenso ...
- applicationContext-redis.xml
一.动态切换单机和集群 spring-redis 的配置 <!-- 连接redis单机版 --> <bean id="jedisClientPool" class ...
- 多个API接口
青云客智能聊天机器人API 为保证接口稳定,API接口正式启用 api.qingyunke.com,www.qingyunke.com 如何更换API接口地址? 答:如果您的API接口是用在微信公众号 ...
- NEFU 84
其实同POJ 1061 #include <iostream> #include <cstdio> #include <cstring> #include < ...