From 17dc26667e400df3c204083e31b42a37a6503e05 Mon Sep 17 00:00:00 2001 From: freakin23 Date: Thu, 21 Nov 2024 12:19:04 +0530 Subject: [PATCH 01/19] formatting --- solutions/silver/cf-702C.mdx | 3 --- 1 file changed, 3 deletions(-) diff --git a/solutions/silver/cf-702C.mdx b/solutions/silver/cf-702C.mdx index c28ebe2bda..7604edb530 100644 --- a/solutions/silver/cf-702C.mdx +++ b/solutions/silver/cf-702C.mdx @@ -12,7 +12,6 @@ author: Nathan Wang, Benjamin Qi, Maggie Liu, Brad Ma, George Pong **Time Complexity:** $\mathcal{O}(N \log N)$ - ```cpp @@ -71,7 +70,6 @@ int main() { ``` - ```java @@ -137,7 +135,6 @@ public class CellularNetwork { ``` - ## Solution 2 - Two Pointers From 6511c34c0b5ba07587afd05d1fca055983017931 Mon Sep 17 00:00:00 2001 From: freakin23 Date: Thu, 21 Nov 2024 12:20:10 +0530 Subject: [PATCH 02/19] py sol1 for cellular network --- solutions/silver/cf-702C.mdx | 45 ++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/solutions/silver/cf-702C.mdx b/solutions/silver/cf-702C.mdx index 7604edb530..b6f2587be5 100644 --- a/solutions/silver/cf-702C.mdx +++ b/solutions/silver/cf-702C.mdx @@ -135,6 +135,51 @@ public class CellularNetwork { ``` + + +```py +""" +returns the first index in the array that is >= value, or arr.size() +if no such index exists +""" + +def first_at_least(value: int) -> int: + lo = 0 + hi = len(towers) + while lo < hi: + mid = (lo + hi) // 2 + if towers[mid] > value: + hi = mid + else: + lo = mid + 1 + + return lo + + +n, m = map(int, input().split()) +cities = list(map(int, input().split())) +towers = list(map(int, input().split())) + +min_r = 0 +for i in range(n): + tower_right = first_at_least(cities[i]) + tower_left = tower_right - 1 + + min_r_for_this_city = float('inf') + if tower_right < m: + min_r_for_this_city = min(min_r_for_this_city, towers[tower_right] - cities[i]) + + if tower_left >= 0: + min_r_for_this_city = min(min_r_for_this_city, cities[i] - towers[tower_left]) + + + min_r = max(min_r, min_r_for_this_city) + +print(min_r) +``` + + + ## Solution 2 - Two Pointers From 09b42df6c2a1a5e0aa31af1600b0bc28e2bc9443 Mon Sep 17 00:00:00 2001 From: freakin23 Date: Thu, 21 Nov 2024 12:22:50 +0530 Subject: [PATCH 03/19] correct variable formatting --- solutions/silver/cf-702C.mdx | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/solutions/silver/cf-702C.mdx b/solutions/silver/cf-702C.mdx index b6f2587be5..3141eeb515 100644 --- a/solutions/silver/cf-702C.mdx +++ b/solutions/silver/cf-702C.mdx @@ -20,7 +20,7 @@ using namespace std; // returns the first index in the array that is >= value, or arr.size() if no // such index exists -int firstAtLeast(const vector &arr, int value) { +int first_at_least(const vector &arr, int value) { int lo = 0, hi = arr.size(); while (lo < hi) { int mid = (lo + hi) / 2; @@ -47,25 +47,25 @@ int main() { towers.push_back(tower); } - int minR = 0; + int min_r = 0; for (int i = 0; i < n; i++) { - int towerRight = firstAtLeast(towers, cities[i]); - int towerLeft = towerRight - 1; + int tower_right = first_at_least(towers, cities[i]); + int tower_left = tower_right - 1; - int minRForThisCity = 2e9; - if (towerRight < m) { - assert(towers[towerRight] >= cities[i]); - minRForThisCity = min(minRForThisCity, towers[towerRight] - cities[i]); + int min_r_for_this_city = 2e9; + if (tower_right < m) { + assert(towers[tower_right] >= cities[i]); + min_r_for_this_city = min(min_r_for_this_city, towers[tower_right] - cities[i]); } - if (towerLeft >= 0) { - assert(towers[towerLeft] <= cities[i]); - minRForThisCity = min(minRForThisCity, cities[i] - towers[towerLeft]); + if (tower_left >= 0) { + assert(towers[tower_left] <= cities[i]); + min_r_for_this_city = min(min_r_for_this_city, cities[i] - towers[tower_left]); } - minR = max(minR, minRForThisCity); + min_r = max(min_r, min_r_for_this_city); } - cout << minR << endl; + cout << min_r << endl; } ``` From 7b673301e73c34fe4e71a2e2ed0bccbe40786dfa Mon Sep 17 00:00:00 2001 From: freakin23 Date: Thu, 21 Nov 2024 12:23:18 +0530 Subject: [PATCH 04/19] formatting --- solutions/silver/cf-702C.mdx | 1 - 1 file changed, 1 deletion(-) diff --git a/solutions/silver/cf-702C.mdx b/solutions/silver/cf-702C.mdx index 3141eeb515..bb6dbac6d3 100644 --- a/solutions/silver/cf-702C.mdx +++ b/solutions/silver/cf-702C.mdx @@ -178,7 +178,6 @@ for i in range(n): print(min_r) ``` - From a990cf9ccfacfa36916f8c16b4a877955518696b Mon Sep 17 00:00:00 2001 From: freakin23 Date: Thu, 21 Nov 2024 12:44:48 +0530 Subject: [PATCH 05/19] correct TC --- solutions/silver/cf-702C.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/solutions/silver/cf-702C.mdx b/solutions/silver/cf-702C.mdx index bb6dbac6d3..e951e9bf11 100644 --- a/solutions/silver/cf-702C.mdx +++ b/solutions/silver/cf-702C.mdx @@ -360,7 +360,7 @@ left of the city. Define $\texttt{dist}$ to be the minimum of the distance to the tower on the right and the distance to the tower on the left. Then, set $r$ to be the maximum of itself and $\texttt{dist}$. -**Time Complexity:** $\mathcal{O}(N\log N)$ +**Time Complexity:** $\mathcal{O}((N + M)\log M)$ From 923d390bd72ea396885fcbb58a2d77d8ef80acb0 Mon Sep 17 00:00:00 2001 From: freakin23 Date: Thu, 21 Nov 2024 12:45:34 +0530 Subject: [PATCH 06/19] formatting --- solutions/silver/cf-702C.mdx | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/solutions/silver/cf-702C.mdx b/solutions/silver/cf-702C.mdx index e951e9bf11..a78b3e6137 100644 --- a/solutions/silver/cf-702C.mdx +++ b/solutions/silver/cf-702C.mdx @@ -190,7 +190,6 @@ We store two pointers, one for the current city and one for the current tower. W **Time Complexity:** $\mathcal{O}(N)$ - ```cpp @@ -243,7 +242,6 @@ int main() { ``` - ```py @@ -283,7 +281,6 @@ print(max_dist) ``` - ```java @@ -339,8 +336,8 @@ public class CellularNetwork2 { // CodeSnip{Kattio} } ``` - + ## Solution 3 - Using a Set @@ -363,7 +360,6 @@ to be the maximum of itself and $\texttt{dist}$. **Time Complexity:** $\mathcal{O}((N + M)\log M)$ - ```cpp @@ -406,7 +402,6 @@ int main() { ``` - ```java @@ -451,5 +446,4 @@ public class CellularNetwork { ``` - From e6bcbba98c9bdf30d8d3306df5bfeec3f2b2aab2 Mon Sep 17 00:00:00 2001 From: freakin23 Date: Thu, 21 Nov 2024 12:48:14 +0530 Subject: [PATCH 07/19] variable formatting --- solutions/silver/cf-702C.mdx | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/solutions/silver/cf-702C.mdx b/solutions/silver/cf-702C.mdx index a78b3e6137..cb659ca1b0 100644 --- a/solutions/silver/cf-702C.mdx +++ b/solutions/silver/cf-702C.mdx @@ -383,16 +383,16 @@ int main() { for (int i = 0; i < n; i++) { int dist = 2e9 + 1; // find closest tower to the right of the city - auto closesttower = towers.lower_bound(cities[i]); - if (closesttower != towers.end()) { + auto closest_tower = towers.lower_bound(cities[i]); + if (closest_tower != towers.end()) { // if a tower is found, update the distance - dist = *closesttower - cities[i]; + dist = *closest_tower - cities[i]; } // find closest tower to the left of the city - if (closesttower != towers.begin()) { - closesttower--; + if (closest_tower != towers.begin()) { + closest_tower--; // update dist with the minimum of the distances - dist = min(dist, cities[i] - *closesttower); + dist = min(dist, cities[i] - *closest_tower); } r = max(r, dist); } From 906fef3d53ec3d223c97f52383d6f5c3be47f4fd Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 21 Nov 2024 07:22:38 +0000 Subject: [PATCH 08/19] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- solutions/silver/cf-702C.mdx | 42 +++++++++++++++++++----------------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/solutions/silver/cf-702C.mdx b/solutions/silver/cf-702C.mdx index cb659ca1b0..e7c357228b 100644 --- a/solutions/silver/cf-702C.mdx +++ b/solutions/silver/cf-702C.mdx @@ -55,11 +55,13 @@ int main() { int min_r_for_this_city = 2e9; if (tower_right < m) { assert(towers[tower_right] >= cities[i]); - min_r_for_this_city = min(min_r_for_this_city, towers[tower_right] - cities[i]); + min_r_for_this_city = + min(min_r_for_this_city, towers[tower_right] - cities[i]); } if (tower_left >= 0) { assert(towers[tower_left] <= cities[i]); - min_r_for_this_city = min(min_r_for_this_city, cities[i] - towers[tower_left]); + min_r_for_this_city = + min(min_r_for_this_city, cities[i] - towers[tower_left]); } min_r = max(min_r, min_r_for_this_city); @@ -143,17 +145,18 @@ returns the first index in the array that is >= value, or arr.size() if no such index exists """ + def first_at_least(value: int) -> int: - lo = 0 - hi = len(towers) - while lo < hi: - mid = (lo + hi) // 2 - if towers[mid] > value: - hi = mid - else: - lo = mid + 1 + lo = 0 + hi = len(towers) + while lo < hi: + mid = (lo + hi) // 2 + if towers[mid] > value: + hi = mid + else: + lo = mid + 1 - return lo + return lo n, m = map(int, input().split()) @@ -162,18 +165,17 @@ towers = list(map(int, input().split())) min_r = 0 for i in range(n): - tower_right = first_at_least(cities[i]) - tower_left = tower_right - 1 - - min_r_for_this_city = float('inf') - if tower_right < m: - min_r_for_this_city = min(min_r_for_this_city, towers[tower_right] - cities[i]) + tower_right = first_at_least(cities[i]) + tower_left = tower_right - 1 - if tower_left >= 0: - min_r_for_this_city = min(min_r_for_this_city, cities[i] - towers[tower_left]) + min_r_for_this_city = float("inf") + if tower_right < m: + min_r_for_this_city = min(min_r_for_this_city, towers[tower_right] - cities[i]) + if tower_left >= 0: + min_r_for_this_city = min(min_r_for_this_city, cities[i] - towers[tower_left]) - min_r = max(min_r, min_r_for_this_city) + min_r = max(min_r, min_r_for_this_city) print(min_r) ``` From 70a92b271dccfb018189b50a224c88f4b6493cd4 Mon Sep 17 00:00:00 2001 From: freakin23 Date: Thu, 21 Nov 2024 13:02:56 +0530 Subject: [PATCH 09/19] py sol3 for cellular network --- solutions/silver/cf-702C.mdx | 75 +++++++++++++++++++++++++----------- 1 file changed, 52 insertions(+), 23 deletions(-) diff --git a/solutions/silver/cf-702C.mdx b/solutions/silver/cf-702C.mdx index e7c357228b..3a1613a257 100644 --- a/solutions/silver/cf-702C.mdx +++ b/solutions/silver/cf-702C.mdx @@ -55,13 +55,11 @@ int main() { int min_r_for_this_city = 2e9; if (tower_right < m) { assert(towers[tower_right] >= cities[i]); - min_r_for_this_city = - min(min_r_for_this_city, towers[tower_right] - cities[i]); + min_r_for_this_city = min(min_r_for_this_city, towers[tower_right] - cities[i]); } if (tower_left >= 0) { assert(towers[tower_left] <= cities[i]); - min_r_for_this_city = - min(min_r_for_this_city, cities[i] - towers[tower_left]); + min_r_for_this_city = min(min_r_for_this_city, cities[i] - towers[tower_left]); } min_r = max(min_r, min_r_for_this_city); @@ -145,18 +143,17 @@ returns the first index in the array that is >= value, or arr.size() if no such index exists """ - def first_at_least(value: int) -> int: - lo = 0 - hi = len(towers) - while lo < hi: - mid = (lo + hi) // 2 - if towers[mid] > value: - hi = mid - else: - lo = mid + 1 + lo = 0 + hi = len(towers) + while lo < hi: + mid = (lo + hi) // 2 + if towers[mid] > value: + hi = mid + else: + lo = mid + 1 - return lo + return lo n, m = map(int, input().split()) @@ -165,17 +162,18 @@ towers = list(map(int, input().split())) min_r = 0 for i in range(n): - tower_right = first_at_least(cities[i]) - tower_left = tower_right - 1 + tower_right = first_at_least(cities[i]) + tower_left = tower_right - 1 + + min_r_for_this_city = float('inf') + if tower_right < m: + min_r_for_this_city = min(min_r_for_this_city, towers[tower_right] - cities[i]) - min_r_for_this_city = float("inf") - if tower_right < m: - min_r_for_this_city = min(min_r_for_this_city, towers[tower_right] - cities[i]) + if tower_left >= 0: + min_r_for_this_city = min(min_r_for_this_city, cities[i] - towers[tower_left]) - if tower_left >= 0: - min_r_for_this_city = min(min_r_for_this_city, cities[i] - towers[tower_left]) - min_r = max(min_r, min_r_for_this_city) + min_r = max(min_r, min_r_for_this_city) print(min_r) ``` @@ -342,7 +340,6 @@ public class CellularNetwork2 { -## Solution 3 - Using a Set We use the set $\texttt{towers}$ to store the coordinates of the cellular towers. For each city, we want to find the closest tower and calculate the @@ -364,6 +361,8 @@ to be the maximum of itself and $\texttt{dist}$. +## Solution 3 - Using a Set + ```cpp #include #include @@ -406,6 +405,8 @@ int main() { +## Solution 3 - Using a Set + ```java import java.io.*; import java.util.*; @@ -448,4 +449,32 @@ public class CellularNetwork { ``` + + +## Solution 3 - Using a Sorted List + +```py +import bisect + +n, m = map(int, input().split()) +cities = list(map(int, input().split())) +towers = list(map(int, input().split())) + +r = 0 +for city in cities: + dist = float('inf') + closest_tower = bisect.bisect_left(towers, city) + + if closest_tower != len(towers): + dist = towers[closest_tower] - city + + if closest_tower > 0: + dist = min(dist, city - towers[closest_tower - 1]) + + r = max(r, dist) + +print(r) +``` + + From 39de25a9c46612bb2f098a34103b2f48b2f2ce57 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 21 Nov 2024 07:34:21 +0000 Subject: [PATCH 10/19] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- solutions/silver/cf-702C.mdx | 56 +++++++++++++++++++----------------- 1 file changed, 29 insertions(+), 27 deletions(-) diff --git a/solutions/silver/cf-702C.mdx b/solutions/silver/cf-702C.mdx index 3a1613a257..4d923b2bf0 100644 --- a/solutions/silver/cf-702C.mdx +++ b/solutions/silver/cf-702C.mdx @@ -55,11 +55,13 @@ int main() { int min_r_for_this_city = 2e9; if (tower_right < m) { assert(towers[tower_right] >= cities[i]); - min_r_for_this_city = min(min_r_for_this_city, towers[tower_right] - cities[i]); + min_r_for_this_city = + min(min_r_for_this_city, towers[tower_right] - cities[i]); } if (tower_left >= 0) { assert(towers[tower_left] <= cities[i]); - min_r_for_this_city = min(min_r_for_this_city, cities[i] - towers[tower_left]); + min_r_for_this_city = + min(min_r_for_this_city, cities[i] - towers[tower_left]); } min_r = max(min_r, min_r_for_this_city); @@ -143,17 +145,18 @@ returns the first index in the array that is >= value, or arr.size() if no such index exists """ + def first_at_least(value: int) -> int: - lo = 0 - hi = len(towers) - while lo < hi: - mid = (lo + hi) // 2 - if towers[mid] > value: - hi = mid - else: - lo = mid + 1 + lo = 0 + hi = len(towers) + while lo < hi: + mid = (lo + hi) // 2 + if towers[mid] > value: + hi = mid + else: + lo = mid + 1 - return lo + return lo n, m = map(int, input().split()) @@ -162,18 +165,17 @@ towers = list(map(int, input().split())) min_r = 0 for i in range(n): - tower_right = first_at_least(cities[i]) - tower_left = tower_right - 1 - - min_r_for_this_city = float('inf') - if tower_right < m: - min_r_for_this_city = min(min_r_for_this_city, towers[tower_right] - cities[i]) + tower_right = first_at_least(cities[i]) + tower_left = tower_right - 1 - if tower_left >= 0: - min_r_for_this_city = min(min_r_for_this_city, cities[i] - towers[tower_left]) + min_r_for_this_city = float("inf") + if tower_right < m: + min_r_for_this_city = min(min_r_for_this_city, towers[tower_right] - cities[i]) + if tower_left >= 0: + min_r_for_this_city = min(min_r_for_this_city, cities[i] - towers[tower_left]) - min_r = max(min_r, min_r_for_this_city) + min_r = max(min_r, min_r_for_this_city) print(min_r) ``` @@ -462,16 +464,16 @@ towers = list(map(int, input().split())) r = 0 for city in cities: - dist = float('inf') - closest_tower = bisect.bisect_left(towers, city) + dist = float("inf") + closest_tower = bisect.bisect_left(towers, city) - if closest_tower != len(towers): - dist = towers[closest_tower] - city + if closest_tower != len(towers): + dist = towers[closest_tower] - city - if closest_tower > 0: - dist = min(dist, city - towers[closest_tower - 1]) + if closest_tower > 0: + dist = min(dist, city - towers[closest_tower - 1]) - r = max(r, dist) + r = max(r, dist) print(r) ``` From ee98d92be7b5eac27a0be3f6ff8dad25e14d53e1 Mon Sep 17 00:00:00 2001 From: freakin23 Date: Thu, 21 Nov 2024 13:11:06 +0530 Subject: [PATCH 11/19] revert --- solutions/silver/cf-702C.mdx | 33 +-------------------------------- 1 file changed, 1 insertion(+), 32 deletions(-) diff --git a/solutions/silver/cf-702C.mdx b/solutions/silver/cf-702C.mdx index 4d923b2bf0..e7c357228b 100644 --- a/solutions/silver/cf-702C.mdx +++ b/solutions/silver/cf-702C.mdx @@ -342,6 +342,7 @@ public class CellularNetwork2 { +## Solution 3 - Using a Set We use the set $\texttt{towers}$ to store the coordinates of the cellular towers. For each city, we want to find the closest tower and calculate the @@ -363,8 +364,6 @@ to be the maximum of itself and $\texttt{dist}$. -## Solution 3 - Using a Set - ```cpp #include #include @@ -407,8 +406,6 @@ int main() { -## Solution 3 - Using a Set - ```java import java.io.*; import java.util.*; @@ -451,32 +448,4 @@ public class CellularNetwork { ``` - - -## Solution 3 - Using a Sorted List - -```py -import bisect - -n, m = map(int, input().split()) -cities = list(map(int, input().split())) -towers = list(map(int, input().split())) - -r = 0 -for city in cities: - dist = float("inf") - closest_tower = bisect.bisect_left(towers, city) - - if closest_tower != len(towers): - dist = towers[closest_tower] - city - - if closest_tower > 0: - dist = min(dist, city - towers[closest_tower - 1]) - - r = max(r, dist) - -print(r) -``` - - From 44ad8c7816e5c0c4cd9fd46518556fa24fa2a01c Mon Sep 17 00:00:00 2001 From: Rameez Parwez <79394137+freakin23@users.noreply.github.com> Date: Thu, 21 Nov 2024 13:38:15 +0530 Subject: [PATCH 12/19] Update solutions/silver/cf-702C.mdx Co-authored-by: Ryan Chou <81596991+ryanchou-dev@users.noreply.github.com> --- solutions/silver/cf-702C.mdx | 2 -- 1 file changed, 2 deletions(-) diff --git a/solutions/silver/cf-702C.mdx b/solutions/silver/cf-702C.mdx index e7c357228b..b04a8f41ab 100644 --- a/solutions/silver/cf-702C.mdx +++ b/solutions/silver/cf-702C.mdx @@ -144,8 +144,6 @@ public class CellularNetwork { returns the first index in the array that is >= value, or arr.size() if no such index exists """ - - def first_at_least(value: int) -> int: lo = 0 hi = len(towers) From 689ce220197d433ce4f72dadb23554fbfadda950 Mon Sep 17 00:00:00 2001 From: Rameez Parwez <79394137+freakin23@users.noreply.github.com> Date: Thu, 21 Nov 2024 13:38:25 +0530 Subject: [PATCH 13/19] Update solutions/silver/cf-702C.mdx Co-authored-by: Ryan Chou <81596991+ryanchou-dev@users.noreply.github.com> --- solutions/silver/cf-702C.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/solutions/silver/cf-702C.mdx b/solutions/silver/cf-702C.mdx index b04a8f41ab..b557760a37 100644 --- a/solutions/silver/cf-702C.mdx +++ b/solutions/silver/cf-702C.mdx @@ -141,7 +141,7 @@ public class CellularNetwork { ```py """ -returns the first index in the array that is >= value, or arr.size() +returns the first index in the array that is >= value, or len(arr) if no such index exists """ def first_at_least(value: int) -> int: From a3950153995cf6667f401a7cdf08e8a4817e57d2 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 21 Nov 2024 08:09:29 +0000 Subject: [PATCH 14/19] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- solutions/silver/cf-702C.mdx | 2 ++ 1 file changed, 2 insertions(+) diff --git a/solutions/silver/cf-702C.mdx b/solutions/silver/cf-702C.mdx index b557760a37..97c2c40b06 100644 --- a/solutions/silver/cf-702C.mdx +++ b/solutions/silver/cf-702C.mdx @@ -144,6 +144,8 @@ public class CellularNetwork { returns the first index in the array that is >= value, or len(arr) if no such index exists """ + + def first_at_least(value: int) -> int: lo = 0 hi = len(towers) From f7ea19a8e216defdd6bf79a361bd99de69a783a0 Mon Sep 17 00:00:00 2001 From: Rameez Parwez <79394137+freakin23@users.noreply.github.com> Date: Thu, 21 Nov 2024 13:43:19 +0530 Subject: [PATCH 15/19] Update cf-702C.mdx --- solutions/silver/cf-702C.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/solutions/silver/cf-702C.mdx b/solutions/silver/cf-702C.mdx index 97c2c40b06..eff6f62a79 100644 --- a/solutions/silver/cf-702C.mdx +++ b/solutions/silver/cf-702C.mdx @@ -359,7 +359,7 @@ left of the city. Define $\texttt{dist}$ to be the minimum of the distance to the tower on the right and the distance to the tower on the left. Then, set $r$ to be the maximum of itself and $\texttt{dist}$. -**Time Complexity:** $\mathcal{O}((N + M)\log M)$ +**Time Complexity:** $\mathcal{O}(N \log M)$ From 73cfd775a1350bc0710aebcc7eb3e20666d04667 Mon Sep 17 00:00:00 2001 From: Rameez Parwez <79394137+freakin23@users.noreply.github.com> Date: Fri, 22 Nov 2024 06:57:35 +0530 Subject: [PATCH 16/19] Update cf-702C.mdx --- solutions/silver/cf-702C.mdx | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/solutions/silver/cf-702C.mdx b/solutions/silver/cf-702C.mdx index eff6f62a79..670b90ed26 100644 --- a/solutions/silver/cf-702C.mdx +++ b/solutions/silver/cf-702C.mdx @@ -140,13 +140,12 @@ public class CellularNetwork { ```py -""" -returns the first index in the array that is >= value, or len(arr) -if no such index exists -""" - def first_at_least(value: int) -> int: + """ + returns the first index in the array that is >= value, or len(arr) + if no such index exists + """ lo = 0 hi = len(towers) while lo < hi: From a4ed4690bb84663080fc17412340c86bd62a1a0a Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 22 Nov 2024 01:28:48 +0000 Subject: [PATCH 17/19] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- solutions/silver/cf-702C.mdx | 1 - 1 file changed, 1 deletion(-) diff --git a/solutions/silver/cf-702C.mdx b/solutions/silver/cf-702C.mdx index 670b90ed26..282194695d 100644 --- a/solutions/silver/cf-702C.mdx +++ b/solutions/silver/cf-702C.mdx @@ -140,7 +140,6 @@ public class CellularNetwork { ```py - def first_at_least(value: int) -> int: """ returns the first index in the array that is >= value, or len(arr) From 5ba597a5e7a800b64905d4584bc5ec612f70ec14 Mon Sep 17 00:00:00 2001 From: Kevin Sheng <55369003+SansPapyrus683@users.noreply.github.com> Date: Thu, 21 Nov 2024 17:32:46 -0800 Subject: [PATCH 18/19] remove sol 3 yeah --- solutions/silver/cf-702C.mdx | 112 +---------------------------------- 1 file changed, 2 insertions(+), 110 deletions(-) diff --git a/solutions/silver/cf-702C.mdx b/solutions/silver/cf-702C.mdx index 282194695d..c35d0b5a38 100644 --- a/solutions/silver/cf-702C.mdx +++ b/solutions/silver/cf-702C.mdx @@ -7,7 +7,7 @@ author: Nathan Wang, Benjamin Qi, Maggie Liu, Brad Ma, George Pong [Official Editorial](https://codeforces.com/blog/entry/46324?locale=en) -## Solution 1 - Binary Search on a Sorted Array +## Solution 1 - Binary Search **Time Complexity:** $\mathcal{O}(N \log N)$ @@ -142,7 +142,7 @@ public class CellularNetwork { ```py def first_at_least(value: int) -> int: """ - returns the first index in the array that is >= value, or len(arr) + :return the first index in the array that is >= value, or len(arr) if no such index exists """ lo = 0 @@ -339,111 +339,3 @@ public class CellularNetwork2 { - -## Solution 3 - Using a Set - -We use the set $\texttt{towers}$ to store the coordinates of the cellular -towers. For each city, we want to find the closest tower and calculate the -distance. The minimal $r$ will be the maximum of all these distances. To find -the closest tower to a certain city, we check the towers to the left and right -and determine which one is closer. - -For each city, we use -[`lower_bound`](http://www.cplusplus.com/reference/algorithm/lower_bound/) to -find the closest tower to the right of the city. If this tower is found, -calculate the distance between this tower and the city. If the tower is not at -the beginning of the set, the previous tower in the set will be the tower to the -left of the city. Define $\texttt{dist}$ to be the minimum of the distance to -the tower on the right and the distance to the tower on the left. Then, set $r$ -to be the maximum of itself and $\texttt{dist}$. - -**Time Complexity:** $\mathcal{O}(N \log M)$ - - - - -```cpp -#include -#include -#include -using namespace std; - -int main() { - int n, m; - cin >> n >> m; - int cities[n]; - set towers; - int tower; - for (int i = 0; i < n; i++) { cin >> cities[i]; } - for (int i = 0; i < m; i++) { - cin >> tower; - towers.insert(tower); - } - int r = 0; - for (int i = 0; i < n; i++) { - int dist = 2e9 + 1; - // find closest tower to the right of the city - auto closest_tower = towers.lower_bound(cities[i]); - if (closest_tower != towers.end()) { - // if a tower is found, update the distance - dist = *closest_tower - cities[i]; - } - // find closest tower to the left of the city - if (closest_tower != towers.begin()) { - closest_tower--; - // update dist with the minimum of the distances - dist = min(dist, cities[i] - *closest_tower); - } - r = max(r, dist); - } - cout << r << endl; - return 0; -} -``` - - - - -```java -import java.io.*; -import java.util.*; - -public class CellularNetwork { - // CodeSnip{Kattio} - public static void main(String[] args) { - Kattio io = new Kattio(); - - int n = io.nextInt(); - int m = io.nextInt(); - int[] cities = new int[n]; - TreeSet towers = new TreeSet<>(); - for (int i = 0; i < n; i++) { cities[i] = io.nextInt(); } - - for (int i = 0; i < m; i++) { - int tower = io.nextInt(); - towers.add(tower); - } - - int r = 0; - for (int i = 0; i < n; i++) { - int distance = Integer.MAX_VALUE; - /* - * .lower() and .higher() methods return a value of type - * Integer if the value exists and null if it doesn't - */ - Integer closestTower = towers.lower(cities[i]); - Integer farthestTower = towers.higher(cities[i]); - if (closestTower != null) { distance = cities[i] - closestTower; } - if (farthestTower != null) { - distance = Math.min(distance, farthestTower - cities[i]); - } - r = Math.max(r, distance); - } - io.println(r); - io.close(); - } -} -``` - - - From 8cf844dca5fcb359fdd2a1678e213d661964a2b2 Mon Sep 17 00:00:00 2001 From: Ryan Chou <81596991+ryanchou-dev@users.noreply.github.com> Date: Sat, 23 Nov 2024 19:05:12 -0800 Subject: [PATCH 19/19] int max --- solutions/silver/cf-702C.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/solutions/silver/cf-702C.mdx b/solutions/silver/cf-702C.mdx index c35d0b5a38..a9234a5134 100644 --- a/solutions/silver/cf-702C.mdx +++ b/solutions/silver/cf-702C.mdx @@ -52,7 +52,7 @@ int main() { int tower_right = first_at_least(towers, cities[i]); int tower_left = tower_right - 1; - int min_r_for_this_city = 2e9; + int min_r_for_this_city = INT_MAX; if (tower_right < m) { assert(towers[tower_right] >= cities[i]); min_r_for_this_city =