分披萨问题_比萨疯狂问题
分披薩問題
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 pizzaM: area of medium sized pizzaL: area of large sized pizzaCS: cost of small sized pizzaCM: cost of medium sized pizzaCL: cost of large sized pizzaOutput:Minimum amount of money neededConstraints1<=X<=5001<=S< M< L<=1001<=CS< CM<CL=1000Example:
例:
Input:X : 17S : 4 M : 6 L : 12 CS: 40 CM: 60 CL: 100Output:160Explanation:
說明:
IncludeHelp wants at least 17 unit2 of PizzaFew possible arrangements can be,5 Small pizza (size of 5*4=20) amounting 2004 small pizza, one medium pizza (size 4*4+6=22) amounting 2203 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 160And 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) = XArea of small sized pizza: SArea of medium sized pizza: MArea of large sized pizza: LCost of small sized pizza: CSCost of medium sized pizza: CMCost of large sized pizza: CLNow,
現在,
f(X) = minimum amount of money to get at least X area of pizzaSo, f(X) can be written recursively as,
因此, f(X)可以遞歸地寫為
f(X) = minimum(f(X-S) + CS, f(X-M) + CM, f(X-L) + CLWhere,
哪里,
X-S >= 0, X-m >= 0, X-L >= 0That 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 0Set dps=(i-S)≤0?0:(i-S) // if a medium pizza cut is possible then dpm=i-M else 0Set dpm=(i-M)<=0?0:(i-M) // if a large pizza cut is possible then dpm=i-L else 0Set dpl=(i-L)<=0?0:(i-L) // find the minimum for this sub-problemarr[i]=min(arr[dps]+CS,arr[dpm]+CM,arr[dpl]+CL) end for3) 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 partint dps = (i - S) <= 0 ? 0 : (i - S);//check for valid pizza partint dpm = (i - M) <= 0 ? 0 : (i - M);//check for valid pizza partint dpl = (i - L) <= 0 ? 0 : (i - L);//find the minimumarr[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: 160RUN 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
分披薩問題
總結
以上是生活随笔為你收集整理的分披萨问题_比萨疯狂问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ruby 新建对象_Ruby中的面向对象
- 下一篇: python字符串find_Python