码迷,mamicode.com
首页 > 其他好文 > 详细

12108 - Extraordinarily Tired Students

时间:2015-05-19 16:20:56      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:

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 <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 are 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 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 <tex2html_verbatim_mark>and b <tex2html_verbatim_mark>, 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 <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>(1技术分享c技术分享a + b) <tex2html_verbatim_mark>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 (abc) <tex2html_verbatim_mark>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.

 

技术分享<tex2html_verbatim_mark>

 

Table 1. An example

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 <tex2html_verbatim_mark>(1技术分享n技术分享10) <tex2html_verbatim_mark>, the number of students. This is followed by n lines, each describing a student. Each of these lines contains three integers abc <tex2html_verbatim_mark>(1技术分享ab技术分享5) <tex2html_verbatim_mark>, 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

题目不算太难,模拟一下即可。
 1 #include <stdio.h>
 2 #include <string.h>
 3 
 4 struct student
 5 {
 6     int A;    //清醒时间
 7     int B;  //睡觉时间
 8     int C;  //当前时间 
 9     int D;  //一个周期长度
10     int is_sleep;  //是否正在睡觉 
11 }stu[15];
12 
13 int n, time, awake, sleep, j, kase = 0;
14 
15 int main()
16 {
17     while(scanf("%d", &n) != EOF && n)
18     {
19         printf("Case %d: ", ++kase);
20         awake = sleep = 0;
21         time = 1;
22         for(int i = 0; i < n; i++)
23         {
24             scanf("%d%d%d", &stu[i].A, &stu[i].B, &stu[i].C);
25             stu[i].D = stu[i].A + stu[i].B;
26             time *= stu[i].D;
27             if(1 <= stu[i].C && stu[i].C<= stu[i].A)
28             {
29                 awake++;
30                 stu[i].is_sleep = 0;
31             }
32             else
33             {
34                 sleep++;
35                 stu[i].is_sleep = 1;
36             }
37         }
38         for(j = 1; j < time; j++)
39         {
40             if(awake == n)
41             {
42                 break;
43             }
44             int now_sleep = sleep, now_awake = awake;
45             for(int i = 0; i < n; i++)
46             {
47                 if(stu[i].C+1 <= stu[i].A)    //下一秒在清醒时间的 
48                 {
49                     stu[i].C++;
50                 }
51                 else
52                 {
53                     if(stu[i].C+1 > stu[i].D && stu[i].is_sleep)  //下一秒从睡觉中清醒的 
54                     {
55                         stu[i].C = 1;
56                         awake++;
57                         sleep--;
58                         stu[i].is_sleep = 0;
59                     }
60                     else if(stu[i].C == stu[i].A)    //下一秒要睡觉的
61                     {
62                         if(now_sleep > now_awake)
63                         {
64                             stu[i].C++;
65                             stu[i].is_sleep = 1;
66                             awake--;
67                             sleep++;
68                         }
69                         else
70                         {
71                             stu[i].C = 1;
72                         }
73                     }
74                     else if(stu[i].C+1 <= stu[i].D && stu[i].is_sleep) //下一秒继续睡觉的
75                     {
76                         stu[i].C++;
77                     } 
78                 }
79             }
80         }
81         if(j != time) printf("%d\n", j);
82         else printf("-1\n");
83     }
84 }

 

 

12108 - Extraordinarily Tired Students

标签:

原文地址:http://www.cnblogs.com/hsq666/p/4514709.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!