diff --git a/solutions/silver/ccc-Firehose.mdx b/solutions/silver/ccc-Firehose.mdx index 0954c00909..9fee7c6bbc 100644 --- a/solutions/silver/ccc-Firehose.mdx +++ b/solutions/silver/ccc-Firehose.mdx @@ -16,8 +16,8 @@ We use a greedy algorithm to find $\texttt{possible}(i)$. Firstly, we want to so **Time Complexity**: $\mathcal O(h^2\times log(\texttt{max}(H_i)))$ - + ```cpp #include using namespace std; @@ -71,7 +71,7 @@ int main() { // Binary search for the lowest hose length int l = 0; int r = STREET_SIZE; - int ans = 0; + int ans = -1; while (l <= r) { int m = (l + r) / 2; if (possible(m)) { @@ -82,9 +82,143 @@ int main() { } } - cout << ans << "\n"; + cout << ans << endl; } ``` + + + +```java +import java.io.*; +import java.util.*; + +public class Firehose { + private static final int MAX_H = 1000; + private static final int STREET_SIZE = 1000000; + + private static boolean possible(int length, int numHydrants, int[] houses) { + for (int i = 0; i < houses.length; i++) { + int needed = 0; // Number of fire hydrants that are needed. + // The house that we need to connect a fire hydrant to. + int start = houses[i]; + for (int j = 1; j < houses.length; j++) { + // Address of house at index j. + int end = houses[(i + j) % houses.length]; + /* + * If the distance between the start and end houses is greater than + * two times the length of the hose, a single fire hydrant will not + * be enough to cover both houses. + */ + int dist = (end - start + STREET_SIZE) % STREET_SIZE; + if (dist > 2 * length) { + start = end; + needed++; + } + } + + // Increment needed as we need another hydrant to reach the last houses. + needed++; + + if (needed <= numHydrants) { return true; } + } + return false; + } + + public static void main(String[] args) { + Kattio io = new Kattio(); + + int numHouses = io.nextInt(); + int[] houses = new int[numHouses]; + + for (int i = 0; i < numHouses; i++) { houses[i] = io.nextInt(); } + + int numHydrants = io.nextInt(); + + Arrays.sort(houses); + + // Binary search for the lowest hose length + int left = 0; + int right = STREET_SIZE; + int ans = -1; + while (left <= right) { + int mid = (left + right) / 2; + if (possible(mid, numHydrants, houses)) { + right = mid - 1; + ans = mid; + } else { + left = mid + 1; + } + } + + System.out.println(ans); + io.close(); + } + + // CodeSnip{Kattio} +} +``` + + + + +```py +MAX_H = 1000 +STREET_SIZE = 1000000 + + +def possible(length: int, num_hydrants: int, houses: list) -> bool: + for i in range(len(houses)): + # Number of fire hydrants that are needed. + needed = 0 + # The house that we need to connect a fire hydrant to. + start = houses[i] + for j in range(1, len(houses)): + # Address of house at index j. + end = houses[(i + j) % len(houses)] + + """ + If the distance between the start and end houses is greater than + two times the length of the hose, a single fire hydrant will not + be enough to cover both houses. + """ + dist = (end - start + STREET_SIZE) % STREET_SIZE + if dist > 2 * length: + start = end + needed += 1 + + # Increment needed as we need another hydrant to reach the last houses. + needed += 1 + + if needed <= num_hydrants: + return True + + return False + + +num_houses = int(input()) +houses = [] +for _ in range(num_houses): + houses.append(int(input())) + +num_hydrants = int(input()) + +houses.sort() + +# Binary search for the lowest hose length +l = 0 +r = STREET_SIZE +ans = -1 +while l <= r: + m = (l + r) // 2 + if possible(m, num_hydrants, houses): + r = m - 1 + ans = m + else: + l = m + 1 + +print(ans) +``` +