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

Python Sol 1063 content/3_Silver/Sorting_Custom.mdx #4899

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
65 changes: 54 additions & 11 deletions content/3_Silver/Sorting_Custom.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -821,13 +821,58 @@ public class RectangularPasture {
// EndCodeSnip
```

The solution uses a
[lambda](https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html)
function as the custom comparator, which our guide didn't discuss, but it should
be apparent which coordinate (`x` or `y`) that the comparator is sorting by.

</JavaSection>

<PySection>

```py
# BeginCodeSnip{Solution code}
import sys

input = sys.stdin.readline
# EndCodeSnip

n = int(input().strip())
points = [list(map(int, input().strip().split())) for _ in range(n)]

points.sort(key=lambda i: i[1])
for i in range(n):
points[i][1] = i + 1

points.sort(key=lambda i: i[0])
for i in range(n):
points[i][0] = i + 1

# BeginCodeSnip{Solution code}
psa = [[0 for _ in range(n + 1)] for _ in range(n + 1)]
for x, y in points:
psa[x][y] = 1

for x in range(1, n + 1):
for y in range(1, n + 1):
psa[x][y] += psa[x - 1][y] + psa[x][y - 1] - psa[x - 1][y - 1]

ans = n + 1

for s in range(n):
for e in range(s + 1, n):
srt, end = points[s][0], points[e][0]
top = max(points[s][1], points[e][1])
bottom = min(points[s][1], points[e][1])

above = psa[end][n] - psa[srt - 1][n] - psa[end][top] + psa[srt - 1][top]
below = psa[end][bottom - 1] - psa[srt - 1][bottom - 1]
ans += (above + 1) * (below + 1)

print(ans)

# Note: This solution does not sucessfully pass all test cases on USACO even
# with the correct time complexity of O(n^2) since Python is too slow.
# EndCodeSnip
```

</PySection>

</LanguageSection>

The solution to Rectangular Pasture directly replaces coordinates with their
Expand Down Expand Up @@ -858,12 +903,10 @@ We also provide a more detailed explanation:

First of all, let's figure out the value at each index in array $a$. It would be
too slow to loop through every index in every update interval as well as every
index in a query interval
(The complexity would be $O(C(N+Q))$,
where $C$ is the maximum coordinate given in the input).
At the same time, that approach would take $O(C)$ memory, which is also too much.
Instead, we make an observation
that will make prefix sums paired with coordinate compression a viable option.
index in a query interval (The complexity would be $O(C(N+Q))$, where $C$ is the
maximum coordinate given in the input). At the same time, that approach would
take $O(C)$ memory, which is also too much. Instead, we make an observation that
will make prefix sums paired with coordinate compression a viable option.

Not every index in the whole range from $1...10^9$ is used in updates. In fact,
there can be large intervals of indices that all have the same value. Call every
Expand Down
Loading