#C10032. Calculate Customer Interaction Times
Calculate Customer Interaction Times
Calculate Customer Interaction Times
You are given a series of events related to customer interactions. Each event is represented by a tuple consisting of a customer ID, an event type (either start
or end
), and a timestamp. An interaction starts with a start
event and ends with the corresponding end
event. The interaction time for one session is calculated as:
[ T = \text{timestamp}{\text{end}} - \text{timestamp}{\text{start}} ]
A customer may have multiple interactions. Your task is to compute the total interaction time for each customer by summing all of their interaction durations. The final output should list each customer ID along with their total interaction time, sorted by customer ID in ascending order.
Note: It is possible that an end
event might occur with a timestamp earlier than its corresponding start
event, which could result in a negative duration. In such cases, use the computed negative value.
inputFormat
The input is given via standard input (stdin) with the following format:
- The first line contains an integer
n
representing the number of events. - The following
n
lines each contain an event in the format:customer_id event_type timestamp
, wherecustomer_id
andtimestamp
are integers andevent_type
is a string that can be eitherstart
orend
.
outputFormat
For each customer, print a line containing the customer ID and the total interaction time separated by a space. The output should be in ascending order of customer ID. The result is printed to standard output (stdout).
Example:
Input: 6 1 start 100 2 start 150 1 end 200 1 start 300 1 end 400 2 end 250## sample</p>Output: 1 200 2 100
2
1 start 100
1 end 200
1 100
</p>