#C11216. Merging Sensor Records
Merging Sensor Records
Merging Sensor Records
You are given n sensor records. Each sensor record consists of a device name, power usage and zone. Records with the same power usage are considered as belonging to the same group. For each group, you must choose the lexicographically smallest device name as its canonical name and calculate the total power usage by summing up the power usage values of all records in that group.
The task is to merge the sensor records and output the number of unique devices (i.e. number of groups) along with each device and its corresponding total usage sorted by the device name.
Formally, let \( R = \{(d_i, p_i, z_i) \}_{i=1}^{n} \) denote the list of sensor records. For every group of records sharing the same power usage \( p \), define the canonical device name as \[ D(p) = \min\{ d_i: p_i = p \} \] and the total usage for that group as \[ T(p) = \sum_{\{i: p_i = p\}} p_i = k \times p, \quad \text{where } k \text{ is the number of records with usage } p. \] Output the number of unique groups and the list of merged records sorted by the canonical device name.
inputFormat
The input is read from standard input (stdin) and has the following format:
- The first line contains an integer n representing the number of sensor records.
- Each of the next n lines contains three space-separated values:
device_name
(a string),power_usage
(a float), andzone
(a string).
outputFormat
Print to standard output (stdout) in the following format:
- The first line contains an integer representing the number of unique devices (i.e. merged groups).
- Each of the next lines contains two space-separated values: the canonical device name and its total power usage formatted as a float with one decimal place.
- The output list must be sorted in lexicographical order of the device names.
2
heater 1500.0 zone1
heater_1 1500.0 zone2
1
heater 3000.0