题目

Mooncake is a Chinese bakery product traditionally eaten during the Mid-Autumn Festival. Many types of fillings and crusts can be found in traditional mooncakes according to the regions culture. Now given the inventory amounts and the prices of all kinds of the mooncakes, together with the maximum total demand of the market, you are supposed to tell the maximum profit that can be made.

Note: partial inventory storage can be taken. The sample shows the following situation: given three kinds of mooncakes with inventory amounts being 180, 150, and 100 thousand tons, and the prices being 7.5, 7.2, and 4.5 billion yuans. If the market demand can be at most 200 thousand tons, the best we can do is to sell 150 thousand tons of the second kind of mooncake, and 50 thousand tons of the third kind. Hence the total profit is 7.2 + 4.5/2 = 9.45 (billion yuans).

Input Specification:

Each input file contains one test case. For each case, the first line contains 2 positive integers N (<=1000), the number of diferent kinds of mooncakes, and D (<=500 thousand tons), the maximum total demand of the market. Then the second line gives the positive inventory amounts (in thousand tons), and the third line gives the positive prices (in billion yuans) of N kinds of mooncakes. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print the maximum profit (in billion yuans) in one line, accurate up to 2 decimal places.

Sample Input:

3 200

180 150 100

7.5 7.2 4.5

Sample Output:

9.45

题目分析

已知不同种类月饼的库存,总价格,市场需求量,求最大利润

解题思路

  1. 贪心思想:依次获取单位价格最贵的月饼
  2. 结构体moon,存储月饼的库存,总价格,单位价格(输入总价格时预处理,减少一次遍历)
  3. 按照单位价格降序排序,依次获取,直到达到市场需求量

Code

Code 01

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct moon {
double w; // total weight (in thousand tons)
double p; // total price (in billion yuan)
double pp; // price per thousand ton
};
bool cmp(moon &m1,moon &m2) {
return m1.pp>m2.pp;
}
int main(int argc, char * argv[]) {
// 1 接收数据
int N,D;
scanf("%d %d",&N,&D);
vector<moon> vms(N); //存放月饼数据
for(int i=0; i<N; i++) scanf("%lf", &vms[i].w);
for(int i=0; i<N; i++) {
scanf("%lf", &vms[i].p);
vms[i].pp = vms[i].p/vms[i].w; //计算每重量单位的价格
}
// 2 按照单位价格高-低排序
sort(vms.begin(),vms.end(),cmp);
// 3 求最大利润
double tp = 0.0;
for(int i=0; i<N&&D>0; i++) {
if(vms[i].w<D) {//如果当前月饼种类的重量小于需求剩余数,全部销售
D=D-vms[i].w;
tp+=vms[i].p;
} else { //如果当前月饼种类的重量大于需求剩余数,按照单位价计算剩余需求重量
tp+=D*vms[i].pp;
D=0;
}
}
printf("%.2f", tp);
}

PAT Advanced 1070 Mooncake (25) [贪⼼算法]的更多相关文章

  1. PAT 甲级 1070 Mooncake (25 分)(结构体排序,贪心,简单)

    1070 Mooncake (25 分)   Mooncake is a Chinese bakery product traditionally eaten during the Mid-Autum ...

  2. PAT Advanced 1003 Emergency (25) [Dijkstra算法]

    题目 As an emergency rescue team leader of a city, you are given a special map of your country. The ma ...

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

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

  4. PAT Advanced 1067 Sort with Swap(0,*) (25) [贪⼼算法]

    题目 Given any permutation of the numbers {0, 1, 2,-, N-1}, it is easy to sort them in increasing orde ...

  5. PAT (Advanced Level) 1070. Mooncake (25)

    简单贪心.先买性价比高的. #include<cstdio> #include<cstring> #include<cmath> #include<vecto ...

  6. PAT Advanced 1037 Magic Coupon (25) [贪⼼算法]

    题目 The magic shop in Mars is ofering some magic coupons. Each coupon has an integer N printed on it, ...

  7. PAT Advanced 1033 To Fill or Not to Fill (25) [贪⼼算法]

    题目 With highways available, driving a car from Hangzhou to any other city is easy. But since the tan ...

  8. PAT 1070. Mooncake (25)

    Mooncake is a Chinese bakery product traditionally eaten during the Mid-Autumn Festival.  Many types ...

  9. 【PAT甲级】1070 Mooncake (25 分)(贪心水中水)

    题意: 输入两个正整数N和M(存疑M是否为整数,N<=1000,M<=500)表示月饼的种数和市场对于月饼的最大需求,接着输入N个正整数表示某种月饼的库存,再输入N个正数表示某种月饼库存全 ...

随机推荐

  1. 六十五、SAP中通过BREAK-POINT下断点,进行调试

    一.代码如下,有2个断点的按钮,可以可以写入BREAK-POINT人工断点 二.运行之后,程序会被断下来, 四个执行按钮,意思分别为:单步进入子程序,单步不进入子程序,返回外面,执行到断点处 三.我们 ...

  2. (转)深入理解JVM—JVM内存模型

    原文地址:http://www.cnblogs.com/dingyingsi/p/3760447.html 我们知道,计算机CPU和内存的交互是最频繁的,内存是我们的高速缓存区,用户磁盘和CPU的交互 ...

  3. 实验吧-杂项-MD5之守株待兔(时间戳&python时间戳函数time.time())

    其实也有点蒙圈,因为从没做过和时间戳有关的题. 打开网站,将系统密钥解密得到一串值,而自己的密钥解密是空的,既然说是要和系统匹配,就把解密得到的值以get方式送出去. 但是发现还是在自己的密钥也发生了 ...

  4. python 虚拟环境的安装

    方式一 1. pip install virtualenv 2. virtualenv 虚拟环境的名字 3. mac上 source + 虚拟环境的目录/bin/activate win上 直接进入虚 ...

  5. Linux基础命令层级图-01

    Linux基础命令层级图-01:

  6. 第一部分 JavaScript语言核心(二)

    第四章 表达式和运算符 P66 运算符优先级,从上到下: p68 属性访问表达式和调用表达式的优先级比运算符优先级都要高,eg: typeof my.functions[x](y) //typeof在 ...

  7. sudo 提权漏洞(CVE-2019-14287)复现 (10.16 第二十二天)

    sudo是Linux系统命令,让普通账号以root身份去执行某些命令,比,安装软件.查看某些配置文件.关机.重启等操作,如果普通账号需要使用sudo需要修改配置文件/etc/sudoers,将sudo ...

  8. POJ 2031:Building a Space Station 最小生成树

    Building a Space Station Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 6083   Accepte ...

  9. windows driver 驱动程序我的下载地址

    http://download.csdn.net/detail/sz76211822/8197619 版权声明:本文为博主原创文章,未经博主允许不得转载.

  10. 对DataFrame的再理解

    1.构造需要从字典构造 cds={'code':["002372.XSHE","002415.XSHE","002304.XSHE",&qu ...