|
| 1 | +# Defibrillators |
| 2 | + |
| 3 | +## Problem Description |
| 4 | + |
| 5 | +The city of Montpellier has equipped its streets with defibrillators, and the data corresponding to their positions is available. The task is to write a program that helps users find the nearest defibrillator to their current location. |
| 6 | + |
| 7 | +### Inputs |
| 8 | + |
| 9 | +1. **User Location:** |
| 10 | + - Line 1: User's longitude (in degrees, comma-separated). |
| 11 | + - Line 2: User's latitude (in degrees, comma-separated). |
| 12 | + - Line 3: The number `N` of defibrillators in the city. |
| 13 | + |
| 14 | +2. **Defibrillators Data:** |
| 15 | + Each of the next `N` lines contains the following fields separated by semicolons (`;`): |
| 16 | + - A number identifying the defibrillator. |
| 17 | + - Name of the defibrillator. |
| 18 | + - Address. |
| 19 | + - Contact Phone number. |
| 20 | + - Longitude (degrees, comma-separated). |
| 21 | + - Latitude (degrees, comma-separated). |
| 22 | + |
| 23 | +### Output |
| 24 | + |
| 25 | +The program should display the **name** of the defibrillator that is **closest** to the user’s current position. |
| 26 | + |
| 27 | +### Distance Formula |
| 28 | + |
| 29 | +The distance `d` between the user and a defibrillator is calculated as: |
| 30 | + |
| 31 | +``` |
| 32 | +x = (longitudeB - longitudeA) * cos((latitudeA + latitudeB) / 2) |
| 33 | +y = latitudeB - latitudeA |
| 34 | +distance = hypot(x, y) * 6371 |
| 35 | +``` |
| 36 | + |
| 37 | +### Constraints |
| 38 | + |
| 39 | +- 0 < `N` < 10,000 |
| 40 | + |
| 41 | +### Example |
| 42 | + |
| 43 | +#### Input |
| 44 | +``` |
| 45 | +3,879483 |
| 46 | +43,608177 |
| 47 | +3 |
| 48 | +1;Maison de la Prevention Sante;6 rue Maguelone 340000 Montpellier;;3,87952263361082;43,6071285339217 |
| 49 | +2;Hotel de Ville;1 place Georges Freche 34267 Montpellier;;3,89652239197876;43,5987299452849 |
| 50 | +3;Zoo de Lunaret;50 avenue Agropolis 34090 Mtp;;3,87388031141133;43,6395872778854 |
| 51 | +``` |
| 52 | + |
| 53 | +#### Output |
| 54 | +``` |
| 55 | +Maison de la Prevention Sante |
| 56 | +``` |
| 57 | + |
| 58 | +## Code Example |
| 59 | + |
| 60 | +```javascript |
| 61 | +function toRadians(degrees) { |
| 62 | + return degrees * Math.PI / 180; |
| 63 | +} |
| 64 | + |
| 65 | +// Parse the user's longitude and latitude |
| 66 | +const LON = parseFloat(readline().replace(',', '.')); |
| 67 | +const LAT = parseFloat(readline().replace(',', '.')); |
| 68 | + |
| 69 | +// Number of defibrillators |
| 70 | +const N = parseInt(readline()); |
| 71 | + |
| 72 | +let closestDefibName = ''; |
| 73 | +let closestDistance = Infinity; |
| 74 | + |
| 75 | +// Loop over each defibrillator to calculate the distance |
| 76 | +for (let i = 0; i < N; i++) { |
| 77 | + const DEFIB = readline().split(';'); |
| 78 | + const defibLon = parseFloat(DEFIB[4].replace(',', '.')); |
| 79 | + const defibLat = parseFloat(DEFIB[5].replace(',', '.')); |
| 80 | + |
| 81 | + // Calculate the distance using the provided formula |
| 82 | + const x = (defibLon - LON) * Math.cos(toRadians((LAT + defibLat) / 2)); |
| 83 | + const y = defibLat - LAT; |
| 84 | + const distance = Math.hypot(x, y) * 6371; // Earth's radius in kilometers |
| 85 | + |
| 86 | + // Check if this defibrillator is closer than the current closest |
| 87 | + if (distance < closestDistance) { |
| 88 | + closestDistance = distance; |
| 89 | + closestDefibName = DEFIB[1]; |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +// Output the name of the closest defibrillator |
| 94 | +console.log(closestDefibName); |
| 95 | + |
| 96 | +``` |
0 commit comments