Explore Westonci.ca, the premier Q&A site that helps you find precise answers to your questions, no matter the topic. Explore in-depth answers to your questions from a knowledgeable community of experts across different fields. Discover in-depth answers to your questions from a wide network of professionals on our user-friendly Q&A platform.
Sagot :
Answer:
This can be done by a greedy solution. The algorithm does the following:
Put song 1 on CD1.
For song 1, if there's space left on the present CD, then put the song on the present CD. If not, use a replacement CD.
If there are not any CDs left, output "no solution".
Explanation:
The main thing is prove the correctness, do that by the "greedy stays ahead argument". For the primary song, the greedy solution is perfect trivially.
Now, let the optimal solution match the greedy solution upto song i. Then, if the present CD has space and optimal solution puts song (i+1) on an equivalent CD, then the greedy and optimal match, hence greedy is not any worse than the optimal.Else, optimal puts song (i + 1) on subsequent CD. Consider an answer during which only song (i + 1) is moved to the present CD and zip else is modified. Clearly this is often another valid solution and no worse than the optimal, hence greedy is not any worse than the optimal solution during this case either. This proves the correctness of the algorithm. As for every song, there are constantly many operations to try to do, the complexity is O(n).
Thank you for choosing our platform. We're dedicated to providing the best answers for all your questions. Visit us again. Your visit means a lot to us. Don't hesitate to return for more reliable answers to any questions you may have. We're here to help at Westonci.ca. Keep visiting for the best answers to your questions.