#K55222. Longest Continuous Usage
Longest Continuous Usage
Longest Continuous Usage
You are given a sequence of log entries from a resource usage system. Each log entry contains three integers: a user id, a resource id, and a timestamp. Your task is to determine the resource that has the longest continuous usage by a single user.
A continuous usage is defined as a series of consecutive log entries (for the same user and resource) where the difference between consecutive timestamps is no more than 1. The duration of a continuous segment is measured as the difference between the last and the first timestamp of that segment. If a user-resource pair has only one log entry, the continuous usage duration is 0. In case of a tie (i.e. when multiple user-resource pairs share the same maximum continuous usage duration), choose the resource with the smallest resource ID.
Note: The input logs may not be in chronological order. You must consider only logs for the same user and resource together and compute the maximum duration from any continuous segment in that group. Formally, if for some user and resource the logs (after sorting by timestamp) are given by \(t_1, t_2, \dots, t_k\), then a continuous segment is a maximal subsequence \(t_i, t_{i+1}, \dots, t_j\) such that \(t_{p+1} - t_p \le 1\) for all \(i \le p < j\), and the segment duration is \(t_j - t_i\).
inputFormat
The first line contains an integer (n) (1 ≤ n ≤ 10^5) representing the number of log entries. Each of the following (n) lines contains three space-separated integers: user id, resource id, and timestamp.
outputFormat
Output a single integer, the resource id that achieved the longest continuous usage by a single user. If there is a tie, output the smallest resource id.## sample
5
123 1 100
123 1 200
123 2 300
123 1 400
124 1 500
1