A wildlife research station fits tracked animals with electronic collars and logs a sighting record every time an animal is detected by a sensor. Each sighting record lists the animal's ID, the ID of the collar device it was wearing at the time, the day number the sighting occurred on, and how many sensor readings were captured during that sighting. An animal may be fitted with a different collar between sightings (for example, after a battery swap), and it is never sighted twice on the same day.
For every animal that appears in the log, the research team wants to know which collar it was wearing on the very first day it was ever sighted — that is, on the sighting with the smallest day number recorded for that animal. The number of sensor readings in a record is logged purely for reference and plays no part in the answer.
Line 1: an integer m — the number of sighting records. Next m lines: four integers animal_id device_id day readings — one sighting record. For a fixed animal_id, all day values among its records are distinct.
Print one line per distinct animal_id that appears in the log, in increasing order of animal_id, each formatted as: animal_id device_id — the device_id of the collar recorded on that animal's smallest day.
Example 1
Input
5 1 101 3 10 1 102 1 15 2 201 2 5 2 202 5 7 3 301 4 2
Expected
1 102 2 201 3 301
Explanation
Animal 1 was sighted on day 3 wearing collar 101 and on day 1 wearing collar 102; day 1 is earlier, so its first collar was 102. Animal 2 was sighted on day 2 wearing collar 201 and on day 5 wearing collar 202; day 2 is earlier, so its first collar was 201. Animal 3 has only one record, on day 4 wearing collar 301. Sorted by animal_id, the output is "1 102", "2 201", "3 301".
Example 2
Input
3 5 555 10 0 2 222 20 100 5 556 5 50
Expected
2 222 5 556
Explanation
Animal 5 appears twice: day 10 wearing collar 555 and day 5 wearing collar 556; day 5 is earlier, so its first collar was 556. Animal 2 has a single record, day 20 wearing collar 222. Even though animal 5's records appear before animal 2's in the input, the output must be sorted by animal_id, so animal 2 ("2 222") is printed before animal 5 ("5 556").
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →