"Analog" UVa 12108-extraordinarily tired Students

Source: Internet
Author: User

When a student was too tired, he can ' t help sleeping in class, even if his favorite teacher was right here in front of him. Imagine you has a class of extraordinarily tired students, how long does you have to wait, and before all the students is List Ening to you and won ' t sleep any more? In order to complete the this task, you need to understand how students behave.

When a student was awaken, he struggles for a <tex2html_verbatim_mark>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 is strictly more sleeping students than awaken students, he sleeps for b <tex2html_verbatim_mark >minutes. Otherwise, he struggles for another a <tex2html_verbatim_mark>minutes, because he knew this when there is only very few sleeping students, there are a big chance for them to be punished! Note that a student counts the number of sleeping students if he wants to sleep again.

Now so you understand each student could is described by and integersa<tex2html_verbatim_mark>andb<tex2html_verbatim_mark>, the length of awaken and sleeping period. If there is always more sleeping students, these, 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 <tex2html_verbatim_mark>and b = 4 <tex2html_verbatim_mark>has an Awaken-sleeping period of awaken-sleeping-sleeping-sleeping-sleeping. In this problem, we need another parameter c <tex2html_verbatim_mark>(1Ca + b) <tex2html_verbatim_mark>to describe a student ' s initial condition:the initial position in his AWAKEN-SL Eeping period. The 1st and 2nd position of the period discussed above are awaken and sleeping, respectively.

Now we use a triple (a, b, C) <tex2html_verbatim_mark>to describe a student. Suppose there is three students (2, 4, 1), (1, 5, 2) and (1, 4, 3), all the students would be awaken at time 18. The details is shown in the table below.

<tex2html_verbatim_mark>

Table 1. An example

Write a program to calculate the first time, all the students is not sleeping.

Input

The input consists of several test cases. The first line of all case contains a single integer n (1n), the number of students. This was followed by n lines and each describing a student. Each of these lines contains three integers a, b, C (1a, b5), desc Ribed above. The last test case was followed by a single zero, which should wasn't be processed.

Output

For each test case, print the case number and the first time all the students is 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 30
Sample Output
Case 1:18 Case 2:1

Test instructions: Every student (1<=n<=10) has a awake-sleep cycle, and when the student is at the end of the awake, he wants to judge the current number of sleeping and waking students, and if the number of people sleeping is definitely greater than the number of people awake, then he will remain awake for a minute, Otherwise, go to sleep.
For the absence of a full awake state, because each student's cycle is relatively short, you can set a time to go online. Exceeding this on-line is considered non-existent. It turns out that when you take 500, you can get a.
Water. Note that the transition of the state is updated.

1#include <iostream>2#include <cstdio>3#include <cstdlib>4#include <cstring>5 using namespacestd;6 Const intMAXN = -;7 structStu8 {9     intA, B, C, period;Ten     BOOLsleep; One}st[ the]; A  - intMain () - { the     intN, Kase =0; -     intAwake =0, sleep =0; -      while(~SCANF ("%d", &n) &&N) -     { +kase++; -          for(inti =0; I < n; i++) +         { Ascanf"%d%d%d", &AMP;ST[I].A, &st[i].b, &st[i].c); atSt[i].period = st[i].a +st[i].b; -St[i].sleep = (st[i].c > st[i].a)?true:false; -         } -         intt =1;BOOLAllawake =false; -          while(T <=MAXN) -         { inAwake =0; sleep=0; -              for(inti =0; I < n; i++) to             { +                 if(!st[i].sleep) awake++; -                 Elsesleep++; the             } *             if(Awake = = N) {Allawake =true; Break;} $              for(inti =0; I < n; i++)Panax Notoginseng             { -                 if(ST[I].C = =st[i].a) the                 { +                     if(Sleep <= Awake) {St[i].sleep =false; ST[I].C =1;} A                     Else{st[i].c++; st[i].sleep =true;} the                 } +                 Else if(ST[I].C = =st[i].period) -                 { $ST[I].C =1; $St[i].sleep =false; -                 } -                 Else the                 { -St[i].c++;Wuyi                 } the             } -t++; Wu         } -         if(Allawake) printf ("Case %d:%d\n", Kase, T); About         Elseprintf"Case %d: -1\n", Kase); $     } -     return 0; -}

"Analog" UVa 12108-extraordinarily tired Students

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.