Codeforces Round 811 (Div. 3) A. Everyone Loves to Sleep

211 阅读2分钟

题目链接

题目详情

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 hih_i hours mim_i minutes every day (0hi<24,0mi<60) (0≤h_i<24,0≤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 (0H<24,0M<60)(0≤H<24,0≤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(1t100)t (1≤t≤100) — the number of test cases in the test.

The first line of the case contains three integers n,Hn , H and M(1n10,0H<24,0M<60) M (1≤n≤10,0≤H<24,0≤M<60) — the number of alarms and the time Vlad went to bed.

The following n lines contain two numbers each hih_i and mim_i (0hi<24,0mi<60)(0≤h_i<24,0≤m_i<60) — the time of the ii 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 tt 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.

input

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

output

1 47
0 0
10 55

题目大意及思路

大意:

弗拉德和其他人一样,非常喜欢睡觉。弗拉德每天都要做一些事情,每件事都要在特定的时间做。对于这些事情中的每一件,他都有一个闹钟,其中第i个是在每天的hi小时mi分钟触发的Vlad使用24小时的时间格式,因此在h=12,m=59之后,h=13,m=0,在h=23之后,h=0,m=0。这一次Vlad在H小时M分钟上床睡觉,并要求你回答:在下一个闹钟之前,他能睡多少觉。如果他上床睡觉时有闹钟响,然后他将睡眠一段长度为0.如果他上床睡觉的时候有闹钟响,答案是0。
解题思路:
这里我们只需要将每次给出的要做的所有事的时间点和上床的时间点都计算成分钟形式,再注意作差,找到差值最小的并转换成hhmm形式输出即可。注意:可能差值为负数,当任务事件小于上床时间时,将任务事件加上1440(一天)再计算。

AC代码

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;

int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        int n, H, M, clo,cha = 1000000;
        cin >> n >> H >> M;
        clo = H * 60 + M;
        while (n--)
        {
            int minutes = 0;
            int a, b;
            cin >> a >> b;
            int t = a * 60 + b;
            if (t < clo)
            {
                t += 1440;
            }
            cha = min(cha, t - clo);
        }
        cout << cha / 60 << " " << cha % 60 << endl;
    }
}