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 region's 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 (≤), the number of different kinds of mooncakes, and D (≤ 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

算法设计

这道题是贪心算法的经典应用。可对给出的月饼按照单价从大到小进行排序,将单价多的月饼尽可能地卖多一些,便能得到最大的收益。

注意点

题目给定的各种月饼的库存量以及单价并未说明是正整数,所以需要用double来储存,否则有一个测试点将会出现答案错误的情况

 #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct Node
{
double qty, prof, pric;
}node;
int N, D;
int main()
{
cin >> N >> D;
vector<Node>Cake;
double res = 0.0;
for (int i = ; i < N; ++i)
{
cin >> node.qty;
Cake.push_back(node);
}
for (int i = ; i < N; ++i)
{
cin >> Cake[i].prof;
Cake[i].pric = Cake[i].prof / Cake[i].qty;
}
sort(Cake.begin(), Cake.end(), [](Node a, Node b) {return a.pric > b.pric; });
for (int i = ; i < N && D>; ++i)
{
int buy = Cake[i].qty < D ? Cake[i].qty : D;
D -= buy;
res += buy * Cake[i].pric;
}
printf("%.2f\n", res);
return ;
}

PAT甲级——A1070 Mooncake的更多相关文章

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

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

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

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

  3. PAT甲级题解(慢慢刷中)

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6102219.html特别不喜欢那些随便转载别人的原创文章又不给 ...

  4. PAT甲级考前整理(2019年3月备考)之一

       转载请注明出处:https://www.cnblogs.com/jlyg/p/7525244.html 终于在考前,刷完PAT甲级131道题目,不容易!!!每天沉迷在刷题之中而不能超脱,也是一种 ...

  5. 【转载】【PAT】PAT甲级题型分类整理

    最短路径 Emergency (25)-PAT甲级真题(Dijkstra算法) Public Bike Management (30)-PAT甲级真题(Dijkstra + DFS) Travel P ...

  6. PAT甲级题分类汇编——杂项

    本文为PAT甲级分类汇编系列文章. 集合.散列.数学.算法,这几类的题目都比较少,放到一起讲. 题号 标题 分数 大意 类型 1063 Set Similarity 25 集合相似度 集合 1067 ...

  7. PAT甲级题分类汇编——序言

    今天开个坑,分类整理PAT甲级题目(https://pintia.cn/problem-sets/994805342720868352/problems/type/7)中1051~1100部分.语言是 ...

  8. PAT甲级题解分类byZlc

    专题一  字符串处理 A1001 Format(20) #include<cstdio> int main () { ]; int a,b,sum; scanf ("%d %d& ...

  9. PAT甲级1131. Subway Map

    PAT甲级1131. Subway Map 题意: 在大城市,地铁系统对访客总是看起来很复杂.给你一些感觉,下图显示了北京地铁的地图.现在你应该帮助人们掌握你的电脑技能!鉴于您的用户的起始位置,您的任 ...

随机推荐

  1. flutter setInitialRoute: 不生效

    概述 需要实现native跳转到flutter 指定的路由页面. iOS 工程中发现 FlutterViewController  setInitialRouter 无效,在我的需求里面是: 在iOS ...

  2. IdentityServer_0_参考资料

    官方 项目地址:https://github.com/IdentityServer/IdentityServer4 官方Demo:https://github.com/IdentityServer/I ...

  3. Erlang学习记录:语法和特性

    特性 大下排序:number < atom < reference < fun < port < pid < tuple < list < bit st ...

  4. Java Collection - HashMap

    HashMap源码解析 java.util.HashMap 类 https://www.cnblogs.com/ysocean/p/8711071.html HashMap线程不安全的原因 https ...

  5. 关于Cadence OrCad 16.6的破解

    相信很多人都知道去老吴的博客上找安装包和破解文件,但是上面的自称一键式破解程序.以及破解图文说明,都是很有问题的. 首先,该一键式破解程序默认的文件后缀与该程序指向的安装压缩包后缀不一致:其次,该程序 ...

  6. 软件-开发软件:Android Studio

    ylbtech-软件-开发软件:Android Studio Android Studio 是谷歌推出的一个Android集成开发工具,基于IntelliJ IDEA. 类似 Eclipse ADT, ...

  7. ">/dev/null 2>&1 "是什么意思

    在Linux中: 0:表示键盘输入(stdin) 1:表示标准输出(stdout),系统默认是1 2:表示错误输出(stderr) shell命令:command >/dev/null  2&g ...

  8. <每日一题>题目11:以文件夹名称作为参数,返回该文件夹下所有文件的路径

    ''' 分析: 1.知道文件夹名称(假设是形如:E:\\software\\Notepad++),很显然可以通过OS模块去求 2.OS.listdir(sPath),列出文件夹内所有的文件和文件夹,以 ...

  9. UMP系统架构 Zookeeper

  10. 安卓中 使用html来使文字变色Html.fromHtml

    在这里  我是用的html使文字的个别颜色变红 String textStr = " 本课程为<font color=\"#FF0000\">" + ...