1020 月饼 (25 分)

月饼是中国人在中秋佳节时吃的一种传统食品,不同地区有许多不同风味的月饼。现给定所有种类月饼的库存量、总售价、以及市场的最大需求量,请你计算可以获得的最大收益是多少。

注意:销售时允许取出一部分库存。样例给出的情形是这样的:假如我们有 3 种月饼,其库存量分别为 18、15、10 万吨,总售价分别为 75、72、45 亿元。如果市场的最大需求量只有 20 万吨,那么我们最大收益策略应该是卖出全部 15 万吨第 2 种月饼、以及 5 万吨第 3 种月饼,获得 72 + 45/2 = 94.5(亿元)。

输入格式:

每个输入包含一个测试用例。每个测试用例先给出一个不超过 1000 的正整数 N 表示月饼的种类数、以及不超过 500(以万吨为单位)的正整数 D 表示市场最大需求量。随后一行给出 N 个正数表示每种月饼的库存量(以万吨为单位);最后一行给出 N 个正数表示每种月饼的总售价(以亿元为单位)。数字间以空格分隔。

输出格式:

对每组测试用例,在一行中输出最大收益,以亿元为单位并精确到小数点后 2 位。

输入样例:

3 20
18 15 10
75 72 45

输出样例:

94.50

题目中的坑已经用红色标出,审题是关键
另外,double精度比float高
本题思路是求出每吨多少钱,然后每次选出最大的(性价比最高的)
进行运算,还需要考虑极端情况,也就是全部月饼都不够需求
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include <vector>
#include<algorithm>
#include<string>
#include<string.h>
#include<math.h>
#define max 1000
#define debug 0
using namespace std; int main() {
#if debug
    freopen("in.txt", "r", stdin);
#endif     int n = 0,D=0;
    double s = 0,d=0;
    double store[max];
    double sale[max];
    memset(store, 0, sizeof(double)*max);
    memset(sale, 0, sizeof(double)*max);
    cin >> n>>D;
    for (int i = 0; i < n; i++)
    {
        cin >> store[i];
    }
    for (int i = 0; i < n; i++)
    {
        cin >> sale[i];
    }
    double total = 0;
    for (int i = 0; i < n; i++)
    {
        total += store[i];
    }
    if (total <= D)
    {
        double s = 0;
        for (int i = 0; i < n; i++)
        {
            s += sale[i];
        }
        printf("%.2f", s);
        return 0;
    }
    double saleperstore[max];
    memset(saleperstore, 0, sizeof(double)*max);
    for (int i = 0; i < n; i++)
    {
        saleperstore[i] = sale[i] / store[i];
    }
    int ma = 0;
    while (d<D)
    {         for (int i = 1; i < n; i++)
        {
            if (saleperstore[i] > saleperstore[ma])
                ma = i;
        }
        if (store[ma] <= D-d)
        {
            d = d + store[ma];
            s += sale[ma];
        }else
        {
            s += saleperstore[ma] * (D-d);
            d = D;
        }
        saleperstore[ma] = 0;
        ma = 0;
    }
    printf("%.2f", s); #if debug
    freopen("CON", "r", stdin);
#endif
    return 0;
}

PAT Basic 1020的更多相关文章

  1. PAT Basic 1020 月饼 (25 分)

    月饼是中国人在中秋佳节时吃的一种传统食品,不同地区有许多不同风味的月饼.现给定所有种类月饼的库存量.总售价.以及市场的最大需求量,请你计算可以获得的最大收益是多少. 注意:销售时允许取出一部分库存.样 ...

  2. PAT Basic 1020 ⽉饼 (25) [贪⼼算法]

    题目 ⽉饼是中国⼈在中秋佳节时吃的⼀种传统⻝品,不同地区有许多不同⻛味的⽉饼.现给定所有种类⽉饼的库存量.总售价.以及市场的最⼤需求量,请你计算可以获得的最⼤收益是多少. 注意:销售时允许取出⼀部分库 ...

  3. PAT Basic 1057

    1057 数零壹 给定一串长度不超过 10​5​​ 的字符串,本题要求你将其中所有英文字母的序号(字母 a-z 对应序号 1-26,不分大小写)相加,得到整数 N,然后再分析一下 N 的二进制表示中有 ...

  4. PAT (Basic Level) Practise (中文)-1039. 到底买不买(20)

    PAT (Basic Level) Practise (中文)-1039. 到底买不买(20) http://www.patest.cn/contests/pat-b-practise/1039 小红 ...

  5. PAT (Basic Level) Practise (中文)- 1022. D进制的A+B (20)

    PAT (Basic Level) Practise (中文)-  1022. D进制的A+B (20)  http://www.patest.cn/contests/pat-b-practise/1 ...

  6. PAT (Basic Level) Practise (中文)- 1024. 科学计数法 (20)

    PAT (Basic Level) Practise (中文)- 1024. 科学计数法 (20) http://www.patest.cn/contests/pat-b-practise/1024 ...

  7. PAT (Basic Level) Practise (中文)-1025. 反转链表 (25)

    PAT (Basic Level) Practise (中文)-1025. 反转链表 (25)   http://www.patest.cn/contests/pat-b-practise/1025 ...

  8. PAT (Basic Level) Practise (中文)- 1026. 程序运行时间(15)

    PAT (Basic Level) Practise (中文)- 1026. 程序运行时间(15)    http://www.patest.cn/contests/pat-b-practise/10 ...

  9. PAT (Basic Level) Practise (中文)-1027. 打印沙漏(20)

    PAT (Basic Level) Practise (中文)-1027. 打印沙漏(20)  http://www.patest.cn/contests/pat-b-practise/1027 本题 ...

随机推荐

  1. 设置git记住用户和密码

     git config --global credential.helper store

  2. Sql语句拼接(EXEC和sp_executesql的区别)

    1.前言 MSSQL为我们提供了两种动态执行SQL语句的命令,分别是EXEC和sp_executesql;通常,sp_executesql则更具有优势,它提供了输入输出接口,而EXEC没有.还有一个最 ...

  3. 【转载】Python and Mysql Andy Dustman

    0. http://mysql-python.sourceforge.net/ Python and MySQL: This is a presentation I did a couple year ...

  4. bat处理复制文件

    1.建bat文件自动执行复制,删除命令. 复制cd.dll文件至windows\system32的bat文件内容: @echo offset JtlDir=D:\apache-jmeter-3.0\t ...

  5. 牛客挑战赛30 小G砍树 树形dp

    小G砍树 dfs两次, dp出每个点作为最后一个点的方案数. #include<bits/stdc++.h> #define LL long long #define fi first # ...

  6. BZOJ3796 Mushroom追妹纸 字符串 SA KMP

    原文链接https://www.cnblogs.com/zhouzhendong/p/9253173.html 题目传送门 - BZOJ3796 题意 找一个串 $w$ 满足: 1.$w$ 是 $s_ ...

  7. Period kmp

    For each prefix of a given string S with N characters (each character has an ASCII code between 97 a ...

  8. 029 c3p0的小测试

    今天被问到这个问题,就实验了一下,后续会继续补充一些配置项的意思. 一:操作步骤 1.大纲 2.新建sql -- ---------------------------- -- Table struc ...

  9. 了解一下vue源码中vue 的由来

     我们之前提到过 Vue.js 构建过程,在 web 应用下,我们来分析 Runtime + Compiler 构建出来的 Vue.js,它的入口是 src/platforms/web/entry-r ...

  10. 【LeetCode算法-14】Longest Common Prefix

    Write a function to find the longest common prefix string amongst an array of strings. If there is n ...