12108 - Extraordinarily Tired Students

来源:互联网 发布:怎么跟网络管理员联系 编辑:程序博客网 时间:2024/06/06 02:01

When a student is too tired, he can't helpsleeping 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 youhave to wait, before all the students are listening to you and won't sleep anymore? In order to complete this task, you need to understand how studentsbehave.

When a student is awaken, he strugglesfor a minutes listening to the teacher (after all, it's toobad to sleep all the time). After that, he counts the number of awaken andsleeping students (including himself). If there are strictly more sleepingstudents than awaken students, he sleeps for b minutes.Otherwise, he struggles for another a minutes, because he knewthat when there is only very few sleeping students, there is a big chance forthem to be punished! Note that a student counts the number of sleeping studentsonly when he wants to sleep again.

Now that you understand each student could bedescribed by two integers a and b , thelength of awaken and sleeping period. If there are always more sleepingstudents, these two periods continue again and again. We combine an awakenperiod with a sleeping period after it, and call the combined period anawaken-sleeping period. For example, a student with a =1 and b = 4 has an awaken-sleeping period ofawaken-sleeping-sleeping-sleeping-sleeping. In this problem, we need anotherparameter c (1ca + b) todescribe a student's initial condition: the initial position in hisawaken-sleeping period. The 1st and 2nd position of the period discussed aboveare awaken and sleeping, respectively.

Now we use a triple (abc) todescribe 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 inthe table below.

Table 1. An example

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

Input 

The input consists of several test cases. Thefirst line of each case contains a single integern (1n10) , the number ofstudents. This is followed by n lines, each describing a student. Each of theselines contains three integers abc (1ab5) , 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 andthe 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

 

 

 

 

代码:

#include<iostream>

using namespacestd;

const intMAX=1000000;//由于题目没告诉数据范围,所以将时间的上限定得大一些,暴力求解

int main()

{

    int a[15],b[15],c[15];

    int stnum;//学生人数

    int casenumber=1;//数据序号

    while(cin>>stnum&&stnum!=0)

    {

        for(int i=0;i<stnum;i++)

        {

            cin>>a[i]>>b[i]>>c[i];

        }

        int time=1;

        for(;time<MAX;time++)//开始遍历时间

        {

            int wake=0;//醒的人数每次必须清零

            for(int i=0;i<stnum;i++)

            {

                if(c[i]<=a[i])

                {

                    wake++;

                }

            }

            if(wake==stnum)

            {

                break;

            }

            for(int i=0;i<stnum;i++)

            {

               if(c[i]==a[i]+b[i]||(c[i]==a[i]&&wake>=stnum-wake))

//周期结束后或者想睡觉但是醒的人数不小于睡的人数的时候,C[i]重设为1

                {

                    c[i]=1;

                }

                else

                {

                    c[i]++;

                }

            }

        }

        if(time==MAX)//不出现目标情况

        {

            cout<<"Case"<<casenumber<<": -1\n";

            casenumber++;

        }

        else

        {

            cout<<"Case"<<casenumber<<": "<<time<<endl;

            casenumber++;

        }

    }

}

0 0