编程介的小学生 2019-12-07 09:47 采纳率: 20.5%
浏览 62

Extraordinarily Tired Students

Problem Description
When a student is too tired, he can't help sleeping in class, even if his favorite teacher is right here in front of him. Imagine you have a class of extraordinarily tired students, how long do you have to wait, before all the students are listening to you and won't sleep any more? In order to complete this task, you need to understand how students behave. When a student is awaken, he struggles for a minutes listening to the teacher (after all, it's too bad to sleep all the time). After that, he counts the number of awaken and sleeping students (including himself). If there are strictly more sleeping students than awaken students, he sleeps for b minutes. Otherwise, he struggles for another a minutes, because he knew that when there is only very few sleeping students, there is a big chance for them to be punished! Note that a student counts the number of sleeping students only when he wants to sleep again. Now that you understand each student could be described by two integers a and b, the length of awaken and sleeping period. If there are always more sleeping students, these two periods continue again and again. We combine an awaken period with a sleeping period after it, and call the combined period an awaken-sleeping period. For example, a student with a=1 and b=4 has an awaken-sleeping period of awaken-sleeping-sleeping-sleeping-sleeping. In this problem, we
need another parameter c (1 ≤ c ≤ a+b) to describe a student's initial condition: the initial position in his awaken-sleeping period. The 1st and 2nd position of the period discussed above are awaken and sleeping, respectively.
Now we use a triple (a, b, c) to describe a student. Suppose there are three students (2, 4, 1), (1, 5, 2) and (1, 4, 3), all the students will be awaken at time 18. The details are shown in the table below.

Write a program to calculate the first time when all the students are not sleeping.

Input
The input consists of several test cases. The first line of each case contains a single integer n (1 ≤ n ≤ 10), the number of students. This is followed by n lines, each describing a student. Each of these lines contains three integers a, b, c (1 ≤ a, b ≤ 5), described above. The last test case is followed by a single zero, which should not be processed.

Output
For each test case, print the case number and the first time all the students are awaken. If it'll never happen, output -1.

Sample Input
3
2 4 1
1 5 2
1 4 3
3
1 2 1
1 2 2
1 2 3
0

Sample Output
Case 1: 18
Case 2: -1

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 鸿业暖通修改详细负荷时闪退
    • ¥15 有偿求码,CNN+LSTM实现单通道脑电信号EEG的睡眠分期评估
    • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
    • ¥50 成都蓉城足球俱乐部小程序抢票
    • ¥15 yolov7训练自己的数据集
    • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
    • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
    • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
    • ¥20 matlab yalmip kkt 双层优化问题
    • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体