FatMouse' Trade

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

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
 
Author
CHEN, Yue
 
Source
 
Recommend
JGShining   |   We have carefully selected several
similar problems for you:  1008 1050 1051 2037 1052 
#include <string.h>
#include <iostream>
#include <algorithm>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
struct sa
{
int a,b;
double c;
}data[];
bool cmp(sa x,sa y)
{
return x.c>y.c;
}
int main()
{
int i,j,m,n;
double sum;
while (cin>>m>>n&&m!=-&&n!=-)
{
sum=;
for (i=;i<n;i++)
{
cin>>data[i].a>>data[i].b;
data[i].c=(double)data[i].a/(double)data[i].b;
}
sort(data,data+n,cmp);
//for (i=0;i<n;i++)
//cout<<data[i].a<<" "<<data[i].b<<" "<<data[i].c<<endl;
for (i=;i<n;i++)
{
if (m>=data[i].b)
{
m-=data[i].b;
sum+=data[i].a;
}
else if (m<data[i].b&&m>)
{
sum+=data[i].c*(double)m;
m=;
}
else
break;
}
printf("%.3lf\n",sum);
}
return ;
}

HDU1009老鼠的旅行 (贪心算法)的更多相关文章

  1. 贪心算法:旅行商问题(TSP)

    TSP问题(Traveling Salesman Problem,旅行商问题),由威廉哈密顿爵士和英国数学家克克曼T.P.Kirkman于19世纪初提出.问题描述如下: 有若干个城市,任何两个城市之间 ...

  2. 基于贪心算法求解TSP问题(JAVA)

    概述 前段时间在搞贪心算法,为了举例,故拿TSP来开刀,写了段求解算法代码以便有需之人,注意代码考虑可读性从最容易理解角度写,没有优化,有需要可以自行优化! 详细 代码下载:http://www.de ...

  3. HDU 1009 FatMouse' Trade (贪心算法)

    题意:就是老鼠要用猫粮换粮食,第i个房间一些东西,要用东西去换,可以不全换.问给定的猫粮最多能换多少粮食. 析:贪心算法.我们先算出来每个房间物品的平均价格是多少,肯定越低越好,并且如果能全换就全换, ...

  4. C/C++贪心算法解决TSP问题

    贪心算法解决旅行商问题 TSP问题(Traveling Salesman Problem,旅行商问题),由威廉哈密顿爵士和英国数学家克克曼T.P.Kirkman于19世纪初提出.问题描述如下: 有若干 ...

  5. 贪心算法(Greedy Algorithm)

    参考: 五大常用算法之三:贪心算法 算法系列:贪心算法 贪心算法详解 从零开始学贪心算法 一.基本概念: 所谓贪心算法是指,在对问题求解时,总是做出在当前看来是最好的选择.也就是说,不从整体最优上加以 ...

  6. 算法导论----贪心算法,删除k个数,使剩下的数字最小

    先贴问题: 1个n位正整数a,删去其中的k位,得到一个新的正整数b,设计一个贪心算法,对给定的a和k得到最小的b: 一.我的想法:先看例子:a=5476579228:去掉4位,则位数n=10,k=4, ...

  7. LEETCODE —— Best Time to Buy and Sell Stock II [贪心算法]

    Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the price of a ...

  8. ACM_ICPC hdu-2111(简单贪心算法)

    一道非常简单的贪心算法,但是要注意输入的价值是单位体积的价值,并不是这个物品的总价值!#include <iostream> #include <stdio.h> #inclu ...

  9. 基于贪心算法的几类区间覆盖问题 nyoj 12喷水装置(二) nyoj 14会场安排问题

    1)区间完全覆盖问题 问题描述:给定一个长度为m的区间,再给出n条线段的起点和终点(注意这里是闭区间),求最少使用多少条线段可以将整个区间完全覆盖 样例: 区间长度8,可选的覆盖线段[2,6],[1, ...

随机推荐

  1. SpringAOP

    首先导包, 我用的是Spring4.0.4;需要这三个包 Spring-AOP-4.0.4.REALEASE.jar + Spring-aspect-4.0.4.REALEASE.jar +aspec ...

  2. java中的不为空判断

    String不为空判断 if(null != str && !"".equals(str)) List不为空判断 if(list!=null && ...

  3. this的使用

    1.使用this调用本类中的属性 class Person{ private String name; private int age; public Person(String name,int a ...

  4. IOCP I/O完成端口(了解)

    IOCP(I/O Completion Port,I/O完成端口)是性能最好的一种I/O模型.它是应用程序使用线程池处理异步I/O请求的一种机制.在处理多个并发的异步I/O请求时,以往的模型都是在接收 ...

  5. PRML Chapter 1. Introduction

    PRML Chapter 1. Introduction 为了防止忘记,要把每章的重要内容都记下来,从第一章开始 2012@3@28 今天又回去稍微翻了一下第一章内容,发现第一次看的时候没有看透,每次 ...

  6. ios 图片尺寸

  7. 让scrollView、tableView滚动到底部

    - (void)scrollsToBottomAnimated:(BOOL)animated { CGFloat offset = self.tableView.contentSize.height ...

  8. (转)MFC的一些宏的整理 (DECLARE_DYNCREATE/IMPLEMENT_DYNCREATE)

    很早看了MFC的一些宏的实现,什么RUNTIME_CLASS, DECLARE_DYNAMIC, DECLARE_DYNCREATE,IMPLEMENT_DYNCREATE, etc,看了就烦,现在整 ...

  9. “System.Transactions.Diagnostics.DiagnosticTrace”的类型初始值设定项引发异常[WCF]

    未处理System.TypeInitializationException  HResult=-2146233036  Message=“System.ServiceModel.Diagnostics ...

  10. du: fts_read 失败: 无法分配内存

    今天在查看一个大的文件时突然报出一个du: fts_read 失败: 无法分配内存的错误. 用 ulimit -a 查看下 core file size (blocks, -c) 0 data seg ...