ZOJ 2109 FatMouse' Trade (背包 dp + 贪婪)
链接: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' Trade (背包 dp + 贪婪)的更多相关文章
- zoj 2109 FatMouse' Trade
FatMouse' Trade Time Limit: 2 Seconds Memory Limit: 65536 KB FatMouse prepared M pounds of cat ...
- FatMouse' Trade(杭电1009)
FatMouse' Trade Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Tot ...
- zoj 1108 FatMouse's Speed 基础dp
FatMouse's Speed Time Limit: 2 Seconds Memory Limit:65536 KB Special Judge FatMouse believe ...
- zoj 1108 FatMouse's Speed 基础dp
FatMouse's Speed Time Limit: 2 Seconds Memory Limit:65536 KB Special Judge FatMouse believe ...
- HDU 1009:FatMouse' Trade(简单贪心)
FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- HDU 1160 FatMouse's Speed(DP)
题意 输入n个老鼠的体重和速度 从里面找出最长的序列 是的重量递增时速度递减 简单的DP 令d[i]表示以第i个老鼠为所求序列最后一个时序列的长度 对与每一个老鼠i 遍历全部老鼠j 当 ...
- hdu 1009 FatMouse' Trade
FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- ZOJ 3956 Course Selection System 背包DP
ZOJ3956 观察数据范围, c的值非常小 只有100 所以c的和也很有限 只有50000 是否可以从这里下手? 对于某一个c的和 我们一定希望h的和最大 才有可能是最终答案. 于是有了类似背包的d ...
- HDU 1160 FatMouse's Speed DP题解
本题就先排序老鼠的重量,然后查找老鼠的速度的最长递增子序列,只是由于须要按原来的标号输出,故此须要使用struct把三个信息打包起来. 查找最长递增子序列使用动态规划法.主要的一维动态规划法了. 记录 ...
随机推荐
- Cookie和Session (转)
Session和Cookie在网站开发中是用来保存用户与后端服务器的交互状态.它们有各自的缺点和优点.而且,他们的优点和应用场景是对立的. Cookie 完整地描述:当一个用户通过HTTP访问一个 ...
- 你所不了解的float(滥用float的怪异现象) (转)
阅读目录 float设计初衷就是为了实现文字环绕效果 如何解决浮动造成的父容器塌陷? 兼容各浏览器清除浮动的通用方式 滥用浮动 运用浮动的一些特性 浮动与布局 浮动与单侧固定布局 浮动与智能自适应的流 ...
- hdu 1159 Common Subsequence (dp乞讨LCS)
Common Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- eclipse 在win7 64两个图标出现位操作系统无法锁定到任务栏或任务栏
eclipse 在win7 64位操作系统无法锁定到任务栏或者任务栏上出现两个图标 解决的方法 eclipse在win7 64bit下无法锁定到任务栏问题(或是锁定后任务栏出现两个eclipse图标) ...
- 【cocos2d-js公文】十八、Cocos2d-JS v3.0物业风格API
1. 新的API风格 我们直接来看看你能够怎样使用Cocos2d-JS v3.0: 曾经的API 新的API node.setPosition(x, y); node.x = x; node.y = ...
- (大数据工程师学习路径)第三步 Git Community Book----Git基本用法(上)
一.git的初始化 1.Git 配置 使用Git的第一件事就是设置你的名字和email,这些就是你在提交commit时的签名. $ git config --global user.name &quo ...
- 【SSH 基金会】SSH框架--struts进一步的详细解释(两)
继上篇博客 既然我们知道了不使用struts给我们带来这么多弊端,那么以下我们来看看struts是怎样封装的.怎么解决我们出现的问题的? 先来说一下struts的基本流程,帮助大家理解以下的代码: S ...
- mvc5 解析route源码实现自己的route系统
Asp.net mvc5 解析route源码实现自己的route系统 url route 路由系统的责任是找到匹配的路由,创建路由数据,并将请求分配给一个处理程序. 选择动作是 MVC 的处理程序 ...
- Html.Partial和Html. RenderPartial用法
Html.Partial和Html. RenderPartial用法 Html.partial和RenderPartial的用法与区别Html.partial和RenderPartial都是输出htm ...
- 怎样改动、扩展并重写Magento代码
作为一个开发人员的你,肯定要改动Magento代码去适应你的业务需求,可是在非常多时候我们不希望改动Magento的核心代码,这里有非常多原因, 比如将来还希望升级Magento.还想使用很多其它的M ...