IBMOA #68
Service Timeout Detection
Estimated EasyJudge-ready
Topics (AI-labelled)HashingIn-place Arrays
Each heartbeat record contains a timestamp and the ID of the service it belongs to. For each service ID, sort its heartbeats by timestamp; if the interval between any two adjacent heartbeats is strictly greater than threshold, the service is considered to have timed out at least once. A service with only one heartbeat never times out. Note that the heartbeats in the input are not sorted by timestamp.
Find all service IDs that timed out at least once and output them in ascending lexicographic order.
Input
The first line contains two integers n and threshold. Each of the next n lines contains an integer timestamp and a string serviceId. 1≤n≤200000, 1≤timestamp≤10^9, 0≤threshold≤10^9, and serviceId has length 1..10 and contains only lowercase letters and digits.
Output
Output the number k of timed-out services on the first line; then output k lines, one service ID each, in ascending lexicographic order.
Example 1
Input:
5 30
10 svc1
20 svc1
80 svc1
10 svc2
65 svc2
Output:
2
svc1
svc2
Example 2
Input:
3 1
1 svc1
2 svc2
3 svc1
Output:
1
svc1
Example 3
Input:
4 5
3 b
1 a
6 b
4 a
Output:
0
Example notes
Example 1: svc1 has a gap of 60 between 20 and 80, and svc2 has a gap of 55 between 10 and 65; both exceed 30, so 2 services are output. Example 2: svc1 has a gap of 2 between 1 and 3, which exceeds 1; svc2 has only one heartbeat and does not time out, so 1 service, svc1, is output. Example 3: a has heartbeats 1 and 4, and b has heartbeats 3 and 6; both gaps are 3, which does not exceed 5, so the output is 0.
Editorial
Loading solution…