题目描述

1070 Mooncake (25分)
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 (≤1000), the number of different 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

代码(AC)

#include<stdio.h>
#include<algorithm>
using namespace std;
struct mooncake{double num; //月饼的数量 double y;//月饼的总售价double price;//月饼的单价
};
bool cmp(mooncake a,mooncake b)
{return a.price>b.price;
}
int main()
{int n,d;//n表示月饼的种类数,d表示市场的最大需求量 scanf("%d %d",&n,&d);mooncake m[n]; for(int i=0;i<n;i++){scanf("%lf",&m[i].num);}for(int i=0;i<n;i++){scanf("%lf",&m[i].y);m[i].price=m[i].y/m[i].num;}sort(m,m+n,cmp);double g=d*1.0,count=0;for(int i=0;i<n;i++){if(m[i].num<=g){g-=m[i].num;count+=m[i].y;}   else{count+=m[i].price*g;break;}}printf("%.2f\n",count);return 0;
}

PAT A1070:Mooncake之贪心求解相关推荐

  1. 【LeetCode】IPO(使用贪心求解)

    502. IPO - 力扣(LeetCode) 一.题目 假设 力扣(LeetCode)即将开始 IPO .为了以更高的价格将股票卖给风险投资公司,力扣 希望在 IPO 之前开展一些项目以增加其资本. ...

  2. 1579: 【例 5】皇宫看守(最小支配集——贪心求解/树形DP)

    [题目描述] 太平王世子事件后,陆小凤成了皇上特聘的御前一品侍卫. 皇宫以午门为起点,直到后宫嫔妃们的寝宫,呈一棵树的形状,某些宫殿间可以互相望见.大内保卫森严,三步一岗,五步一哨,每个宫殿都要有人全 ...

  3. 算法笔记:加勒比海盗船---最优装载问题 如何将最多的宝物装上船 贪心求解 C++

    问题描述:在北美洲的南部,有一片美丽神秘的海域,名字叫作加勒比海,那里有一群海盗,非常猖獗.有一天海盗收获了很多古董,海盗船的载重为C,每件古董的重量为wi,如何将尽可能多的宝物装上船呢? 思路:很明 ...

  4. 【PAT甲级】A1051-A1100刷题记录

    文章目录 (栈) A1051 Pop Sequence (25 分) 0.47 (静态链表) A1052 Linked List Sorting (25 分) 0.21 (静态树+先根遍历DFS) A ...

  5. 【置顶】【PAT】PAT甲级题目及分类总结(持续更新ing)

    在2019年3月底,我决定考浙大计院,经过一个月还算凑合的学习,痛定思痛,决定整理整理自己的博客. 粗略估计,大概一个月的时间里我AC了31道题,大概用时40个小时上下,毕竟还要把大部分时间花在笔试上 ...

  6. PAT 1069 1070 1071 1072

    pat 1069 The Black Hole of Numbers 水题,代码如下: 1 #include<cstdio> 2 #include<cstdlib> 3 #in ...

  7. 拜托,别再问我贪心算法了!

    来自:码海 前言 上篇一文学会动态规划解题技巧 被不少号转载了,其中发现有一位读者提了一个疑惑,在求三角形最短路径和时,能否用贪心算法求解.所以本文打算对贪心算法进行简单地介绍,介绍完之后我们再来看看 ...

  8. 2017广东工业大学程序设计竞赛决赛 题解源码(A,数学解方程,B,贪心博弈,C,递归,D,水,E,贪心,面试题,F,贪心,枚举,LCA,G,dp,记忆化搜索,H,思维题)...

    心得: 这比赛真的是不要不要的,pending了一下午,也不知道对错,直接做过去就是了,也没有管太多! Problem A: 两只老虎 Description 来,我们先来放松下,听听儿歌,一起&qu ...

  9. 牛客 - Sixth Sense(贪心+二分)

    题目链接:点击查看 题目大意:给出两个长度为 n 的数列记为 a 和 b,现在 a 的数列固定不动,问如何对数列 b 进行排列,可以使得: b[ i ] > a[ i ] (严格大于)的位置尽可 ...

最新文章

  1. 广度优先搜索(BreadthFirstSearch) 迪克斯特拉算法 (Dijkstra's algorithm)
  2. 2017报计算机热不热,2017年五月份热吗?2017年五月天气热不热?
  3. 报名啦!阿里云智能客服对话式AI算法大赛之知识图谱构建与问答
  4. 项目管理其实可以简单一点——任务分工
  5. CCF201409-2 画图 java(100分)
  6. NodeJs .net core connect Azure service bus
  7. kafka启动_Kafka安装部署——单节点
  8. 中缀试转后缀试及前缀试并计算其结果
  9. axios文件上传 formdata_基于业务场景下的图片/文件上传方案总结
  10. 在三个层次对Asp.Net的数据操作进行事务
  11. 华为模拟器如何实现不同Vlan不同网段之间的互通
  12. HttpClient 4.3学习笔记
  13. addr2line排查地址
  14. httpclient基本get用法
  15. 不知道如何raw转换成jpg格式?可以试试这几个软件
  16. 安徒生---海的女儿
  17. 【已解决】Spring容器中找不到ServletWebServerFactory类出现的异常
  18. http.Request
  19. docker 国内镜像设置
  20. openresty通过ffi调用一个c编写的base64动态库

热门文章

  1. PHP社会管理网格化GIS系统,社会管理网格化平台
  2. wpspbc按钮是什么意思_10秒搞定 超简单的PBC按钮配置法
  3. c语言水塔水位控制,基于西门子PLC的水塔水位控制系统及梯形图
  4. HTTP协议常问的面试题(吐血整理)
  5. 判断Object类型的数据是否为空
  6. 解决ROS和python3中cv_bridge的问题
  7. LCD1602与51单片机
  8. 【数学建模】16 数学建模竞赛论文写作
  9. 微信公众号开发--获取当前用户位置,并把经度纬度转化成省市县
  10. Android应用开发用Kotlin还是Java 好?