Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Berry Picking Editorial #5040

Merged
merged 8 commits into from
Jan 17, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading