FatMouse' Trade

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


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

经典贪心,老鼠要换取到最多的JavaBean,按照每个房间的j和f,算出比率,当然是j/f越大越好,按照比率排一下序。
#include <iostream>
#include <stdio.h>
#include <algorithm> using namespace std; struct dat
{
int j;
int f;
double sc;
} data[]; bool cmp(dat a, dat b)
{
return a.sc>b.sc;
} int main()
{
int m,n;
double ans;
while(scanf("%d%d",&m,&n) && m!=- && n!=-)
{
for(int i=; i<n; i++)
{
scanf("%d%d",&data[i].j, &data[i].f);
data[i].sc = (double)data[i].j/(double)data[i].f;
} sort(data, data+n, cmp); ans = ;
for(int i=; i<n; i++)
{
if(data[i].f<=m)
{
ans+=data[i].j;
m-=data[i].f;
}
else
{
ans+=data[i].sc*(double)m;
break;
} }
printf("%.3f\n", ans);
} return ;
}

hdu_1009 贪心的更多相关文章

  1. BZOJ 1692: [Usaco2007 Dec]队列变换 [后缀数组 贪心]

    1692: [Usaco2007 Dec]队列变换 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1383  Solved: 582[Submit][St ...

  2. HDOJ 1051. Wooden Sticks 贪心 结构体排序

    Wooden Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

  3. HDOJ 1009. Fat Mouse' Trade 贪心 结构体排序

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

  4. BZOJ 1691: [Usaco2007 Dec]挑剔的美食家 [treap 贪心]

    1691: [Usaco2007 Dec]挑剔的美食家 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 786  Solved: 391[Submit][S ...

  5. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  6. 【BZOJ-4245】OR-XOR 按位贪心

    4245: [ONTAK2015]OR-XOR Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 486  Solved: 266[Submit][Sta ...

  7. code vs 1098 均分纸牌(贪心)

    1098 均分纸牌 2002年NOIP全国联赛提高组  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解   题目描述 Description 有 N 堆纸牌 ...

  8. 【BZOJ1623】 [Usaco2008 Open]Cow Cars 奶牛飞车 贪心

    SB贪心,一开始还想着用二分,看了眼黄学长的blog,发现自己SB了... 最小道路=已选取的奶牛/道路总数. #include <iostream> #include <cstdi ...

  9. 【贪心】HDU 1257

    HDU 1257 最少拦截系统 题意:中文题不解释. 思路:网上有说贪心有说DP,想法就是开一个数组存每个拦截系统当前最高能拦截的导弹高度.输入每个导弹高度的时候就开始处理,遍历每一个拦截系统,一旦最 ...

随机推荐

  1. Java学习--list,set,Map接口使用

    list接口: 泛型:规定list中的元素的类型 /*     *      * 泛型不能使用基本数据类型(可以使用基本类型的包装类)     *      */    public void tes ...

  2. 一:Jquery-selector

    一:jQuery概述 1.基本功能 a.访问和操作DOM元素:获取元素,修改其样式和内容,删除元素,复制元素... b.对页面事件的处理:不需要指定事件中的函数名,直接在事件中绑定响应函数(匿名函数) ...

  3. SpringBoot Mybatis的驼峰命名

    开启驼峰命名的方法 第一种方式: 可以在配置类中进行配置.配置的Demo如下: @Bean(name="sqlSessionFactory") public SqlSessionF ...

  4. 4.java设计模式-原型模式(prototype)

    在<JAVA与模式>一书中开头是这样描述原型(Prototype)模式的: 原型模式属于对象的创建模式.通过给出一个原型对象来指明所有创建的对象的类型,然后用复制这个原型对象的办法创建出更 ...

  5. 记录一次teamview无法远程连接对方teamview的过程

    问题描述: teamviewer 提示 超时后连接被阻断.您的许可证对您与伙伴的最大话时间有所限制...... 解决方法: 1.先将自己的teamview完全卸载,连同安装目录一起删除.尽量卸载完全 ...

  6. 浅谈arguments与arguments的妙用

    1.每个函数都有一个arguments属性,表示函数的实参集合,这里的实参是重点,就是执行函数时实际传入的参数的集合. 2.arguments不是数组而是一个对象,但它和数组很相似,所以通常称为类数组 ...

  7. JavaScript自定义字符串格式化

    在JS中没有字符串拼接的方法,如过要使用怎么办呢?这时我们可以通过字符串的prototype可以自定义方法. <script> String.prototype.format = func ...

  8. 苹果ios,下拉菜单错位的问题(目前iphone x没发现有这个问题)

    苹果手机,点击下拉框,再点击确认按钮,页面位置错乱(感觉背景整体往上移动了一段距离,并且,页面所有的元素都往上移了一定的距离),导致手机页面底部留白的问题,并且,元素实际位置跟页面位置不一致. 解决方 ...

  9. Cloudera Manager5安装总结遇到问题及解决办法 CDH 5.8 on CentOS 7

    问题导读:1.Cloudera Manager在线安装你认为有什么优点和缺点?2.Cloudera Manager安装过程中遇到,你认为该如何解决?3.在线安装addr.arpa domain nam ...

  10. Android深入理解Context(三)Context完全解析

      1· Context类型        我们知道,Android应用都是使用Java语言来编写的,那么大家可以思考一下,一个Android程序和一个Java程序,他们最大的区别在哪里?划分界限又是 ...