链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1109

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

翻译:

从前有仅仅肥肥的老鼠。他叫FatMouse,他就像人类的恐怖分子跟敌人交易军火一样,猥琐的他准备了M磅猫食,准备与守卫仓库的大猫们进行交易,仓库里有他最爱吃的食物Javabean。

仓库里有N个房间,第i间房间里有J[i]磅Javabean且须要F[i]磅猫食进行交换,FatMouse不必吧每一个房间里的Javabean所实用于交易,相反。他可以付给大猫F[i]*a%磅猫食,从而换的J[i]*a%磅的Javabean。当中,a是一个实数,如今他给你布置一个家庭作业,请你告诉他他最多可以获得多少磅Javabean。

输入描写叙述:

输入包括多组測试数据,每组測试数据的开头一行是两个非负整数M, N.接下来的N行中,每行包括两个非负整数J[i]和F[i],最后一组測试数据是两个-1。全部的整数的值不糊超过1000。

输出描写叙述:

对于每组測试数据,在一行上打印出一个3位小数的实数,这个实数是FatMouse可以交易到的最大数量的Javabean.

解题思路:

本题要求输出最大交易量。并保留三位小数。这样,我们使用J[i]除以F[i]就得到了a,那么,交易的时候,为了获得最多的Javabean,要先交易a大的。这样就确保了能交易到最多的Javabean.

把数据读入结构体中,再将结构体作为向量的元素,再按a由大到小的顺序给向量排序,然后依次进行计算。这样的题目属于背包类的题目!

(dp + 贪心)

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <set>
#define MAXN 10005
#define RST(N)memset(N, 0, sizeof(N))
#include <algorithm>
using namespace std; typedef struct Mouse_ {
double J, F;
double a;
}Mouse; int n, m;
vector <Mouse> v;
vector <Mouse> ::iterator it; bool cmp(const Mouse m1, const Mouse m2)
{
if(m1.a != m2.a) return m1.a > m2.a;
else return m1.F < m2.F;
} int main()
{
while(~scanf("%d %d", &n, &m)) {
if(n == -1 && m == -1) break;
Mouse mouse;
v.clear();
for(int i=0; i<m; i++) {
scanf("%lf %lf", &mouse.J, &mouse.F);
mouse.a = mouse.J/mouse.F;
v.push_back(mouse);
}
sort(v.begin(), v.end(), cmp);
double sum = 0;
for(int i=0; i<v.size(); i++) {
if(n > v[i].F) {
sum += v[i].J;
n -= v[i].F;
}else {
sum += n*v[i].a;
break;
}
}
printf("%.3lf\n", sum);
}
return 0;
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

ZOJ 2109 FatMouse&#39; Trade (背包 dp + 贪婪)的更多相关文章

  1. zoj 2109 FatMouse' Trade

    FatMouse' Trade Time Limit: 2 Seconds      Memory Limit: 65536 KB FatMouse prepared M pounds of cat ...

  2. FatMouse&#39; Trade(杭电1009)

    FatMouse' Trade Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Tot ...

  3. zoj 1108 FatMouse's Speed 基础dp

    FatMouse's Speed Time Limit: 2 Seconds      Memory Limit:65536 KB     Special Judge FatMouse believe ...

  4. zoj 1108 FatMouse's Speed 基础dp

    FatMouse's Speed Time Limit: 2 Seconds      Memory Limit:65536 KB     Special Judge FatMouse believe ...

  5. HDU 1009:FatMouse&#39; Trade(简单贪心)

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

  6. HDU 1160 FatMouse&#39;s Speed(DP)

    题意  输入n个老鼠的体重和速度   从里面找出最长的序列  是的重量递增时速度递减 简单的DP  令d[i]表示以第i个老鼠为所求序列最后一个时序列的长度  对与每一个老鼠i  遍历全部老鼠j  当 ...

  7. hdu 1009 FatMouse&#39; Trade

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

  8. ZOJ 3956 Course Selection System 背包DP

    ZOJ3956 观察数据范围, c的值非常小 只有100 所以c的和也很有限 只有50000 是否可以从这里下手? 对于某一个c的和 我们一定希望h的和最大 才有可能是最终答案. 于是有了类似背包的d ...

  9. HDU 1160 FatMouse&#39;s Speed DP题解

    本题就先排序老鼠的重量,然后查找老鼠的速度的最长递增子序列,只是由于须要按原来的标号输出,故此须要使用struct把三个信息打包起来. 查找最长递增子序列使用动态规划法.主要的一维动态规划法了. 记录 ...

随机推荐

  1. Visual Studio 2015使用EF6的DBFirst模式操作Sqlite数据库

    什么是DBFirst 1:到官方下载并安装32位驱动(如果你是旧版的驱动,卸载掉,然后下载最新版的,否则操作数据时会出现异常) 2:通过Nuget获取System.Data.SQLite(会默认把下面 ...

  2. 设置SQLServer数据库中某些表为只读的多种方法

    原文:设置SQLServer数据库中某些表为只读的多种方法 翻译自:http://www.mssqltips.com/sqlservertip/2711/different-ways-to-make- ...

  3. Java的进程内缓存框架:EhCache (转)

    EhCache 是一个纯Java的进程内缓存框架,具有快速.精干等特点,是Hibernate中默认的CacheProvider. Ehcache缓存的特点: 1. 快速. 2. 简单. 3. 多种缓存 ...

  4. 用Ghostscript API将PDF格式转换为图像格式(C#)

    原文:用Ghostscript API将PDF格式转换为图像格式(C#) 由于项目需要在.net下将pdf转换为普通图像格式,在网上搜了好久终于找到一个解决方案,于是采用拿来主义直接用.来源见代码中注 ...

  5. C++实现链表

    最后几天留在Intel,没什么事情,都是开开会.趁着闲功夫,把数据结构复习一下,写了一个list.时间仓促,有些地方考虑的可能没那么到位,望高手们指点. #include <iostream&g ...

  6. Oracle解锁的相关操作(转)

    当某个数据库用户在数据库中插入.更新.删除一个表的数据,或者增加一个表的主键时或者表的索引时,常常会出现ora-00054:resource busy and acquire with nowait ...

  7. 深入理解ASP.NET MVC Day1

    深入理解ASP.NET MVC   ASP.NET vs MVC vs WebForms 许多ASP.NET开发人员开始接触MVC认为MVC与ASP.NET完全没有关系,是一个全新的Web开发,事实上 ...

  8. 第6章 适配器模式(Adapter Pattern)

    原文 第6章 适配器模式(Adapter Pattern) 概述 将一个类的接口转换成客户希望的另外一个接口.Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以在一起工作.  解决的问 ...

  9. 深入探讨 Java 类加载器[转]

    原文地址:http://www.ibm.com/developerworks/cn/java/j-lo-classloader/index.html 类加载器(class loader)是 Java™ ...

  10. Spring环境配置

    研究spring3的时候发现一个非常好用的特性:环境配置(spring2是否有此特性未知) 官方演示样例代码例如以下: <!-- app-config.xml --> <beans ...