Skip to content

Commit

Permalink
remove sol 3 yeah
Browse files Browse the repository at this point in the history
  • Loading branch information
SansPapyrus683 authored Nov 22, 2024
1 parent a4ed469 commit 5ba597a
Showing 1 changed file with 2 additions and 110 deletions.
112 changes: 2 additions & 110 deletions solutions/silver/cf-702C.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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)$

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -339,111 +339,3 @@ public class CellularNetwork2 {
</JavaSection>
</LanguageSection>
## 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)$
<LanguageSection>
<CPPSection>
```cpp
#include <algorithm>
#include <iostream>
#include <set>
using namespace std;

int main() {
int n, m;
cin >> n >> m;
int cities[n];
set<int> 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;
}
```
</CPPSection>
<JavaSection>
```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<Integer> 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();
}
}
```
</JavaSection>
</LanguageSection>

0 comments on commit 5ba597a

Please sign in to comment.