FatMouse' Trade

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 50   Accepted Submission(s) : 19

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

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

ZJCPC2004

Statistic | Submit | Back
大意:老鼠有m磅猫粮,在一个地方有n个房间,每个这里有猫在看守,第i个房间里有j[i]镑豆子要求f[i]磅猫粮房间里的豆子便可全部带走,但老鼠不一定要全部带走,如果老鼠给了猫f[i]*a%磅的猫粮,则他可获得j[i]*a%磅的豆子,求老鼠可获得的最大数量的豆子是多少?
思路:问题相当于买东西,如何花最少的钱来买最近可能多的东西。所以那件东西花的钱(猫粮)最少就先买哪一个,也就是说花同样的钱(猫粮),哪个买的多就先买哪个。
即先买j[i]/f[i]最大的那个,若不猫要求的猫粮,则就买m/f[i]*j[i]的豆子。可先将j[i]/f[i]排序,再从最多的往下买
代码:
#include <iostream>
#include<iomanip>
#define MAX 9999999
using namespace std;
double j[1000],f[1000],p[1000];
void order(double p[],double j[],double f[],int n)
{int i,s,k;for(i=n; i>0; i--){k=0;for(s=0; s<i; s++){if(p[k]>=p[s]){k=s;}}double t;t=p[k];p[k]=p[i-1];p[i-1]=t;t=j[k];j[k]=j[i-1];j[i-1]=t;t=f[k];f[k]=f[i-1];f[i-1]=t;}
}
int main()
{int n,i;
double s,m;while(cin>>m>>n&&(n!=-1&&m!=-1)){s=0;for(i=0; i<n; i++){cin>>j[i]>>f[i];if(f[i]!=0)//分母等于零的情况处理p[i]=j[i]/f[i];elsep[i]=MAX;//max假定无穷}order(p,j,f,n);//排序for(i=0; i<n; i++){if(m>=f[i])//足够付得起{s=s+j[i];m=m-f[i];}else if(m>=0&&m<f[i])付不起时{s=s+m*p[i];m=0;}}cout<<setiosflags(ios::fixed)<<setprecision(3);cout<<s<<endl;}return 0;
}

HDU 1009FatMouse' Trade相关推荐

  1. hdu 3401 Trade(单调队列优化dp)

    hdu 3401 Trade(单调队列优化dp) 题意:lxhgww喜欢炒股票,他可以在第i天买入或者卖出若干张股票(一天只能买或者卖),两个交易日之间至少相隔w天,问他t天后最多能赚多少. 解题思路 ...

  2. ACM--猫鼠交易--贪心--HDOJ 1009--FatMouse' Trade

    HDOJ题目地址:传送门 FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K ( ...

  3. 动态规划总结与题目分类

    源博客链接:http://blog.csdn.net/cc_again/article/details/25866971 动态规划一直是ACM竞赛中的重点,同时又是难点,因为该算法时间效率高,代码量少 ...

  4. dp进阶之FFT加速+数据结构优化+不等式优化

    快速傅里叶变换 快速傅里叶变换(英语:Fast Fourier Transform, FFT),是快速计算序列的离散傅里叶变换(DFT)或其逆变换的方法.傅里叶分析将信号从原始域(通常是时间或空间)转 ...

  5. (转)dp动态规划分类详解

    dp动态规划分类详解 转自:http://blog.csdn.NET/cc_again/article/details/25866971 动态规划一直是ACM竞赛中的重点,同时又是难点,因为该算法时间 ...

  6. DP分类题目 转载 《志当存高远》大神的 没有冒犯的意思 只是拿过来学习的

    A.各种背包,就根据背包九讲的内容来做吧! --不会背包的可以边看背包九讲边练习 =========================================================== ...

  7. 《动态规划》— 动态规划分类

    动态规划(英语:Dynamic programming,DP)是一种在数学.计算机科学和经济学中使用的,通过把原问题分解为相对简单的子问题的方式求解复杂问题的方法. 动态规划常常适用于有重叠子问题和最 ...

  8. 转:动态规划题目分类

    https://blog.csdn.net/cc_again/article/details/25866971 一.简单基础dp 这类dp主要是一些状态比较容易表示,转移方程比较好想,问题比较基本常见 ...

  9. 【DP专辑】ACM动态规划总结

    转载请注明出处,谢谢.   http://blog.csdn.net/cc_again?viewmode=list          ----------  Accagain  2014年5月15日 ...

最新文章

  1. LeetCode: 102. Binary Tree Level Order Traversal
  2. 深入理解Java 8 Lambda(语言篇——lambda,方法引用,目标类型和默认方法)
  3. 华为招聘公关总监:接触近10位路透资深记者 年薪高达20万美元
  4. Golang标准库RPC实践及改进
  5. python中文分词统计_python 实现中文分词统计
  6. Transfer-Encoding: chunked
  7. Linux下的GCC和Windows下Visual Studio的编译所生成的文件作用
  8. 利用协程实现计时效果
  9. BZOJ4152 AMPPZ2014 The Captain 【最短路】【贪心】*
  10. U盘启动装系统的操作方法(U盘PE启动安装GHOST XP)
  11. jade2html 在线转换,jade转化为html
  12. 运放专题:电压电流转换
  13. 银行业数据安全建设要点分析2022
  14. 自定义图标iconfont
  15. 深度学习 再次爆发_如何教自己再次学习
  16. PI AAE (Advanced Adapter Engine) 介绍一
  17. 网页加入动态动漫人物
  18. php一点通,编程一点通app下载-编程一点通安卓版下载v1.0.1-游迅网
  19. rot13初学者和python的实现
  20. 16丨数据分析基础篇答疑

热门文章

  1. String的空校验
  2. linux 无线网卡ping不同,请教高手,本地网卡和无线网卡均不能ping通网关
  3. Android Q 开机启动流程
  4. Codeforces Round #613 (Div. 2)(B-D)
  5. python-pygame作品之MineCraft小鸡
  6. uos命令_UOS与Deepin OS区别详解
  7. nginx 配置之 proxy_pass参数详解
  8. 键盘定位板图纸_防火卷帘轨道安装强制定位
  9. iOS小技能:设备ID除了使用_idfa、_idfv 还可使用其他替代方案(使用Keychain 存储UUID)
  10. Sql Server 2008 通用语法总结