Editing
2019 Dec Gold Problem 1 Milk Pumping
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=969 Milk Pumping] Problem Statement: Milk Pumping Farmer John has N (1 <= N <= 1,000) cows, conveniently numbered 1..N. He needs to pump milk from each of them. The cows are arranged in a line, and it takes one minute to pump milk from a cow. However, if two cows are adjacent, it takes only 30 seconds to pump milk from both of them simultaneously. Help Farmer John determine the minimum amount of time it will take to pump milk from all of his cows. Solution: The solution to this problem is to use a greedy algorithm. We start at the first cow and check if the next cow is adjacent. If so, we pump milk from both cows simultaneously, which takes 30 seconds. If not, we pump milk from the current cow, which takes one minute. We continue this process until all cows have been milked. The following C++ code implements this algorithm: int N; // number of cows int time = 0; // total time to pump milk for (int i = 0; i < N; i++) { if (i < N-1 && cows[i+1] - cows[i] == 1) { // adjacent cows, pump milk from both in 30 seconds time += 30; } else { // non-adjacent cows, pump milk from current cow in one minute time += 60; } } // Output the total time cout << time << endl; [[Category:Yearly_2019_2020]] [[Category:Gold]] [[Category:Graph]] [[Category:Dijkstra's Algorithm]] [[Category:Shortest Path]] [[Category:Binary Search]]
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