Question

There is a stick of length N. You want to cut and sell the stick. Each...

There is a stick of length N. You want to cut and sell the stick. Each cut has a different price depending on its length. Design an algorithm to find the optimal way to cut the stick into two pieces and the sum of prices is maximized. A list of pairs (length, price) is given. For instance, {(1,1), (2,5), (3,8), (4,9)} and N = 4. The optimal solution in this case is cutting into two pieces of length 2 each and its price sum is 5 + 5 10.

Your algorithm’s time complexity should be no larger than O(N2).

The algorithm should be designed to cut the stick into two pieces only no more no less. Edge cases are not necessary just general algorithm for optimal way to cut the stick will be sufficient.

Homework Answers

Answer #1

Answer:

We are creating an algorithm using Bottom Up Solution:

Algorithm:

ExtendedBottomUpStick(p, n)
        s: array(0..n)    //optimal value for sticks of length 0 to n
        a: array(0..n)    //optimal first cut for stick of length 0 to n
        s(0) := 0
        for j in 1 .. n loop
            q := MinInt
            for i in 1 .. j loop  //Find the max cut position for length j
                if q < p(i) + s(j-i) then
                    q := p(i) + s(j-i)
                    a(j) := i    //Remember the value of best so far value of i
                end if
            end loop
            s(j) := q
        end loop
        return s and a

    PrintCutStickSolution(p, n)
        (s, a) := ExtendedBottomUpStick(p, n)
        while n > 0 loop
            print a(n)
            n := n - a(n)
        end loop

Here, s[] -> stick array

a[] -> Optimal first cut for stick of length from 0 to N

This bottom up solution require O(n2) time.

Hope I answered the question.

If you have any doubts, or queries, feel free to ask

I'll respond to you as soon as I can.

Have a nice day

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
The Setup: A piece of wire of length 10 cm is cut into two (not necessarily...
The Setup: A piece of wire of length 10 cm is cut into two (not necessarily equal length) pieces. One piece of length x cm is made into a circle and the rest is made into a square. 1. Express the sum of the areas of the square and circle as a function of x 2. For what x-value is maximum area achieved? 3. For maximum area, how should the wire be bent? 4. Find the critical values of your...
Suppose that you want to add items to an array such that the items are always...
Suppose that you want to add items to an array such that the items are always ordered in ascending order; e.g., [1, 2, 2, 4, 5, 8, 9, 14, 32], and any duplicate values are adjacent to each other. We haven’t talked about sorting algorithms yet, so assume you want to be able to keep the items in the array in order without having to sort them. So for example, suppose you want to add the integer value 7 to...
Please answer the following Case analysis questions 1-How is New Balance performing compared to its primary...
Please answer the following Case analysis questions 1-How is New Balance performing compared to its primary rivals? How will the acquisition of Reebok by Adidas impact the structure of the athletic shoe industry? Is this likely to be favorable or unfavorable for New Balance? 2- What issues does New Balance management need to address? 3-What recommendations would you make to New Balance Management? What does New Balance need to do to continue to be successful? Should management continue to invest...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT