Skip to content

Commit 079f14a

Browse files
authored
Merge pull request #364 from Mastermind497/master
content: add cpp implementation to feb17-maxcross
2 parents 39697c7 + 0a57272 commit 079f14a

File tree

1 file changed

+49
-5
lines changed

1 file changed

+49
-5
lines changed

solutions/USACO feb17-maxcross.mdx

+49-5
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,62 @@
11
---
22
id: usaco-715
33
title: USACO Feb 2017 Silver - Why Did the Cow Cross the Road II
4-
author: Mrinall Umasudhan
4+
author: Mrinall Umasudhan, Shourya Bansal
55
---
66

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)
88

9-
### Implementation
9+
[Official Analysis](http://www.usaco.org/current/data/sol_maxcross_silver_feb17.html)
1010

1111
<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>
1256
<JavaSection>
1357

58+
## Java Implementation
59+
1460
```java
1561
import java.io.*;
1662
import java.util.*;
@@ -91,5 +137,3 @@ public class maxcross {
91137

92138
</JavaSection>
93139
</LanguageSection>
94-
95-

0 commit comments

Comments
 (0)