Editing
2020 Jan Silver Problem 2 Loan Repayment
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!
== Official Problem Statement == [http://www.usaco.org/index.php?page=viewproblem2&cpid=991 Loan Repayment] Problem Statement: USACO Contest 991: Loan Repayment You are given a loan of N dollars, and you must repay it over the course of M months. Each month, you must pay a certain amount of money (A_i) towards the loan. You can choose to pay more than the required amount each month, but you cannot pay less. At the end of each month, the remaining loan balance is reduced by the amount you paid. If the remaining balance is less than the amount you paid, the extra money is not returned to you. Your task is to determine the minimum amount of money you need to pay each month in order to pay off the loan in M months. Solution: We can solve this problem using dynamic programming. We create an array dp[i] that stores the minimum amount of money we need to pay in order to pay off the loan in i months. We start with dp[0] = N, since that is the initial loan amount. Then, for each month i, we calculate dp[i] = min(dp[i-1], A_i) + (N - dp[i-1]). This is because we need to pay the minimum amount of money in order to pay off the loan in i months, which is either the amount we paid in the previous month (dp[i-1]) or the amount required for this month (A_i). We then add the remaining loan balance (N - dp[i-1]) to get the total amount we need to pay in this month. Finally, we return dp[M], which is the minimum amount of money we need to pay each month in order to pay off the loan in M months. <pre> int dp[M+1]; dp[0] = N; for (int i = 1; i <= M; i++) { dp[i] = min(dp[i-1], A_i) + (N - dp[i-1]); } return dp[M]; </pre> [[Category:Yearly_2019_2020]] [[Category:Silver]] [[Category:Binary Search]] [[Category:Greedy]] [[Category:Simulation]]
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