This is a creation in Article, where the information may have evolved or changed.
Dessert Shop Cake Cutting problem (dynamic programming, go language implementation)
Problem Recurrence:
Little Y recently worked at the dessert shop, whose job was to cut the cake. Now there are N customers to buy the cake, and each customer has a time to arrive, as well as the length of the cake to buy AI. Since little y can only serve one customer at a time, "the problem is rigorous: and if the customer does not have a waiter to serve him at once, he will leave" so there is no way to provide services for conflicting customers. Ask Little y how many customers can be served. Little y can decide whether to sell cakes to a customer. If you promise to buy cut cake of the length ai, then small y also cut the cake into unit length to the customer. If the AI cake is cut into X and Ai-x, the time cost is x* (Ai-x). For example, when a user in 1 time, the need for a length of 4 cake, at this time small y can be cut into 2 its first length of 2, the cost of 4, then two segment length 2 is divided into 1, 1, the cost is 1 and 1, the total time is 4+1+1 = 6, then small y for the user Service time is 6.
It is known that the customer enters the shop time, and the size of the cake is purchased. "The author special Note: In the original topic slightly modified, this article focuses on the clear thought"
Analysis: (reprint please specify source and author name)
question one: how long does it take for a cake of size n to be cut into unit length?
F (x) =x* (n-x) plot function sketch can be obtained: x=1 when the minimum value, that is, 1 units per 1 units of the cut
It is proved by mathematical induction that the final settlement of the above solution is also minimal.
n=2 f (x) is clearly the smallest solution
N=3 f (x) is clearly the smallest solution
N=4 f (x) is clearly the smallest solution
...
The minimum solution can also be obtained when N=I-1 is assumed
When N=i
Cut into X and (I-x)
It is clear that X must be a solution to the n that has been introduced earlier, and I-x is a solution that was previously introduced, while F (x) 's Optimal and F (i-x) is the tangent of 1 units per 1 units.
At this time as long as the x* (i-x) value is guaranteed to be the smallest, the smallest case x=1, it is proved that every 1 units of 1 units of the final settlement of the solution is also the smallest.
question two: How to choose the best service target when you know the time of entering the store and the size of the cake purchased?
This problem can at least be solved with a greedy strategy that seems to include a dynamic plan that looks like a 01 knapsack problem
Dynamic planning:
F[i][t] represents the number of service objects that have been served by the former I in the T time period
S[i] Indicates the length of service required by the first person
R[i] indicates that the first person reaches the point in time
㈠ There are two cases for the choice of service I
① selected, but to meet the completion of the service of the first I not more than t
(If I am selected, there may be some people who are not allowed to select the former i-1 person)
F[i][t]=f[i-1][r[i]]+1 "T>=r[i]+s[i]"
②, do not change the strategy in T-time
(The reason why is not selected, because the first person to the time, little Y has not served the previous person, or if I was selected I will delay more people behind)
F[I][T]=F[I-1][T]
㈡ decision exit conditions (decision-making conditions)
Time has no negative numbers, so no need to judge
I==0 returns 1 or 0
Explanation: Because i==0 is the last person, if the condition is met T >= R[0]+s[0], return 1
㈢ decision Entry Conditions
T=max{s[i]+r[i]}
You can then conclude that:
The state transition equation is : f[i][t]=max{F[i-1][t-s[i]] (T>=r[i]+s[i]),
F[I-1][T]}
The following gives the go language implementation code:
Package Mainimport ("FMT")/* for minimum service duration, 1 units of 1 units per cut, get the smallest solution */func Smin (n Int32) int32 {if n&1 = 0 {return (N/2) * (n-1 )}return (n-1)/2 * n}/* time per customer */func Servertime (S, lenght []int32, maxlen int32) {for I: = Range lenght {s[i] = Smin ( Lenght[i])}}/* maximum value */func maxInt32 (A, b int32) int32 {if a > B {return a}return B}/*DP problem core Author: Days of csdn blog: http: Blog.csdn.net/wapwo?viewmode=contents*/func Dptz (I, t Int32, R, S []int32) int32 {if i = = 0 {if T >= r[0]+s[0] {retur n 1}return 0}if t >= r[i]+s[i] {return MaxInt32 (DPTZ (i-1, R[i], R, s) +1, DPTZ (i-1, T, R, s))}return Dptz (i-1, T, R, s)} /* Request last End time */func EndTime (R, S []int32) int32 {var max, tmp int32 = 0, 0for i: = range r {tmp = R[i] + s[i]if Max < tmp { max = Tmp}}return Max}func main () {//Cake length, first served time and service time length: = []int32{2, 2, 3, 4}r: = []int32{5, 5, 6, 10}s: = Make ([]in T32, 4) servertime (s, length, 4) fmt. Println (DPTZ (3, EndTime (R, s), R, s))}