【HDU - 1009 】FatMouse' Trade (贪心)

题干:

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

题目大意:

肥鼠准备了 M 磅的猫粮,准备和看管仓库的猫交易,仓库里装有他最喜爱的食物 Java 豆。

仓库有 N 个房间。第 i 间房包含了 J[i] 磅的 Java 豆,需要 F[i] 磅的猫粮。肥鼠不必为了房间中的所有 Java 豆而交易,相反,他可以支付 F[i] * a% 磅的猫粮去交换得到 J[i] * a% 磅的 Java 豆。这里,a 表示一个实数。

现在他将这项任务分配给了你:请告诉他,能够获得的 Java 豆的最大值是多少。

注意这个‘%’是百分之!!不是取模!!!傻子了吧、、、

解题报告:

  赤果果的贪心,,排序后先买最值得买的,直到钱花光了。

AC代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream> 
#define ll long long
using namespace std;
int n;
double m,ans;
struct Node {int j,f;
} node[1005];
bool cmp(Node a,Node b) {return 1.0*a.j/a.f > 1.0*b.j/b.f; 
}
int main()
{while(cin>>m>>n) {if(n == -1 && m == -1) break;for(int i = 1; i<=n; i++) {scanf("%d%d",&node[i].j,&node[i].f);}ans=0;sort(node+1,node+n+1,cmp);for(int i = 1; i<=n; i++) {if(m >= node[i].f) {m-=node[i].f;ans+=node[i].j;}else {ans += (m/node[i].f)*node[i].j;break;}}printf("%.3lf\n",ans);}return 0 ;} 

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/441443.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

lua进入压缩包_使用lua语言制作贪吃蛇游戏(love2d)(一)开发环境的搭建

本教程教大家使用lua制作一个贪吃蛇&#xff0c;游戏引擎使用love2d&#xff0c;因为它开源轻巧而且跨平台。1.开发环境搭建&#xff1a;windows系统&#xff1a;在windows系统下&#xff0c;首先我们进入官网www.love2d.org。love2d官网进入官网可以看到Download选项&#xff…

【CodeForces - 660C】Hard Process (尺取 或 二分+滑窗,前缀和预处理)

题干&#xff1a; You are given an array a with n elements. Each element of a is either 0 or 1. Lets denote the length of the longest subsegment of consecutive elements in a, consisting of only numbers one, as f(a). You can change no more than k zeroes to…

mysql rand() 子查询_MySQL ------ 子查询(十三)

查询&#xff08;query&#xff09;:任何SQL 都是查询&#xff0c;但此术语一般指select 语句子查询&#xff08;subquery&#xff09;:嵌套在查询中的查询&#xff0c;MySQL4.1 引入对子查询的支持。接下来得就比较有意思了&#xff0c;需要你对于表与表之间的关系有所了解&am…

【CodeForces - 616D 】Longest k-Good Segment (twopointer,尺取)

题干&#xff1a; The array a with n integers is given. Lets call the sequence of one or more consecutive elements in a segment. Also lets call the segment k-good if it contains no more than k different values. Find any longest k-good segment. As the inp…

centos 6.5 apache mysql php_CentOS 6.5系统安装配置LAMP(Apache+PHP5+MySQL)服务器环境

简单点的&#xff1a;1.关闭SELINUX&#xff1a;setenfo 0 暂时关闭2.安装Apache&#xff1a;yum install httpd3.安装MySQL&#xff1a;yum install mysql mysql-server4.安装PHP&#xff1a;yum install php5.相关的配置&#xff1a;PHP关联MySQL&#xff1b;httpd出错信息…

【HDU - 5585】Numbers (水题,数学,数论)

题干&#xff1a; There is a number N.You should output "YES" if N is a multiple of 2, 3 or 5,otherwise output "NO". Input There are multiple test cases, no more than 1000 cases. For each case,the line contains a integer N.(0<N<…

【HDU - 2255】奔小康赚大钱(KM算法模板,二分图最优匹配)

题干&#xff1a; 传说在遥远的地方有一个非常富裕的村落,有一天,村长决定进行制度改革&#xff1a;重新分配房子。 这可是一件大事,关系到人民的住房问题啊。村里共有n间房间,刚好有n家老百姓,考虑到每家都要有房住&#xff08;如果有老百姓没房子住的话&#xff0c;容易引起…

mysql 如何调用函数结果_MySQL自定义函数调用不出结果

自定义函数的代码&#xff1a;DROP FUNCTION IF EXISTS fn_HrStaffBase_GetNameFromidCarddelimiter //CREATE FUNCTION fn_HrStaffBase_GetNameFromidCard (a VARCHAR(30))RETURNS VARCHAR(50)beginreturn (SELECT staff_name FROM hr_staff_base where idCard a);END //--…

【CodeForces - 1066A~E】水题,模拟(有技巧),思维,题意难懂的模拟,二进制问题(有技巧)

A. 题目大意&#xff1a; x坐标上1~L有L个点都是整数&#xff0c;每v个长度就有一个灯亮着&#xff0c;但是有 [ l , r ] 这段区间上有列火车挡住了&#xff0c;问你能看到多少亮灯。 解题报告&#xff1a; 大水题啊&#xff0c;找几个样例就会发现需要特殊处理一下左边界恰好…

【CodeForces - 987C 】Three displays (dp,最长上升子序列类问题,三元组问题)

题干&#xff1a; It is the middle of 2018 and Maria Stepanovna, who lives outside Krasnokamensk (a town in Zabaikalsky region), wants to rent three displays to highlight an important problem. There are nn displays placed along a road, and the ii-th of th…

python中with open写csv文件_Python中的CSV文件使用with语句的方式详解

是否可以直接使用with语句与CSV文件&#xff1f;能够做这样的事情似乎很自然&#xff1a;import csvwith csv.reader(open("myfile.csv")) as reader:# do things with reader但是csv.reader不提供__enter__和__exit__方法,所以这不行.但是我可以分两步做&#xff1a…

【UVA - 11383】Claw Golden Tiger (二分图最优匹配,KM算法原理)

题干&#xff1a; 粘贴不过来。。。 题目大意&#xff1a; 500*500带权格子&#xff0c;每行每列要确定一个值&#xff0c;使row[i]col[j] > val[i][j]&#xff0c;要使所有row值和col值的和最小 输出每行的row[i],和每列的col[i]。 解题报告&#xff1a; 跑一边KM&#…

git object 很大_这才是真正的Git——Git内部原理

本文以一个具体例子结合动图介绍了Git的内部原理&#xff0c;包括Git是什么储存我们的代码和变更历史的、更改一个文件时&#xff0c;Git内部是怎么变化的、Git这样实现的好处等等。TL;DR本文以一个具体例子结合动图介绍了Git的内部原理&#xff0c;包括Git是什么储存我们的代码…

【CodeForces - 195D】Analyzing Polyline (思维,卡精度的处理方式)

题干&#xff1a; As Valeric and Valerko were watching one of the last Euro Championship games in a sports bar, they broke a mug. Of course, the guys paid for it but the barman said that he will let them watch football in his bar only if they help his son …

【CodeForces - 985D】Sand Fortress (二分,贪心,思维构造,技巧,有坑)

题干&#xff1a; You are going to the beach with the idea to build the greatest sand castle ever in your head! The beach is not as three-dimensional as you could have imagined, it can be decribed as a line of spots to pile up sand pillars. Spots are numbe…

mysql多实例脚本_mysql多实例脚本

mysql多实例脚本##mariadb和mysql-server的通用多实例脚本。vi mdp.sh 脚本内容参考内容如下#!/bin/bashecho ‘等待mariadb-server或mysql-server服务软件安装完毕’while truedoyum install -y mariadb-server mariadb &>/dev/nullyum install -y mysql-serv…

scrapy 分布式 mysql_Scrapy基于scrapy_redis实现分布式爬虫部署的示例

准备工作1.安装scrapy_redis包,打开cmd工具,执行命令pip install scrapy_redis2.准备好一个没有BUG,没有报错的爬虫项目3.准备好redis主服务器还有跟程序相关的mysql数据库前提mysql数据库要打开允许远程连接,因为mysql安装后root用户默认只允许本地连接,详情请看此文章部署过程…

(精)DEVC++的几个实用小技巧

依赖 DEV C 5.11 最新版 下载安装DEV C后&#xff0c;使用DEV C打开一个随便的cpp文件&#xff0c;你看到的应该是这样的界面。&#xff08;为了节约读者的流量&#xff0c;图片进行了有损压缩&#xff0c;但是字看得清楚&#xff09; 重点是确认工具栏有AStyle选项。 相信…

win10一按右键就闪屏_升级Win10正式版后屏幕一直闪烁正确的解决办法

Win10正式版屏幕一直闪烁怎么办呢&#xff1f;升级到Win10正式版并进入Windows桌面后&#xff0c;发现屏幕一直不断的闪烁&#xff0c;此时无法执行任务操作。小编最近在升级到Win10正式版后才遇到了这个问题&#xff0c;后台经过反复思考和探索&#xff0c;终于解决了问题&…

KMP 常用模板

写在前面&#xff1a; 代码中有的是i&#xff0c;j&#xff0c;有的是j&#xff0c;k&#xff0c;总之不唯一啊&#xff01;&#xff01;&#xff01;这个一定要弄清楚&#xff01;&#xff01;&#xff01;我没有统一。。 从0开始的字符串&#xff0c;加速版。 void getnext…