Skip to content

Commit 2eb0816

Browse files
Merge pull request #3211 from saa938/master
added python code
2 parents 8754b59 + b39a473 commit 2eb0816

File tree

1 file changed

+42
-20
lines changed

1 file changed

+42
-20
lines changed

solutions/usaco-104.mdx

+42-20
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@ title: Haybale Stacking
55
author: Óscar Garries, Brad Ma
66
---
77

8-
[Official Analysis](http://www.usaco.org/current/data/sol_stacking.html)
8+
[Official Analysis (Java)](http://www.usaco.org/current/data/sol_stacking.html)
9+
10+
## Implementation
11+
12+
**Time Complexity:** $\mathcal{O}(N\log N + K)$
913

1014
<LanguageSection>
1115
<CPPSection>
1216

13-
## C++ Implementation
14-
1517
```cpp
1618
#include <bits/stdc++.h>
1719

@@ -21,47 +23,46 @@ int main () {
2123
int n, k;
2224
cin >> n >> k;
2325
vector<int> diff(n + 1);
24-
for (int i = 0; i < k; i++) { // read input and build difference array
26+
// read input and build difference array
27+
for (int i = 0; i < k; i++) {
2528
int l, r;
2629
cin >> l >> r;
27-
l--; // make zero-indexed
30+
l--; // make zero-indexed
2831
r--;
2932
diff[l]++;
3033
diff[r + 1]--;
3134
}
3235

3336
int sol[1000000];
3437
int tot = 0;
35-
for (int i = 0; i < n; i++) { // build prefix sum array
38+
for (int i = 0; i < n; i++) { // build prefix sum array
3639
tot += diff[i];
3740
sol[i] = tot;
3841
}
39-
sort (sol, sol + n); // sort to get median
40-
cout << sol[n / 2] << '\n'; // output the median
42+
sort (sol, sol + n); // sort to get median
43+
cout << sol[n / 2] << '\n'; // output the median
4144
}
4245
```
4346

4447
</CPPSection>
45-
4648
<JavaSection>
4749

48-
## Java Implementation
49-
5050
```java
5151
import java.util.*;
5252
import java.io.*;
5353

54-
class haybaleStacking {
54+
class HaybaleStacking {
5555
public static void main (String[] args) {
5656
Kattio io = new Kattio();
5757

5858
int n = io.nextInt();
5959
int k = io.nextInt();
60-
int[] differenceArray = new int[n + 1];
60+
6161
// differenceArray[n] is the difference between A[n + 1] and A[n]
62-
63-
for (int x = 0; x < k; x++) { // read input and build differenceArray
64-
int a = io.nextInt() - 1; // read and make zero-indexed
62+
int[] differenceArray = new int[n + 1];
63+
// read input and build differenceArray
64+
for (int x = 0; x < k; x++) {
65+
int a = io.nextInt() - 1; // read and make zero-indexed
6566
int b = io.nextInt() - 1;
6667

6768
differenceArray[a]++;
@@ -70,8 +71,7 @@ class haybaleStacking {
7071

7172
int[] prefixSum = new int[n + 1];
7273
int total = 0;
73-
74-
for (int x = 0; x < n; x++) { // build prefixSum array
74+
for (int x = 0; x < n; x++) { // build prefixSum array
7575
total += differenceArray[x];
7676
prefixSum[x] = total;
7777
}
@@ -81,11 +81,33 @@ class haybaleStacking {
8181
io.close();
8282
}
8383

84-
// CodeSnip{Kattio}
84+
//CodeSnip{Kattio}
8585
}
8686
```
8787

8888
</JavaSection>
89+
<PySection>
90+
91+
```py
92+
n, k = map(int, input().split())
93+
differences = [0] * (n + 1) # make a difference array
94+
for i in range(k):
95+
start, end = map(int, input().split())
96+
# add one to the start
97+
differences[start - 1] += 1
98+
# subtract one from the next number outside of start - end
99+
differences[end] -= 1
100+
101+
pref = [0] * n # prefix sums array
102+
s = 0
103+
# uses differences and total sum to get where all the haybales are
104+
for i in range(n):
105+
pref[i] = differences[i] + s
106+
s += differences[i]
107+
pref.sort()
108+
109+
print(pref[n // 2])
110+
```
89111
112+
</PySection>
90113
</LanguageSection>
91-

0 commit comments

Comments
 (0)