|
1 | 1 | ---
|
2 | 2 | id: usaco-715
|
3 | 3 | title: USACO Feb 2017 Silver - Why Did the Cow Cross the Road II
|
4 |
| -author: Mrinall Umasudhan |
| 4 | +author: Mrinall Umasudhan, Shourya Bansal |
5 | 5 | ---
|
6 | 6 |
|
7 |
| -Official Editorial: http://www.usaco.org/index.php?page=viewproblem2&cpid=715 |
| 7 | +[Original Problem](http://www.usaco.org/index.php?page=viewproblem2&cpid=715) |
8 | 8 |
|
9 |
| -### Implementation |
| 9 | +[Official Analysis](http://www.usaco.org/current/data/sol_maxcross_silver_feb17.html) |
10 | 10 |
|
11 | 11 | <LanguageSection>
|
| 12 | +<CPPSection> |
| 13 | + |
| 14 | +## C++ Implementation |
| 15 | + |
| 16 | +```cpp |
| 17 | +#include <bits/stdc++.h> |
| 18 | +using namespace std; |
| 19 | + |
| 20 | +int main() { |
| 21 | + freopen("maxcross.in", "r", stdin); |
| 22 | + freopen("maxcross.out", "w", stdout); |
| 23 | + |
| 24 | + int N, K, B; |
| 25 | + cin >> N >> K >> B; |
| 26 | + |
| 27 | + // Use vector because it defaults to 0 |
| 28 | + vector<bool> status(N + 1); |
| 29 | + |
| 30 | + // Converts all broken lights to 1 in vector |
| 31 | + for (int i = 0; i < B; i++) { |
| 32 | + int broken; |
| 33 | + cin >> broken; |
| 34 | + |
| 35 | + status[broken] = 1; |
| 36 | + } |
| 37 | + |
| 38 | + // Calculates the Prefix Sum of the broken lights |
| 39 | + vector<int> prefix(N + 1); |
| 40 | + for (int i = 1; i <= N; i++) { |
| 41 | + prefix[i] = prefix[i - 1] + status[i]; |
| 42 | + } |
| 43 | + |
| 44 | + // Finds a range of K traffic lights with the minimum broken ones |
| 45 | + int broken = K; |
| 46 | + for (int i = K; i <= N; i++) { |
| 47 | + broken = min(broken, prefix[i] - prefix[i - K]); |
| 48 | + } |
| 49 | + |
| 50 | + cout << broken << '\n'; |
| 51 | + return 0; |
| 52 | +} |
| 53 | +``` |
| 54 | + |
| 55 | +</CPPSection> |
12 | 56 | <JavaSection>
|
13 | 57 |
|
| 58 | +## Java Implementation |
| 59 | + |
14 | 60 | ```java
|
15 | 61 | import java.io.*;
|
16 | 62 | import java.util.*;
|
@@ -91,5 +137,3 @@ public class maxcross {
|
91 | 137 |
|
92 | 138 | </JavaSection>
|
93 | 139 | </LanguageSection>
|
94 |
| - |
95 |
| - |
|
0 commit comments