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 + 贪心),布布扣,bubuko.com

ZOJ 2109 FatMouse' Trade (背包 dp + 贪心)

时间: 2024-12-29 06:17:43

ZOJ 2109 FatMouse' Trade (背包 dp + 贪心)的相关文章

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

HDU 1009:FatMouse&#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

Codeforces 830A. Office Keys (背包dp+贪心) / (二分+贪心)

题目链接: http://codeforces.com/problemset/problem/830/A 题意: n个人,k个钥匙(n<=k),p表示这些人要到达的位置 给出n个人的位置以及钥匙的位置,问花时间最多的那个人用时最少是多少?? 思路: 二分+贪心: 二分最少时间,需要对a,b位置数组排序,我们check函数只需要从左到右一个一个找过去,因为如果选后边的点,可能会使结果更差,假如当前这个人选后面的点,那可能会选中后面的人可以选的唯一的钥匙,不会使解更优. check(40)的时候答案

20191110luogu3698小Q的棋盘 | 树形背包dp | 贪心

luogu3698小Q的棋盘 题意: 求从树的根节点出发,走n步能经过的最多的点的数量(可以重复走点,但是重复走的步数会记录) 树形背包dp: 对于从0出发,我们可以这样走: 1.选一条岔路一直走下去 2.选一条岔路走后回到0点,再选一条岔路走下去 对应的dp转移: f[0][u][j]代表从u出发走j步不一定回到u点能到达的最大步数 f[1][u][j]代表从u出发走j步回到u点能到达的最大步数 f[0][u][j] = max(f[0][u][j],f[0][u][k] + f[1][too

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

HDU1009 FatMouse&#39; Trade 【贪心】

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

【贪心专题】HDU 1009 FatMouse&#39; Trade (贪心选取)

链接:click here~~ 题意:老鼠准备了M磅猫食,准备拿这些猫食跟猫交换自己喜欢的食物.有N个房间,每个房间里面都有食物.你可以得到J[i]单位的食物,但你需要付出F[i]单位的的猫食. 计算M磅猫食可以获得最多食物的重量. [解题思路]贪心算法,求最优解.将J[i]/F[i]的值从大到小排列,每次取最大的,局部最优,达到全局最优,从而获得最大值. 代码: // 贪心策略,优先选择投资最大的房间,每选择一次,交换次数依次减少,最后的次数用于价值最小的 //注意精度转化:1.0*(int

贪心/hdu 1009 FatMouse&#39; Trade

题意 有n种物品,每一种需要不同的消费,现在手里有m块钱,求问最多可以买多少 分析 贪心 把每一种物品的价格算出来,然后sort一下,按照价格从便宜到贵排序,能买多少买多少,买买买! Accepted Code 1 /* 2 PROBLEM:hdu1009 3 AUTHER:Nicole Lam 4 MEMO:贪心 5 */ 6 7 #include<cstdio> 8 #include<algorithm> 9 using namespace std; 10 11 12 stru

0-1背包的动态规划算法,部分背包的贪心算法和DP算法------算法导论

一.问题描述 0-1背包问题,部分背包问题.分别实现0-1背包的DP算法,部分背包的贪心算法和DP算法. 二.算法原理 (1)0-1背包的DP算法 0-1背包问题:有n件物品和一个容量为W的背包.第i件物品的重量是w[i],价值是v[i].求解将哪些物品装入背包可使这些物品的费用总和不超过背包容量,且价值总和最大.其中每种物品只有一件,可以选择放或者不放. 最优子结构性质:对于0-1问题,考虑重量至多W的最值钱的一包东西.如果去掉其中一个物品j,余下的必是除j以外的n-1件物品中,可以带走的重量