@@ -5,13 +5,15 @@ title: Haybale Stacking
5
5
author : Óscar Garries, Brad Ma
6
6
---
7
7
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)$
9
13
10
14
<LanguageSection >
11
15
<CPPSection >
12
16
13
- ## C++ Implementation
14
-
15
17
``` cpp
16
18
#include < bits/stdc++.h>
17
19
@@ -21,47 +23,46 @@ int main () {
21
23
int n, k;
22
24
cin >> n >> k;
23
25
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++) {
25
28
int l , r ;
26
29
cin >> l >> r ;
27
- l -- ; // make zero-indexed
30
+ l -- ; // make zero-indexed
28
31
r -- ;
29
32
diff [l ]++ ;
30
33
diff [r + 1 ]-- ;
31
34
}
32
35
33
36
int sol[ 1000000] ;
34
37
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
36
39
tot += diff [i ];
37
40
sol [i ] = tot ;
38
41
}
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
41
44
}
42
45
```
43
46
44
47
</CPPSection>
45
-
46
48
<JavaSection>
47
49
48
- ## Java Implementation
49
-
50
50
```java
51
51
import java.util.*;
52
52
import java.io.*;
53
53
54
- class haybaleStacking {
54
+ class HaybaleStacking {
55
55
public static void main (String [] args ) {
56
56
Kattio io = new Kattio ();
57
57
58
58
int n = io .nextInt ();
59
59
int k = io .nextInt ();
60
- int[] differenceArray = new int[ n + 1 ];
60
+
61
61
// 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
65
66
int b = io.nextInt() - 1;
66
67
67
68
differenceArray[a ]++;
@@ -70,8 +71,7 @@ class haybaleStacking {
70
71
71
72
int[] prefixSum = new int[n + 1 ];
72
73
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
75
75
total += differenceArray[x ];
76
76
prefixSum[x ] = total;
77
77
}
@@ -81,11 +81,33 @@ class haybaleStacking {
81
81
io.close();
82
82
}
83
83
84
- // CodeSnip{Kattio}
84
+ // CodeSnip{Kattio}
85
85
}
86
86
```
87
87
88
88
</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
+ ` ` `
89
111
112
+ </PySection>
90
113
</LanguageSection>
91
-
0 commit comments