分披萨问题

Problem statement:

问题陈述:

There is a shop which sells pizza of three different sizes- Small, Medium, and Large Pizza. IncludeHelp is crazy for Pizzas. A small Pizza has an area of 'S' unit2, a medium Pizza has an area of 'M' unit2 and a large Pizza has an area of 'L' unit2. Cost of Small, medium and Large Pizza is 'CS' , 'CM', and 'CL' respectively. IncludeHelp wants at least 'X' unit2 of pizza. What is the minimum amount of money needed to buy at least X unit2 of Pizza? If more than one arrangement is possible, choose that one which requires Least Money. Two arrangements are said to be different if They have different quantities of Small, medium, or large pizzas!

有一家商店出售三种不同尺寸的比萨饼-小,中和大比萨饼。 IncludeHelp对于Pizza来说是疯狂的。 小比萨饼的面积为“ S”单元2 ,中比萨饼的面积为“ M”单元2 ,大比萨饼的面积为“ L”单元2 。 小,中,大披萨的价格分别为“ CS”“ CM”“ CL” 。 IncludeHelp至少需要比萨饼的“ X”单元2购买至少X比萨饼的第2单元所需的最低金额是多少? 如果可能有不止一种安排,请选择一种需要最少钱的安排。 如果有不同数量的小,中或大比萨饼,则这两种安排据说会有所不同!

    Input:
Input contains 6 integers-
X: total area of pizza needed (at least)
S: area of small sized pizza
M: area of medium sized pizza
L: area of large sized pizza
CS: cost of small sized pizza
CM: cost of medium sized pizza
CL: cost of large sized pizza
Output:
Minimum amount of money needed
Constraints
1<=X<=500
1<=S< M< L<=100
1<=CS< CM<CL=1000

Example:

例:

    Input:
X : 17
S : 4
M : 6
L : 12
CS: 40
CM: 60
CL: 100
Output:
160

Explanation:

说明:

    IncludeHelp wants at least 17 unit2 of Pizza
Few possible arrangements can be,
5 Small pizza (size of 5*4=20) amounting 200
4 small pizza, one medium pizza (size 4*4+6=22) amounting 220
3 small pizza, two medium pizza (size 3*4+2*6=24) amounting 240
...
1 large pizza and 1 medium pizza (size 1*6+1*12=18) amounting 160

And this is the optimal money. No other arrangements can lead to a more optimal solution for this problem( that means we can't minimize the money anymore).

这是最理想的钱。 没有其他安排可以为这个问题找到更理想的解决方案(这意味着我们不能再把金钱减少到最低限度了)。

So, how we can solve the above problem?

那么,如何解决以上问题呢?

To understand the problem, we can figure out that it's quite similar to the knapsack problem with constraints that we have an infinite supply of the pizzas and the number of different kinds of pizzas is three. We need to achieve the total size of pizza while investing the minimum amount of money.

为了理解这个问题,我们可以发现它与背包问题非常相似,但有一个局限性,就是我们有无限量的比萨饼,而不同种类的比萨饼的数量是3。 我们需要在投资最小金额的同时实现比萨的总大小。

So, let,

所以让,

    Total area of pizza needed (at least) = X
Area of small sized pizza: S
Area of medium sized pizza: M
Area of large sized pizza: L
Cost of small sized pizza: CS
Cost of medium sized pizza: CM
Cost of large sized pizza: CL

Now,

现在,

    f(X) = minimum amount of money to get at least X area of pizza

So, f(X) can be written recursively as,

因此, f(X)可以递归地写为

    f(X) = minimum(f(X-S) + CS, f(X-M) + CM, f(X-L) + CL

Where,

哪里,

    X-S >= 0, X-m >= 0, X-L >= 0

That means, we choose either of small, medium or large pizza whichever is feasible leading to minimum money for any sub-problem

就是说,我们选择小,中或大披萨,只要可行,就可以使任何子问题的支出降至最低

Since the recursion tree would generate many overlapping subproblems we need to convert it into DP.

由于递归树会生成许多重叠的子问题,因此我们需要将其转换为DP。

Solution Approach:

解决方法:

Converting the recursion into DP:

将递归转换为DP:

    1)  Declare arr[X+1] and initialize arr[0]=0 which is result for X=0;
2)  for  i=1 to X  //for each size of pizza
// if a small pizza cut is possible then dps=i-S else 0
Set dps=(i-S)≤0?0:(i-S)
// if a medium pizza cut is possible then dpm=i-M else 0
Set dpm=(i-M)<=0?0:(i-M)
// if a large pizza cut is possible then dpm=i-L else 0
Set dpl=(i-L)<=0?0:(i-L)
// find the minimum for this sub-problem
arr[i]=min(arr[dps]+CS,arr[dpm]+CM,arr[dpl]+CL)
end for
3) Return arr[X] which is final result.

C++ Implementation:

C ++实现:

#include <bits/stdc++.h>
using namespace std;
int min(int x, int y, int z)
{if (x <= y && x <= z)
return x;
if (y <= z)
return y;
return z;
}
void minimumMoney(int X, int S, int M, int L, int CS, int CM, int CL)
{int arr[X + 1];
arr[0] = 0;
for (int i = 1; i <= X; i++) {//check for valid pizza part
int dps = (i - S) <= 0 ? 0 : (i - S);
//check for valid pizza part
int dpm = (i - M) <= 0 ? 0 : (i - M);
//check for valid pizza part
int dpl = (i - L) <= 0 ? 0 : (i - L);
//find the minimum
arr[i] = min(arr[dps] + CS, arr[dpm] + CM, arr[dpl] + CL);
}
cout << "Minimum amount of money: " << arr[X] << endl;
}
int main()
{int X, S, M, L, CS, CM, CL;
cout << "Enter X, total size(at least)\n";
cin >> X;
cout << "Enter S, small pizza size\n";
cin >> S;
cout << "Enter M, medium pizza size\n";
cin >> M;
cout << "Enter L, large pizza size\n";
cin >> L;
cout << "Enter CS,cost of small pizza \n";
cin >> CS;
cout << "Enter CM,cost of medium pizza \n";
cin >> CM;
cout << "Enter CL,cost of large pizza \n";
cin >> CL;
minimumMoney(X, S, M, L, CS, CM, CL);
return 0;
}

Output

输出量

RUN 1:
Enter X, total size(at least)
17
Enter S, small pizza size
4
Enter M, medium pizza size
6
Enter L, large pizza size
12
Enter CS,cost of small pizza
40
Enter CM,cost of medium pizza
60
Enter CL,cost of large pizza
100
Minimum amount of money: 160
RUN 2:
Enter X, total size(at least)
17
Enter S, small pizza size
3
Enter M, medium pizza size
6
Enter L, large pizza size
12
Enter CS,cost of small pizza
50
Enter CM,cost of medium pizza
60
Enter CL,cost of large pizza
100
Minimum amount of money: 160

翻译自: https://www.includehelp.com/icp/pizza-mania-problem.aspx

分披萨问题

分披萨问题_比萨疯狂问题相关推荐

  1. 分披萨问题_比萨问题–建造者与装饰者

    分披萨问题 问题陈述 我们需要为一家披萨公司构建软件,该公司想要准备不同类型的披萨,例如鸡肉披萨,扁平面包,意大利辣香肠披萨和特制奶酪,并在上面放些配料. 让我们尝试看看哪种设计模式适合该问题说明以及 ...

  2. 计算机考研340分什么概念,考研340分什么概念_中国考研网官网入口

    考研340分什么概念_中国考研网官网入口由广东研究生考试网考试快讯栏目由提供,更多关于考研340分什么概念,中国考研网官网入口,广东研究生考试快讯的内容,请关注广东研究生考试频道/广东人事考试网! 从 ...

  3. 2016计算机考研330分,考研330分什么概念_中国研究生招生信息网官方

    考研330分什么概念_中国研究生招生信息网官方由广东研究生考试网考试快讯栏目由提供,更多关于考研330分什么概念,中国研究生招生信息网官方,广东研究生考试快讯的内容,请关注广东研究生考试频道/广东人事 ...

  4. 考研计算机350分相当于什么水平,考研350分什么水平_中国研究生招生信息网登录...

    考研350分什么水平_中国研究生招生信息网登录由广东研究生考试网考试快讯栏目由提供,更多关于考研350分什么水平,中国研究生招生信息网登录,广东研究生考试快讯的内容,请关注广东研究生考试频道/广东人事 ...

  5. 分库分表之_分库分表 + 复杂查询

    前言 Github:https://github.com/HealerJean 博客:http://blog.healerjean.com 代码配置暂时和和分库分表之_分库分表相同.但是为了测试下面的 ...

  6. 披萨店小程序_比萨问题–建造者与装饰者

    披萨店小程序 问题陈述 我们需要为一家披萨公司构建软件,该公司想要准备不同类型的披萨,例如鸡肉披萨,扁平面包,意大利辣香肠披萨和额外的奶酪,并在上面放些配料. 让我们尝试看看哪种设计模式适合该问题说明 ...

  7. hive 修改分桶数 分桶表_疯狂Hive之DDL操作二(三)

    分区表创建 在Hive Select查询中一般会扫描整个表内容,会消耗很多时间做没必要的工作.有时候只需要扫描表中关心的一部分数据,因此建表时引入了partition分区概念 分区表指的是在创建表的时 ...

  8. POJ 3122 分披萨(二分查找)

    题目链接:http://poj.org/problem?id=3122 题目大意: 有 n 块披萨(大小不一样), f 个人分,包含主人自己 f+1 人: 每人吃的披萨必须是一块披萨上切下来的.每个人 ...

  9. mysql分表插件_分库分表简单?那我想问如何实现“分库分表插件”?

    随着系统数据量的日益增长,在说起数据库架构和数据库优化的时候,我们难免会常常听到分库分表这样的名词. 当然,分库分表有很多的方法论,比如垂直拆分.水平拆分:也有很多的中间件产品,比如MyCat.Sha ...

最新文章

  1. Linux系统的启动过程
  2. UPS故障案例集(一)
  3. spring ioc的包的扫描(基于注解)
  4. 求助!!css选择器为什么有很多标签查不到
  5. 节选—Android 视频直播 ( 从快播到直播,从高清到无码 )十年视频开发项目
  6. 关于JVM中YGC的来龙去脉
  7. SpringBoot-(1)-IDEA创建SpringBoot项目并运行访问接口
  8. 一个可解释的植物胁迫表型的深度机器视觉框架(大豆叶片胁迫程度估算)
  9. 计算机装配调试员培训内容.doc,电子计算机装配调试员理论培训文档.doc
  10. 深度学习基础(基础知识0)
  11. 【51nod 1439】互斥对【容斥原理】
  12. matlab分位数回归,分位数回归及其实例
  13. 终于搞懂了回车与换行的区别
  14. 宝宝的个人博客开通了
  15. API是什么?api的意思!!!
  16. 我发现凡是给offer的公司,面试时基本不问技术细节,那些问得又多又细的公司,后面就没下文了!
  17. MATLAB 中 simulink 里的 scope显示图像格式的设置
  18. deepin启动盘制作工具_YUMI——多重引导制作工具
  19. Dell技术支持年度笑话总结
  20. 月薪2千到年薪百万,厂妹到高盛程序员,她书写了一个女孩的史诗

热门文章

  1. esxi挂载Linux的nfs盘,ESXi安装centos7挂载群晖NFS
  2. mysql migrations_Code First Migrations更新数据库结构(数据迁移)
  3. c++用牛顿法开多次根_望远镜的历史之三:大神出世,改变望远镜历史的竟然是牛顿...
  4. 改变centos系统的时区
  5. MIP ACCESS细节剖析
  6. 5-Dockerfile文件
  7. Problem C: 01字串
  8. Idea项目中常见错误及笔记(Old)
  9. Android手机用wifi连接adb调试的方法
  10. 高级软件工程第一次作业--准备