Skip to content

Commit

Permalink
Merge pull request #5040 from JoltedCowIceCream/patch-7
Browse files Browse the repository at this point in the history
Berry Picking Editorial
  • Loading branch information
SansPapyrus683 authored Jan 17, 2025
2 parents e1b2412 + 37bf9de commit 00a8002
Showing 1 changed file with 26 additions and 14 deletions.
40 changes: 26 additions & 14 deletions solutions/silver/usaco-990.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,42 @@
id: usaco-990
source: USACO Silver 2020 January
title: Berry Picking
author: Qi Wang, Daniel Suh, Juheon Rhee
author: Qi Wang, Daniel Suh, Juheon Rhee, David Guo
---

<Spoiler title="Hint 1">

Let's say Bessie and Elsie get the same number of berries.
What's a case that give us this?
How can we increase this value?
What if you fixed the number of berries $b$ in each bucket? How would you allocate the berries to maximize the number of buckets filled with exactly $b$ berries? How would you fill the remaining buckets that cannot be filled with exactly $b$ berries?

</Spoiler>

<Spoiler title="Solution">

[Official Analysis (C++)](http://www.usaco.org/current/data/sol_berries_silver_jan20.html)

## Explanation

Let $b$ represent the minimum number of berries in any bucket that Elsie receives.
We can assume WLOG that all of Elsie's buckets contain exactly $b$ berries,
as any configuration where Elsie's buckets are unequal can be adjusted to make all buckets contain exactly $b$,
preserving or improving the minimum value $b$.

Our goal is to maximize the total number of berries placed into $K$ buckets,
each containing at most $b$ berries, such that at least $\frac{K}{2}$ buckets contain exactly $b$ berries.

Now, consider the allocation of berries from a single tree in the optimal solution.
There is no benefit to creating multiple buckets with fewer than $b$ berries from the same tree.
Therefore, apart from at most one bucket, all other buckets filled with berries from this tree will contain exactly $b$ berries.

The optimal approach is to repeatedly fill buckets with exactly $b$ berries until either all $K$ buckets are filled
or the tree no longer has at least $b$ berries remaining.
If there are still buckets to fill after this, sort the remaining trees by the value $B_i$ $mod$ $b$,
where $B_i$ is the number of berries on tree $i$.
hen, iterate through the trees in descending order of $B_i$ $mod$ $b$,
filling the remaining buckets as efficiently as possible.

This process can be repeated for each possible value of $b$ from $1$ to $\max(B_i)$.

## Implementation 1

**Time Complexity:** $\mathcal{O}(max(B_i) \cdot N\log N)$
Expand All @@ -28,16 +49,7 @@ How can we increase this value?
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;

#define mpa make_pair
#define pb push_back
#define ins insert
#define f first
#define s second
#define all(x) x.begin(), x.end()
#define nl "\n"

void fileIO(string filename) {
freopen((filename + ".in").c_str(), "r", stdin);
Expand Down Expand Up @@ -81,7 +93,7 @@ void solve() {
for (int j = 0; j < N && j + amount < K; j++) { cur += A[j] % i; }
mx = max(mx, cur);
}
cout << mx << nl;
cout << mx << "\n";
}

int main() {
Expand Down

0 comments on commit 00a8002

Please sign in to comment.