hdu 1009 FatMouse' Trade
来源:互联网 发布:菲律宾网络博客靠谱吗 编辑:程序博客网 时间:2024/06/16 07:22
FatMouse' Trade
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 76996 Accepted Submission(s): 26442
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.
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 37 24 35 220 325 1824 1515 10-1 -1
Sample Output
13.33331.500题目大意:本题就是要求背包中最多可以存放的物品数量,每个物品有相应的体积,并且物品可以分割解题思路:hdu超级水题,本题为典型简单贪心算法题目,由于物品可分割,不能用01背包做。 先对给出的各个仓库的信息(豆子量,所需猫粮量)进行分析,可知仓库中豆子越多,所需猫粮越少,则越划得来,兑换该房间的豆子可以得到最大的豆子量。 因此,先求出各个仓库的豆子量/所需猫粮量的比值(简称比值),比值大的应该考虑优先兑换。 然后将所有仓库信息按照比值从大到小排序,得出各仓库的兑换先后顺序,存储在结构体数组stru[]里面,准备兑换。 然后按排序结果对相应仓库进行兑换,若当前所剩猫粮量不为0并且还有仓库未进行兑换,则继续兑换, (1)如果当前老鼠剩下的猫粮大于兑换当前仓库所有的豆子的所需的猫粮量,则兑换该仓库的所有豆子,豆子总量增加该仓库总豆子量的值,所剩猫粮总量减去兑换当前仓库所有豆子所需猫粮量; (2)如果当前老鼠剩下的猫粮小于兑换当前仓库所有的豆子的所需的猫粮量,则兑换该仓库的所有豆子*所剩猫粮/所需的猫粮量,豆子总量增加所有豆子*所剩猫粮/所需的猫粮量(注意精度,这里的值可能会产生小数),所剩猫粮总量置0; 最后,按题目要求输出兑换所得豆子总量(保留3位小数)即可。代码:#include<cstdio>#include<iostream>#include<algorithm>#define maxn 1005using namespace std;typedef struct node{ double x; double y; double temp;} stru;bool cmp(stru x,stru y){ return x.temp>y.temp;}int main(){ stru str[maxn]; int m,n,i; double a[maxn],b[maxn],num[maxn]; double ans; while(cin>>m>>n&&(m!=-1||n!=-1)) { ans=0; for(i=0; i<n; i++) { cin>>str[i].x>>str[i].y; str[i].temp=str[i].x*1.0/str[i].y; } sort(str,str+n,cmp); for(i=0; i<n; i++) { if(m-str[i].y<0) { ans=ans+m*str[i].temp; break; } else { ans+=str[i].x; m-=str[i].y; } } printf("%.3f\n",ans); } return 0;}
阅读全文
0 0
- HDU 1009 FatMouse' Trade
- hdu 1009 FatMouse' Trade
- hdu 1009 FatMouse' Trade
- HDU 1009 - FatMouse' Trade
- HDU-1009 FatMouse' Trade
- hdu 1009 FatMouse' Trade
- HDU 1009 FatMouse' Trade
- HDU 1009 FatMouse' Trade
- HDU 1009 FatMouse' Trade
- HDU 1009 FatMouse' Trade
- HDU 1009 FatMouse' Trade
- HDU 1009 FatMouse' Trade
- hdu 1009 FatMouse' Trade
- HDU 1009 FatMouse' Trade
- HDU--1009--FatMouse' Trade
- hdu 1009 FatMouse' Trade
- hdu 1009 FatMouse' Trade
- HDU-1009 FatMouse' Trade
- com.mysql.jdbc.MysqlDataTruncation: Data truncation: Truncated incorrect DOUBLE value
- js+json实现ajax实例
- UVA
- JMeter 参数化之利用JDBC Connection Configuration从数据库读取数据并关联变量
- 【JSP学习笔记(4)】——数据库应用
- hdu 1009 FatMouse' Trade
- STL序列式容器概述
- 怎么比较两个json字符串完全相等
- leetcode:Binary Tree Preorder Traversal 先序遍历
- excle.分列.仅仅随笔
- c++内存泄露排查思路
- JavaScript中的DOM
- Hibernate 介绍
- 2017Google Codejam round2 Problem B. Roller Coaster Scheduling