Everyone Loves to Sleep (Codeforces 1714A)
Tags: Math, implementation

Vlad, like everyone else, loves to sleep very much.Every day Vlad has to do $$$n$$$ things, each at a certain time. For each of these things, he has an alarm clock set, the $$$i$$$-th of them is triggered on $$$h_i$$$ hours $$$m_i$$$ minutes every day ($$$0 \le h_i < 24, 0 \le m_i < 60$$$). Vlad uses the $$$24$$$-hour time format, so after $$$h=12, m=59$$$ comes $$$h=13, m=0$$$ and after $$$h=23, m=59$$$ comes $$$h=0, m=0$$$.This time Vlad went to bed at $$$H$$$ hours $$$M$$$ minutes ($$$0 \le H < 24, 0 \le M < 60$$$) and asks you to answer: how much he will be able to sleep until the next alarm clock.If any alarm clock rings at the time when he went to bed, then he will sleep for a period of time of length $$$0$$$.

Input: The first line of input data contains an integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases in the test.The first line of the case contains three integers $$$n$$$, $$$H$$$ and $$$M$$$ ($$$1 \le n \le 10, 0 \le H < 24, 0 \le M < 60$$$) — the number of alarms and the time Vlad went to bed.The following $$$n$$$ lines contain two numbers each $$$h_i$$$ and $$$m_i$$$ ($$$0 \le h_i < 24, 0 \le m_i < 60$$$) — the time of the $$$i$$$ alarm. It is acceptable that two or more alarms will trigger at the same time.Numbers describing time do not contain leading zeros.

Output: Output $$$t$$$ lines, each containing the answer to the corresponding test case. As an answer, output two numbers  — the number of hours and minutes that Vlad will sleep, respectively. If any alarm clock rings at the time when he went to bed, the answer will be 0 0.


Sample Inputs:
3
1 6 13
8 0
3 6 0
12 30
14 45
6 0
2 23 35
20 15
10 30

Sample Outputs:
1 47
0 0
10 55

Solution (Python):
def minutes_since_midnight(h, m):
    return h * 60 + m

def time_to_sleep(n, H, M, alarms):
    bed_time = minutes_since_midnight(H, M)
    alarm_times = [minutes_since_midnight(h, m) for (h, m) in alarms]
    alarm_times += [t + 24 * 60 for t in alarm_times]
    alarm_times.sort()
    for time in alarm_times:
        if time >= bed_time:
            sleep_time = time - bed_time
            return (sleep_time // 60, sleep_time % 60)
    return (0, 0)
t = int(input().strip())
for _ in range(t):
    (n, H, M) = map(int, input().strip().split())
    alarms = [tuple(map(int, input().strip().split())) for _ in range(n)]
    (hours, minutes) = time_to_sleep(n, H, M, alarms)
    print(hours, minutes)

Solution (Python):
t = int(input())
for _ in range(t):
    (n, H, M) = map(int, input().split())
    M1 = 60 * H + M
    res = 1000000000.0
    for i in range(n):
        (q1, q2) = map(int, input().split())
        M2 = 60 * q1 + q2
        if M2 >= M1:
            res = min(res, M2 - M1)
        else:
            res = min(res, 1440 - (M1 - M2))
    print(str(res // 60) + ' ' + str(res % 60))

Solution (Python):
def calculate_sleep_time(t):
    for _ in range(t):
        (n, h, m) = map(int, input().split())
        result = float('inf')
        alarm_times = []
        for _ in range(n):
            (hi, mi) = map(int, input().split())
            alarm_times.append(hi * 60 + mi)
        for alarm_time in alarm_times:
            if alarm_time <= h * 60 + m:
                alarm_time += 24 * 60
            time_diff = alarm_time - (h * 60 + m)
            if time_diff % (24 * 60) == 0:
                result = 0
                break
            if time_diff < result:
                result = time_diff
        print(result // 60, result % 60)
t = int(input())
calculate_sleep_time(t)