#C4272. Group and Sort Records
Group and Sort Records
Group and Sort Records
You are given a list of records, where each record consists of an identifier (a string) and a timestamp (an integer). Your task is to group these records by their identifier and then sort the timestamps in ascending order for each group.
The input will be provided via standard input in the following format:
- The first line contains an integer n, representing the number of records.
- The next n lines each contain a record consisting of an identifier and a timestamp separated by space.
You should output the result as a JSON-formatted object (dictionary) where the keys are the identifiers and the values are arrays of timestamps sorted in ascending order. The keys in the output JSON must be sorted in lexicographical order.
Note: Use stdin for input and stdout for output.
The algorithm must work efficiently even for large inputs (for example, when n is large).
inputFormat
The input is read from standard input.
- Line 1: An integer
n
, the number of records. - Next
n
lines: Each line contains a string and an integer separated by a space, representing an identifier and its associated timestamp.
outputFormat
Output a single line containing a JSON object. The object’s keys are the unique identifiers (sorted lexicographically) and each key maps to an array of timestamps (sorted in ascending order).
The output should be printed to standard output.
## sample5
user1 1623495600
user2 1623499200
user1 1623502800
user3 1623492000
user2 1623488400
{"user1": [1623495600, 1623502800], "user2": [1623488400, 1623499200], "user3": [1623492000]}