HDOJ.1009 FatMouse' Trade (贪心)
FatMouse’ Trade
题意分析
每组数据,给出有的猫粮m与房间数n,接着有n行,分别是这个房间存放的食物和所需要的猫粮。求这组数据能保证的最大的食物是多少? (可以不完全保证这个房间的食物,及食物和猫粮可以同时乘a%)
经典的贪心策略。
先保证性价比最高的房间(花较少的猫粮可以保证最多的粮食),每组数据计算一个比率rate = 房间存放的粮食/所需要的猫粮,按照rate对其进行降序排列,优先满足上方的房间即可。
代码纵览
/*
Title:HDOJ.1009
Author:pengwill
Date:2016-11-22
*/
#include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std;
struct room{
double food;
double need;
double rate;
}item[1001];
double cmp(room a,room b)
{
return a.rate > b.rate;
}
int main()
{
int n;
double m;
while(scanf("%lf %d",&m,&n)!= EOF && (m!= -1 && n!= -1)){
int i;
for(i = 0;i<n;++i){
scanf("%lf %lf", &item[i].food,&item[i].need);
item[i].rate = item[i].food / item[i].need;
}
sort(item,item+n,cmp);
double ret = 0;
for(i = 0;i < n; ++i){
if(m>item[i].need){
ret+=item[i].food;
m-=item[i].need;
}else{
double percentage = m/item[i].need;
ret+=item[i].food * percentage;
break;
}
}
printf("%.3f\n",ret);
}
return 0;
}
HDOJ.1009 FatMouse' Trade (贪心)的更多相关文章
- HDU 1009 FatMouse' Trade(贪心)
FatMouse' Trade Problem Description FatMouse prepared M pounds of cat food, ready to trade with the ...
- Hdoj 1009.FatMouse' Trade 题解
Problem Description FatMouse prepared M pounds of cat food, ready to trade with the cats guarding th ...
- 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 ...
- HDU 1009 FatMouse' Trade【贪心】
解题思路:一只老鼠共有m的猫粮,给出n个房间,每一间房间可以用f[i]的猫粮换取w[i]的豆,问老鼠最多能够获得豆的数量 sum 即每一间房间的豆的单价为v[i]=f[i]/w[i],要想买到最多的豆 ...
- 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) ...
随机推荐
- 推荐:一个适合于Python新手的入门练手项目
随着人工智能的兴起,国内掀起了一股Python学习热潮,入门级编程语言,大多选择Python,有经验的程序员,也开始学习Python,正所谓是人生苦短,我用Python 有个Python入门练手项目, ...
- Spring Cloud(五):Hystrix 监控面板【Finchley 版】
Spring Cloud(五):Hystrix 监控面板[Finchley 版] 发表于 2018-04-16 | 更新于 2018-05-10 | 在上一篇 Hystrix 的介绍中,我们提到 ...
- 【转载】appium 操作汇总
'''.appium api第二弹 锋利的python,这是初稿,2015/1/5 如有错误的地方,请同学们进行留言,我会及时予以修改,尽量整合一份ok的api 作者:Mads Spiral QQ:7 ...
- 关于excle导数据的一些代码笔记
package com.bonc.util; import java.io.File; import java.io.FileInputStream; import java.io.FileOutpu ...
- 【C#】arcface人脸识别使用问题分析
arcface上线了新版 正好有空 赶紧下载体验了一番 凡是过程中也遇到一些问题 1.初始化 [DllImport("libarcsoft_face_engine.dll", En ...
- 6.azkban的监控
azkaban自带的监控flow自带的邮件功能SLA总结写程序监控job情况监控azkaban的元数据库使用azkaban API监控总结 azkaban自带的监控 azkban目前仅仅支持邮件监控, ...
- Python中的print
Python 3.X的print 在Python 3.X中,print是一个内置函数,完整的声明形式如下: print([object, ...][, sep=' '][, end='\n'][, f ...
- 自测之Lesson12:信号量
题目:创建一个包含5个信号量的信号集. 完成代码: #include <stdio.h> #include <sys/ipc.h> #include <sys/sem.h ...
- 接口文档管理工具-Postman、Swagger、RAP(转载)
接口文档管理工具-Postman.Swagger.RAP 转自:http://www.51testing.com/html/10/n-3715910.html 在项目开发测试中,接口文档是贯穿始终的. ...
- 什么是BCL
原文: 原文:https://www.cnblogs.com/1996V/p/9037603.html 什么是BCL 当你通过VS创建一个项目后,你这个项目就已经引用好了通过.NET下的语言编写好的一 ...