CF1714A题解

一个人给定他睡觉的时间,以及他设定的各个闹钟的时间,判断在下一次闹钟响之前,它还能睡多长时间。
摘要由CSDN通过智能技术生成

题目描述

Vlad, like everyone else, loves to sleep very much.

Every day Vlad has to do n n n things, each at a certain time. For each of these things, he has an alarm clock set, the i i i -th of them is triggered on hi h_i hi​ hours mi m_i mi​ minutes every day ( 0≤hi<24,0≤mi<60 0 \le h_i < 24, 0 \le m_i < 60 0≤hi​<24,0≤mi​<60 ). Vlad uses the 24 24 24 -hour time format, so after h=12,m=59 h=12, m=59 h=12,m=59 comes h=13,m=0 h=13, m=0 h=13,m=0 and after h=23,m=59 h=23, m=59 h=23,m=59 comes h=0,m=0 h=0, m=0 h=0,m=0 .

This time Vlad went to bed at H H H hours M M M minutes ( 0≤H<24,0≤M<60 0 \le H < 24, 0 \le M < 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 0 0 .

输入格式

The first line of input data contains an integer t t t ( 1≤t≤100 1 \le t \le 100 1≤t≤100 ) — the number of test cases in the test.

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

The following n n n lines contain two numbers each hi h_i hi​ and mi m_i mi​ ( 0≤hi<24,0≤mi<60 0 \le h_i < 24, 0 \le m_i < 60 0≤hi​<24,0≤mi​<60 ) — the time of the i i 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 t t 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.

输入输出样例

输入 #1

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

输出 #1

1 47
0 0
10 55

题目大意

一个人给定他睡觉的时间,以及他设定的各个闹钟的时间,判断在下一次闹钟响之前,它还能睡多长时间

题目分析

因为题目可能会有第一天第二天的区分,我们不能确定一个时间是否和这个人睡觉的时间是同一天,这时我们就需要进行一个预处理,将时间小于这个人睡觉的时间小时数+++ 242424,使其变成第二天,这样即使距离他的睡眠时间最近但是是最早一个叫醒这个人的,也依然可以确认是第一个,接下来进行 sortsortsort 排序,手写一个 cmpcmpcmp,小时数优先,相同比较小时数,取第一个闹钟时间与这个人的睡觉时间进行相减,这里我用的是闹钟小时数 ∗*∗ 606060 +++ 闹钟分钟数 −-− 睡觉小时数 ∗*∗ 606060 −-− 睡觉分钟数,最终的小时数就除 606060,分钟数就模 606060

上代码

    #include<bits/stdc++.h>
    using namespace std;
    int t;
    int n,h,m;
    struct node
    {
        int hh,minn;
    }clockk[61];//结构体便于sort排序 
    int cmp(node a,node b)
    {
        if(a.hh!=b.hh)//先比较小时数 
        {
            return a.hh<b.hh;
        }
        else if(a.hh==b.hh)//小时数相同比较分钟数 
        {
            return a.minn<b.minn;
        }
    }
    int main()
    {
        scanf("%d",&t);
        while(t--)
        {
            scanf("%d %d %d",&n,&h,&m);
            for(int i=1;i<=n;i++)
            {
                scanf("%d %d",&clockk[i].hh,&clockk[i].minn);   
                if(clockk[i].hh<h||(clockk[i].hh==h&&clockk[i].minn<m))//小于睡觉时间+24默认变为第二天 
                {
                    clockk[i].hh+=24;
                }
            }
            sort(clockk+1,clockk+1+n,cmp);
            printf("%d %d\n",(clockk[1].hh*60+clockk[1].minn-h*60-m)/60,(clockk[1].hh*60+clockk[1].minn-h*60-m)%60);
            for(int i=1;i<=61;i++)//初始化防止sort重利用 
            {
                clockk[i].hh=0;
                clockk[i].minn=0;
            }
        }   
        return 0;//好习惯 
    } 
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值