USACO Bronze 2022 January - Drought
Authors: Chongtian Ma, Juheon Rhee
Explanation
Let's go through the cows from left to right, making sure that all the cows we've processed have the same hunger value. Thus, we can break this problem up into cases:
Suppose the cows are numbered from to , and we are currently looking at cow . If cow has a greater hunger value, then it is sufficient to decrease cow to the same hunger value as cow . To do this we will pick the pair of cows and and decrease their hungers until cow has the same hunger value as cow . Of course, if cow ends up with a negative hunger value, then it is impossible and we output .
Now suppose cow has a greater hunger value than cow . We obviously cannot decrease the hunger for cow any further since it will never be equal - we only can decrease the hunger for all the cows before . To do this we select adjacent pairs of cows starting with the odd numbered cows and decrease their hunger values together (cows and , cows and , , cows and ).
The following sequence of feeding shows how this strategy can be applied:
5 5 5 5 3 ... 4 4 4 4 3 ... 3 3 3 3 3 ...
After this, all the cows before will be decreased to the same hunger value as cow . However, when is odd, it is impossible to decrease it's hunger unless we also decrease the hunger value for cow , so we output .
Implementation
Time Complexity:
C++
#include <bits/stdc++.h>using namespace std;using ll = long long;void solve() {int n;cin >> n;vector<ll> hunger(n + 1); // hunger for each cow, (1-indexed)for (int i = 1; i <= n; i++) { cin >> hunger[i]; }
Python
from typing import Listdef check(same_lvl: int, corn: List[int], hunger_lvl: List[int]) -> bool:# check if proposed corn is achievablehunger_lvl[0] -= corn[0]hunger_lvl[-1] -= corn[-1]for i in range(1, len(hunger_lvl) - 1):sum_of_corn = corn[i - 1] + corn[i]
Join the USACO Forum!
Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!