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 + 贪婪)

时间: 2024-10-25 11:19:01

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

ZOJ 2109 FatMouse&#39; 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

ZOJ 3956 Course Selection System 背包DP

ZOJ3956 观察数据范围, c的值非常小 只有100 所以c的和也很有限 只有50000 是否可以从这里下手? 对于某一个c的和 我们一定希望h的和最大 才有可能是最终答案. 于是有了类似背包的dp方程. 代码很简单,就不给出方程了. //比赛的时候想得太多,都想到斜率优化上了,完全忽略了c的范围这么小!!!毕竟图样. //一个人的面命运,当然要靠自我奋斗,但是也要考虑到历史的行程. #include<iostream> #include<cstdio> #include<

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

本题就先排序老鼠的重量,然后查找老鼠的速度的最长递增子序列,只是由于须要按原来的标号输出,故此须要使用struct把三个信息打包起来. 查找最长递增子序列使用动态规划法.主要的一维动态规划法了. 记录路径:仅仅须要记录后继标号,就能够逐个输出了. #include <stdio.h> #include <algorithm> using namespace std; const int MAX_N = 1005; struct MouseSpeed { int id, w, s;

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

FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 41982    Accepted Submission(s): 13962 Problem Description FatMouse prepared M pounds of cat food, ready to trade with the cats g

ZOJ 3164 Cookie Choice 分组背包 混合背包

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3181 题意: 就是混合背包加分组背包,有的物品是01背包,有的是多重背包,有的是完全背包,同时物品还有不超过8组的分组,如果在同一组则最多只能选一种.问能不能恰好地用掉D的容量,并且使所获价值最大. 分析: 开始的想法是多开一个下标,先把没有分组的做了,在0的下标,然后分组分别在组号的下标里按顺序处理,是什么背包就用什么做法,不过一直WA,对拍小数据大数据都没啥问题(可能随机

HDU 1160 FatMouse&#39;s Speed (sort + dp)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1160 给你一些老鼠的体重和速度,问你最多需要几只可以证明体重越重速度越慢,并输出任意一组答案. 结构体按照体重从小到大排序,然后根据速度就是最长下降子序列. 1 //#pragma comment(linker, "/STACK:102400000, 102400000") 2 #include <algorithm> 3 #include <iostream> 4

hdu--1059--多重背包||DP||搜索

碎碎念----- 突然 觉得好无聊~~ 还好 只有4天了~~ 直接上题吧~~ touch  me 使用多重背包的代码是转自---键盘上的舞者---- 写得特别有想法 使用dp的代码是porker写的.. 使用搜索的 我一开始是将价值从低到高搜索的 这样TLE了,,因为速度实在是太慢了 假如有arr[k]个 那么我要有arr[k]+1个操作... 但是 按价值从高到低的搜索 只要遵循 尽量在不到sum/2的时候去搜就是了 有就添加进来...但这个方法的正确性 没有得到证明... 1 #includ

HDU 1160 FatMouse&#39;s Speed 简单DP

FatMouse's Speed Problem Description FatMouse believes that the fatter a mouse is, the faster it runs. To disprove this, you want to take the data on a collection of mice and put as large a subset of this data as possible into a sequence so that the

[HDOJ1160]FatMouse&#39;s Speed(DP)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1160 FatMouse believes that the fatter a mouse is, the faster it runs. To disprove this, you want to take the data on a collection of mice and put as large a subset of this data as possible into a sequen