2019 Dec Gold Problem 2 Milk Visits: Difference between revisions

From Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
 
Line 1: Line 1:
== Official Problem Statement ==
[http://www.usaco.org/index.php?page=viewproblem2&cpid=970 Milk Visits]
Problem Statement:
Problem Statement:



Latest revision as of 23:00, 11 June 2023

Official Problem Statement[edit]

Milk Visits

Problem Statement:

Milk Visits[edit]

Farmer John has N cows (1 ≤ N ≤ 100,000), conveniently numbered 1..N, and M visits (1 ≤ M ≤ 100,000) from the milk truck. Each visit involves the milk truck visiting a single cow, taking away some milk, and then moving on to the next cow.

The milk truck visits the cows in a sequence, visiting cow i immediately after cow i-1 for each i from 2 to M. The milk truck never visits the same cow twice.

Solution[edit]

The solution to this problem is to use a dynamic programming approach. We can create an array A of size N+1, where A[i] represents the amount of milk taken away from cow i. We can then iterate through the visits and update A[i] accordingly.

For example, in C++:

int N, M; int A[N+1];

for (int i = 1; i <= M; i++) {

   int cow;
   cin >> cow;
   A[cow]++;

}

At the end of the loop, A[i] will contain the total amount of milk taken away from cow i.