FatMouse' Trade HDU - 1009(贪心)

怎么说呢,相信这题也卡了不少人,评价一下就是: 真是扣字扣到极致了!
能看到我的文章我很欣赏你,那么请让我带你重新理解下这道题:

先说一下题目:

原题:

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.

原题目链接 Here

我尝试给翻译一下,快看 ↓ 我英语不好 any trouble no guarantee

你玩欢乐斗地主上瘾了,对就是你,你要把家里的粮食拿去换豆子;疫情期间,没几个店开门,你找半天只找到了'n'家店
他们的规定都一致:第i家店允许你用F[i]粮食换'最多'J[i]欢乐豆
(题目说的含蓄,但是我告诉你,他们里面'有傻子',你不用给他粮食 他就会把他的豆豆都给你;
也有企图把你当傻子的'黑心商',他一个豆子都没有所以无论你给他多少粮食都不会换到豆子);
但是你只知道这些还是不够的,你还想'用你有限的粮食尽可能多得豆子',并且你不惜'以0.001度量豆子和粮食'
And..'你家的粮食也许早就被吃空空了'

思路:
上面是闹着玩的,只要注意 m的浮点性质 以及 所有的整数均可以是0 就好
按照最大利润,我们将n家店排序,黑心商直接全部抛掷最后,不考虑;傻瓜商家我们最先考虑(拿到就是赚到)
然后单独处理剩下粮食只够买店里一部分的情况(如果有)

一下代码我用的冒泡,比较水了,可供参考:

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <list>
#include <stack>
#include <queue>
#include <deque>
#include<iomanip>
using namespace std;
typedef long long LL;
typedef long double LD;
typedef unsigned long long ULL;
const int maxn = 1e9;
const int INF = 0x3f3f3f3f;
const double PI = acos(-1.0);
const int dx[] = {0, 0,1,-1};
const int dy[] = {1,-1,0, 0};
struct P {
    int x, y;
    P(int x = 0, int y = 0): x(x), y(y) {}
};
//int r, c;
//inline bool in_border (int mx, int my) {
//    return mx>=0 && mx<r && my>=0 && my<c;
//}
bool cmp (const void* p1, const void* p2) {
    return true;
}

int main() {//int T; cin >> T;

    int i, n; double m;//m为浮点
    while(cin >> m >> n && n!=-1) {// m = 0 ? n = 0 ?

        int a[1010][2]={0};
        double r[1010]={0};
        for(i = 0; i < n; i++) {
            cin >> a[i][0] >> a[i][1];
			
            if(!a[i][0]) r[i] = -1; // 不交易 设为 minimal
            else if (!a[i][1]) r[i] = INF; // 白嫖快乐 设为 maximal
            //NOTE: 若某店数据为(0,0)我们认为它是黑心商
            r[i] = a[i][0] * 1.0 / a[i][1]; // 其余正常除法即可
        }
        for(i = 0; i < n-1; i++)
            for(int j = 0; j < n-1-i; j++)
                if(r[j] < r[j+1]) {
                    swap(r[j], r[j+1]);
                    swap(a[j], a[j+1]);
                } // 这样黑心商都排在最后了

        double ans = 0; int i = 0;
        while(1) {
            if(m >= a[i][1]) {
                m -= a[i][1];
                ans += a[i][0];
                i++;
            }
            else {ans += m*a[i][0]/a[i][1]; break;} // 单独处理
            if(r[i] <= 0) break; // 没店了 或 黑心商 break 
        }
        printf("%.3lf\n", ans);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值