Skip to content

Commit a20f924

Browse files
Merge pull request #4323 from IDON-TEXIST/master
Hints for haybale stacking and painting the barn gold
2 parents 092a3db + b376e22 commit a20f924

File tree

3 files changed

+96
-44
lines changed

3 files changed

+96
-44
lines changed

content/3_Silver/Prefix_Sums.problems.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@
9797
"isStarred": true,
9898
"tags": ["Prefix Sums"],
9999
"solutionMetadata": {
100-
"kind": "internal"
100+
"kind": "internal",
101+
"hasHints": true
101102
}
102103
},
103104
{

solutions/silver/usaco-104.mdx

+14-1
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,20 @@
22
id: usaco-104
33
source: USACO Bronze 2012 January
44
title: Haybale Stacking
5-
author: Óscar Garries, Brad Ma
5+
author: Óscar Garries, Brad Ma, KJ Karaisz
66
---
77

8+
<Spoiler title="Hint">
9+
10+
Prefix sums consists of an $\mathcal{O}(N)$ preprocess, followed by $\mathcal{O}(1)$ queries.
11+
12+
The reverse of that would be doing $\mathcal{O}(1)$ *updates*, followed by an $\mathcal{O}(N)$ *post*process.
13+
Try to figure out what that might look like in the context of this problem!
14+
15+
</Spoiler>
16+
17+
<Spoiler title="Solution">
18+
819
[Official Analysis (Java)](http://www.usaco.org/current/data/sol_stacking.html)
920

1021
## Implementation
@@ -111,3 +122,5 @@ print(pref[n // 2])
111122
112123
</PySection>
113124
</LanguageSection>
125+
126+
</Spoiler>

solutions/silver/usaco-923.mdx

+80-42
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,58 @@
22
id: usaco-923
33
source: USACO Gold 2019 February
44
title: Painting the Barn
5-
author: Kevin Sheng
5+
author: Kevin Sheng, KJ Karaisz
66
---
77

8+
<Warning title="Prior Knowledge">
9+
10+
This editorial assumes one already has a firm grasp of the
11+
[silver](http://www.usaco.org/current/data/sol_paintbarn_silver_feb19.html)
12+
version of this problem.
13+
14+
</Warning>
15+
16+
<Spoiler title="Hint 1">
17+
18+
Say FJ could only paint one rectangle of paint over the barn.
19+
Then, this problem would turn into finding the
20+
[maximum sum of a submatrix](https://stackoverflow.com/a/18220549/12128483).
21+
22+
</Spoiler>
23+
24+
<Spoiler title="Hint 2">
25+
26+
So you have your one rectangle now. How do you make this algorithm work for two?
27+
28+
Try to think about how you can guarantee disjoint rectangles!
29+
30+
</Spoiler>
31+
32+
<Spoiler title="Answer to Hint 2">
33+
34+
If two rectangles are disjoint, there can always be a horizontal or vertical line separating them.
35+
36+
</Spoiler>
37+
38+
<Spoiler title="Solution">
39+
840
[Official Analysis (C++)](http://www.usaco.org/current/data/sol_paintbarn_gold_feb19.html)
941

1042
## Explanation
1143

1244
### Initial Observations
1345

14-
**Note:** This solution assumes one has already had a firm grasp of the
15-
[silver](http://www.usaco.org/current/data/sol_paintbarn_silver_feb19.html)
16-
version of this problem.
17-
1846
We start off by calculating the layers of paint over each cell of the barn with
1947
prefix sums, but from there it's a bit hard to determine which layers we should
20-
paint. It's hard to directly calculate it from this array, but notice that there's
21-
two general states a cell can be in.
48+
paint. It's hard to directly calculate it from this array, but notice that
49+
there's two general states a cell can be in.
2250

23-
1. If it's painted over, it will either add $1$ to the optimal area
24-
(because it initially has $K-1$ layers of paint)
25-
or subtract $1$ from the optimal area
51+
1. If it's painted over, it will either add $1$ to the optimal area (because it
52+
initially has $K-1$ layers of paint) or subtract $1$ from the optimal area
2653
(because it already has $K$ layers of paint).
27-
3. Nothing will happen to the optimal area if it's painted over.
54+
2. Nothing will happen to the optimal area if it's painted over.
2855

29-
Since FJ must paint two *disjoint* rectangles, we don't have to consider the
56+
Since FJ must paint two _disjoint_ rectangles, we don't have to consider the
3057
cells that need two layers of paint to become optimal.
3158

3259
Let's call this array $\texttt{leftovers}$. With our example input, it would
@@ -52,14 +79,15 @@ $200\times200$ matrix):
5279
### Using Kadane's
5380

5481
Now, we've reduced this problem to finding the max submatrix sum on a matrix,
55-
given that we can have two disjoint submatrices.
56-
Though it's a tall talk, let's try and tackle this bit by bit, first by considering
57-
if there's only *1* matrix we can paint.
82+
given that we can have two disjoint submatrices. Though it's a tall task, let's
83+
try and tackle this bit by bit, first by considering if there's only _1_ matrix
84+
we can paint.
5885

5986
To do that, we use a 2D version of
6087
[Kadane's Algorithm](https://en.wikipedia.org/wiki/Maximum_subarray_problem#Kadane's_algorithm).
6188

6289
The $\mathcal{O}(N)$ 1D version can be implemented by the following code:
90+
6391
<LanguageSection>
6492
<CPPSection>
6593

@@ -106,49 +134,58 @@ def max_subarray(arr: List[int]) -> int:
106134
</PySection>
107135
</LanguageSection>
108136

109-
We can extend this to the second dimension
110-
(albeit with an extra $N^2$ factor in the complexity)
111-
by iterating through all ranges of columns in the matrix and running a 1D Kadane's
112-
where the sum of each element is the subarray is the sum of the column range of a row.
137+
We can extend this to the second dimension (albeit with an extra $N^2$ factor in
138+
the complexity) by iterating through all ranges of columns in the matrix and
139+
running a 1D Kadane's where the sum of each element is the subarray is the sum
140+
of the column range of a row.
141+
142+
For example, if our current column range was $[0,2]$, then we'd run our 1D
143+
Kadane's on the following array:
113144

114-
For example, if our current column range was $[0,2]$, then we'd run our 1D Kadane's
115-
on the following array:
116145
$$
117146
[0,2,0,0,1,1,1,0,0,0]
118147
$$
119148

120149
### Solving the Second Matrix
121150

122151
The key to getting the max with two submatrices is noticing that there will
123-
always be a line, vertical or horizontal, that can divide the grid such that
124-
one rectangle is in one section and the other rectangle is in the other section.
152+
always be a line, vertical or horizontal, that can divide the grid such that one
153+
rectangle is in one section and the other rectangle is in the other section.
125154

126155
Building on this observation, we can go through all the lines we can draw
127-
through the matrix and get the max subarray on both sides.
128-
After this, we can take the max of the results of these lines and add it
129-
to the number of cells that were initially painted with the optimal number
130-
of layers of paint to get our final answer.
156+
through the matrix and get the max subarray on both sides. After this, we can
157+
take the max of the results of these lines and add it to the number of cells
158+
that were initially painted with the optimal number of layers of paint to get
159+
our final answer.
131160

132161
We need a method to efficiently calculate the max submatrices for all the split
133-
regions, though.
134-
To start off, let's define four arrays which will have the following
135-
***initial*** values:
136-
137-
* $\texttt{top\_best[i]}$: the best matrix whose ***lower*** side is the $i$th row.
138-
* $\texttt{bottom\_best[i]}$: the best matrix whose ***upper*** side is the $i$th row.
139-
* $\texttt{left\_best[i]}$: the best matrix whose ***right*** side is the $i$th column.
140-
* $\texttt{right\_best[i]}$: the best matrix whose ***left*** side is the $i$th column.
162+
regions, though. To start off, let's define four arrays which will have the
163+
following **_initial_** values:
164+
165+
- $\texttt{top\_best[i]}$: the best matrix whose **_lower_** side is the $i$th
166+
row.
167+
- $\texttt{bottom\_best[i]}$: the best matrix whose **_upper_** side is the
168+
$i$th row.
169+
- $\texttt{left\_best[i]}$: the best matrix whose **_right_** side is the $i$th
170+
column.
171+
- $\texttt{right\_best[i]}$: the best matrix whose **_left_** side is the $i$th
172+
column.
141173

142174
We fill these arrays by running a 2D Kadane's starting from the top, bottom,
143-
left, or right, and updating the corresponding arrays with the max submatrix value.
175+
left, or right, and updating the corresponding arrays with the max submatrix
176+
value.
144177

145-
Next, we do a cumulative maximum from the start or from the end depending on the array
146-
to get the arrays with the following updated values:
178+
Next, we do a cumulative maximum from the start or from the end depending on the
179+
array to get the arrays with the following updated values:
147180

148-
* $\texttt{top\_best[i]}$: the best matrix that lies ***at or above*** the $i$th row.
149-
* $\texttt{bottom\_best[i]}$: the best matrix that lies ***at or below*** $i$th row.
150-
* $\texttt{left\_best[i]}$: the best matrix that's ***at or to the left of*** the $i$th column.
151-
* $\texttt{right\_best[i]}$: the best matrix that's ***at or to the right of*** the $i$th column.
181+
- $\texttt{top\_best[i]}$: the best matrix that lies **_at or above_** the $i$th
182+
row.
183+
- $\texttt{bottom\_best[i]}$: the best matrix that lies **_at or below_** $i$th
184+
row.
185+
- $\texttt{left\_best[i]}$: the best matrix that's **_at or to the left of_**
186+
the $i$th column.
187+
- $\texttt{right\_best[i]}$: the best matrix that's **_at or to the right of_**
188+
the $i$th column.
152189

153190
## Implementation
154191

@@ -407,3 +444,4 @@ public final class PaintBarn {
407444
408445
</JavaSection>
409446
</LanguageSection>
447+
</Spoiler>

0 commit comments

Comments
 (0)