美文网首页
Codeforces 1354A - Alarm Clock

Codeforces 1354A - Alarm Clock

作者: 费城的二鹏 | 来源:发表于2020-06-17 20:47 被阅读0次

    日常一道算法题。

    翻译

    Polycarp 需要睡个觉,至少要休息 a 分钟,只有闹钟能叫醒他。他直接入睡了,第一个闹钟 b 分钟后响起。

    每次醒来他都会判断自己是继续睡觉还是继续休息,如果他休息的时间少于 a 分钟,那么将继续睡觉。

    如果需要继续睡觉,他会花费 d 分钟入睡,如此往复。

    输入格式

    输入整数 t 表示测试用例组数。

    每个测试用例输入一行 a b c d 用空格分隔。

    输出格式

    每个测试用例输出需要花费的分钟数 或者 -1。

    分析

    先判断 a 是否小于 b,如果满足条件直接输出 b。

    然后判断 d 是否大于等于 c,如果是,则输出 -1。

    否则,判断需要多少次 c 才能足够睡眠,需要注意的是余数问题。因为数据量比较大,不能纯模拟实现。

    代码(Python3)

    # https://codeforces.com/problemset/problem/1354/A
     
    import sys
    import os
    import heapq
    import math
     
    try:
        path = "./file/input.txt"
        if os.path.exists(path):
            sys.stdin = open(path, 'r')
        # sys.stdout = open(r"./file/output.txt", 'w')
    except:
        pass
     
    t = int(input())
     
    def printd(value):
        # print(value)
        pass
     
    def case():
        arr = list(map(int, input().split(" ")))
        a, b, c, d = arr[0], arr[1], arr[2], arr[3]
        if a > 0:
            a -= b
        
        if a > 0:
            if d >= c:
                print(-1)
            else:
                count =  a // (c - d)
                if a % (c - d) != 0:
                    count += 1
                print(b + count * c) 
        else:
            print(b)
    
    for _ in range(t):
        case()
    

    更多代码尽在 https://github.com/Tconan99/Codeforces

    by 费城的二鹏 2020.06.15 长春

    相关文章

      网友评论

          本文标题:Codeforces 1354A - Alarm Clock

          本文链接:https://www.haomeiwen.com/subject/rkxkxktx.html