New Passenger Trams (Codeforces 1379D)
Tags: Others, two pointers

There are many freight trains departing from Kirnes planet every day. One day on that planet consists of $$$h$$$ hours, and each hour consists of $$$m$$$ minutes, where $$$m$$$ is an even number. Currently, there are $$$n$$$ freight trains, and they depart every day at the same time: $$$i$$$-th train departs at $$$h_i$$$ hours and $$$m_i$$$ minutes.The government decided to add passenger trams as well: they plan to add a regular tram service with half-hour intervals. It means that the first tram of the day must depart at $$$0$$$ hours and $$$t$$$ minutes, where $$$0 \le t < {m \over 2}$$$, the second tram departs $$$m \over 2$$$ minutes after the first one and so on. This schedule allows exactly two passenger trams per hour, which is a great improvement.To allow passengers to board the tram safely, the tram must arrive $$$k$$$ minutes before. During the time when passengers are boarding the tram, no freight train can depart from the planet. However, freight trains are allowed to depart at the very moment when the boarding starts, as well as at the moment when the passenger tram departs. Note that, if the first passenger tram departs at $$$0$$$ hours and $$$t$$$ minutes, where $$$t < k$$$, then the freight trains can not depart during the last $$$k - t$$$ minutes of the day.  A schematic picture of the correct way to run passenger trams. Here $$$h=2$$$ (therefore, the number of passenger trams is $$$2h=4$$$), the number of freight trains is $$$n=6$$$. The passenger trams are marked in red (note that the spaces between them are the same). The freight trains are marked in blue. Time segments of length $$$k$$$ before each passenger tram are highlighted in red. Note that there are no freight trains inside these segments. Unfortunately, it might not be possible to satisfy the requirements of the government without canceling some of the freight trains. Please help the government find the optimal value of $$$t$$$ to minimize the number of canceled freight trains in case all passenger trams depart according to schedule.

Input: The first line of input contains four integers $$$n$$$, $$$h$$$, $$$m$$$, $$$k$$$ ($$$1 \le n \le 100\,000$$$, $$$1 \le h \le 10^9$$$, $$$2 \le m \le 10^9$$$, $$$m$$$ is even, $$$1 \le k \le {m \over 2}$$$) — the number of freight trains per day, the number of hours and minutes on the planet, and the boarding time for each passenger tram.$$$n$$$ lines follow, each contains two integers $$$h_i$$$ and $$$m_i$$$ ($$$0 \le h_i < h$$$, $$$0 \le m_i < m$$$) — the time when $$$i$$$-th freight train departs. It is guaranteed that no freight trains depart at the same time.

Output: The first line of output should contain two integers: the minimum number of trains that need to be canceled, and the optimal starting time $$$t$$$. Second line of output should contain freight trains that need to be canceled.

Note: In the first test case of the example the first tram can depart at 0 hours and 0 minutes. Then the freight train at 16 hours and 0 minutes can depart at the same time as the passenger tram, and the freight train at 17 hours and 15 minutes can depart at the same time as the boarding starts for the upcoming passenger tram.In the second test case of the example it is not possible to design the passenger tram schedule without cancelling any of the freight trains: if $$$t \in [1, 15]$$$, then the freight train at 16 hours and 0 minutes is not able to depart (since boarding time is 16 minutes). If $$$t = 0$$$ or $$$t \in [16, 29]$$$, then the freight train departing at 17 hours 15 minutes is not able to depart. However, if the second freight train is canceled, one can choose $$$t = 0$$$. Another possible option is to cancel the first train and choose $$$t = 13$$$.

Sample Inputs:
2 24 60 15
16 0
17 15

2 24 60 16
16 0
17 15

Sample Outputs:
0 0

1 0
2

Solution (Python):
import os, io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
(n, h, m, k) = map(int, input().split())
trainMod = []
for i in range(n):
    (hi, mi) = map(int, input().split())
    trainMod.append((mi % (m // 2), i + 1))
    if mi % (m // 2) <= k:
        trainMod.append((mi % (m // 2) + m // 2, i + 1))
trainMod.sort(key=lambda x: x[0])
leftIndex = 0
minCancelledTrain = n
startTime = 0
cancelLeft = 0
cancelRight = n
for i in range(len(trainMod)):
    if trainMod[i][0] < k:
        continue
    while trainMod[leftIndex][0] <= trainMod[i][0] - k:
        leftIndex += 1
    if minCancelledTrain > i - leftIndex:
        minCancelledTrain = i - leftIndex
        startTime = trainMod[i][0] % (m // 2)
        cancelLeft = leftIndex
        cancelRight = i
print(str(minCancelledTrain) + ' ' + str(startTime))
cancelTrain = []
for i in range(cancelLeft, cancelRight):
    cancelTrain.append(str(trainMod[i][1]))
print(' '.join(cancelTrain))

Solution (Python):
import os, io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
(n, h, m, k) = map(int, input().split())
trainMod = []
for i in range(n):
    (hi, mi) = map(int, input().split())
    trainMod.append((mi % (m // 2), i + 1))
    if mi % (m // 2) <= k:
        trainMod.append((mi % (m // 2) + m // 2, i + 1))
trainMod.sort(key=lambda x: x[0])
leftIndex = 0
minCancelledTrain = n
startTime = 0
cancelLeft = 0
cancelRight = n
for i in range(len(trainMod)):
    if trainMod[i][0] < k:
        continue
    while trainMod[leftIndex][0] <= trainMod[i][0] - k:
        leftIndex += 1
    if minCancelledTrain > i - leftIndex:
        minCancelledTrain = i - leftIndex
        startTime = trainMod[i][0] % (m // 2)
        cancelLeft = leftIndex
        cancelRight = i
print(str(minCancelledTrain) + ' ' + str(startTime))
cancelTrain = []
for i in range(cancelLeft, cancelRight):
    cancelTrain.append(str(trainMod[i][1]))
print(' '.join(cancelTrain))

Solution (Python):
(n, h, m, k) = map(int, input().split())
train_mod = []
for i in range(n):
    (h_i, m_i) = map(int, input().split())
    train_mod.append((m_i % (m // 2), i + 1))
    if m_i % (m // 2) <= k:
        train_mod.append((m_i % (m // 2) + m // 2, i + 1))
train_mod.sort(key=lambda x: x[0])
(left_index, min_cancelled_train, start_time, cancel_left, cancel_right) = (0, n, 0, 0, n)
for i in range(len(train_mod)):
    if train_mod[i][0] < k:
        continue
    while train_mod[left_index][0] <= train_mod[i][0] - k:
        left_index += 1
    if min_cancelled_train > i - left_index:
        min_cancelled_train = i - left_index
        start_time = train_mod[i][0] % (m // 2)
        cancel_left = left_index
        cancel_right = i
print(min_cancelled_train, start_time)
cancel_train = []
for i in range(cancel_left, cancel_right):
    print(train_mod[i][1], end=' ')