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) ...
随机推荐
- 「日常训练&知识学习」树的分块(王室联邦,HYSBZ-1086)
题意与分析 这题的题意就是树分块,更具体的看题目(中文题). 学习这一题是为了树的分块,为树上莫队做铺垫. 参考1:https://blog.csdn.net/LJH_KOQI/article/det ...
- 阿里云服务器Linux系统安装配置ElasticSearch搜索引擎
近几篇ElasticSearch系列: 1.阿里云服务器Linux系统安装配置ElasticSearch搜索引擎 2.Linux系统中ElasticSearch搜索引擎安装配置Head插件 3.Ela ...
- sql注入记录------类型转换错误---convert()函数,一句话图片马制作
sql注入在联合查询是出现一下错误查不到数据 Illegal mix of collations for operation 'UNION' 用convert() 转换编码为utf8 或者big5 就 ...
- git服务器搭建及eclipse使用git
一.搭建git服务器 1.yum install git 2.新建用户linux用户git,管理git服务 useradd git passwd git 3.初始化git仓库 git init --b ...
- 中文乱码的处理—@北河的ppt
- CSP201609-1:最大波动
引言:CSP(http://www.cspro.org/lead/application/ccf/login.jsp)是由中国计算机学会(CCF)发起的"计算机职业资格认证"考试, ...
- 关于TensorFlow的GPU设置
摘自:https://blog.csdn.net/byron123456sfsfsfa/article/details/79811286 1. 在使用GPU版的TensorFlow跑程序的时候,如果 ...
- vue移动音乐app开发学习(一):环境搭建
本系列文章是为了记录学习中的知识点,便于后期自己观看.如果有需要的同学请登录慕课网,找到Vue 2.0 高级实战-开发移动端音乐WebApp进行观看,传送门. 一:使用vue-cli脚手架搭建: 1: ...
- $http.get(...).success is not a function 错误解决
$http.get(...).success is not a function 错误解决 1.6 新版本的AngularJs中用then和catch 代替了success和error,用PRomis ...
- object-oriented第二次作业(1)
1001.A+B F Format(20) 我的代码 题目看完,感觉挺简单的,就直接开始写代码了. 我把加起来后的数字的每位数用数组存起来,特判一下0和负数的情况,然后再一位位输出,遇到该输逗号的时候 ...