#C3091. Longest Sequence of User Actions

    ID: 46480 Type: Default 1000ms 256MiB

Longest Sequence of User Actions

Longest Sequence of User Actions

Given a list of user actions, your task is to determine the length of the longest sequence of consecutive actions performed by any single user within a time window of \(W\) seconds.

Each action is represented by a triple: a user identifier (a string), a timestamp (an integer), and an action type (a string). The actions for each user may be provided in any order.

The sequence for a user is defined as a maximum set of actions such that the difference between the earliest and the latest timestamp in the sequence is strictly less than \(W\) seconds. Formally, if the sorted timestamps for a given user are \(t_1, t_2, \ldots, t_k\), you need to find the maximum length \(L\) such that there exists an index \(i\) with \[ t_{j} - t_{i} < W \quad \text{for all}\ i \leq j \leq i+L-1. \]

Output the maximum such sequence length across all users.

inputFormat

The input is read from standard input (stdin) and has the following format:

n W
user_1 timestamp_1 action_type_1
user_2 timestamp_2 action_type_2
... (n lines in total)

Here:

  • n is the number of actions (an integer).
  • W is the time window in seconds (an integer).
  • Each of the next n lines describes an action with a user id (string), a timestamp (integer) and an action type (string), separated by spaces.

outputFormat

Output a single integer, which is the length of the longest sequence of consecutive actions by any user within a time window of \(W\) seconds. The output should be written to standard output (stdout).

## sample
8 10
user1 1 view
user2 2 like
user1 3 comment
user1 4 share
user2 10 view
user2 11 like
user1 12 view
user1 13 like
3