Editing
2015 Dec Gold Problem 2 Fruit Feast
(section)
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Solution == We use dynamic programming to solve this problem. Let dp[i][j] be the maximum tastiness that Bessie can achieve after consuming fruits with a total tastiness of i and taking j naps (0 if no nap is taken, 1 if a nap is taken). We have: dp[i][0] = max(dp[i-A][0], dp[i-B][0]) if i-A >= 0 or i-B >= 0 dp[i][1] = max(dp[i][1], dp[i//2][0] if i//2 >= 0) The first equation represents consuming an apple or an orange without taking a nap, while the second equation represents taking a nap after consuming some fruits. Finally, the maximum tastiness that Bessie can achieve is max(dp[i][1] for i in range(T+1)). Note that to save memory, we can use a rolling array and compute dp[i] in non-decreasing order of i. The time complexity is O(T), and the space complexity is O(T) without optimizations, or O(1) with a rolling array.
Summary:
Please note that all contributions to Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
My wiki:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Create account
Log in
Namespaces
Page
Discussion
English
Views
Read
Edit
View history
More
Search
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Tools
What links here
Related changes
Special pages
Page information