Skip to content

Commit

Permalink
Merge pull request #4906 from freakin23/sols
Browse files Browse the repository at this point in the history
py sol for Stalling
  • Loading branch information
SansPapyrus683 authored Nov 6, 2024
2 parents e3ee04f + d13397b commit 835a3a1
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
30 changes: 24 additions & 6 deletions solutions/gold/usaco-1085.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ author: Ananth Kashyap
**Time Complexity:** $\mathcal{O}(N^2)$

<LanguageSection>

<PySection>

```py
Expand Down Expand Up @@ -44,20 +43,18 @@ print(possibilities)
```

</PySection>

</LanguageSection>

## Solution 2

Instead of iterating through $\text{stalls}$ every time, we can use [two
pointers](/silver/two-pointers).
Instead of iterating through $\text{stalls}$ every time, we can use
[two pointers](/silver/two-pointers).

## Implementation

**Time Complexity:** $\mathcal{O}(N \log N)$

<LanguageSection>

<CPPSection>

```cpp
Expand All @@ -80,7 +77,6 @@ int main() {
int j = n - 1;
for (int i = n - 1; i >= 1; i--) {
while (j >= 0 && stalls[j] >= cows[i]) { j--; }

possibilities *= i - j;
}

Expand All @@ -89,5 +85,27 @@ int main() {
```

</CPPSection>
<PySection>

```py
n = int(input())

cows = list(map(int, input().split()))
stalls = list(map(int, input().split()))

cows.sort()
stalls.sort()

possibilities = 1
j = n - 1
for i in range(n - 1, 0, -1):
while j >= 0 and stalls[j] >= cows[i]:
j -= 1

possibilities *= i - j

print(possibilities)
```
</PySection>
</LanguageSection>
5 changes: 4 additions & 1 deletion solutions/gold/usaco-673.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ author: Ryan Chou

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

## Explanation

Let's say that we want to find the answer for some subsection of the sample input. Then, we would need the number of elements to consider for Farmer John and Farmer Paul, and the size of the team. A naive approach would be to go through these in $NMK$ time, and due to the low bounds on $N$, $M$, and $K$, this'll actually run in time. So, we'll just run a bottom-up DP where

$\texttt{dp}[i][j][k]$ = the number of teams of size $k$ after we've considered the first $i$ cows in Farmer John's team, the first $j$ cows in Farmer Paul's team.
Expand Down Expand Up @@ -82,8 +84,8 @@ int main() {
std::cout << dp[n][m][k] << std::endl;
}
```
</CPPSection>
</CPPSection>
<JavaSection>
```java
Expand Down Expand Up @@ -144,5 +146,6 @@ public class Team {
// CodeSnip{Kattio}
}
```
</JavaSection>
</LanguageSection>

0 comments on commit 835a3a1

Please sign in to comment.