UVA - 12108 Extraordinarily Tired Students(模拟)

来源:互联网 发布:linux网络运维是什么 编辑:程序博客网 时间:2024/06/05 18:53
Extraordinarily Tired Students
Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu

Submit Status

Description

Download as PDF

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 bminutes. 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$ \le$c$ \le$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.

\epsfbox{p3785.eps}
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(1$ \le$n$ \le$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$ \le$a, b$ \le$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 30

Sample Output 

Case 1: 18 Case 2: -1

每个学生(1<=n<=10)存在一个awake-sleep周期,当这个学生到awake的最后一刻时,他要判断当前睡觉和醒的学生的人数,如果睡觉的人数绝对大于醒着的人数,那么他要继续保持清醒a分钟,否则就进入睡觉状态。对于不存在全部醒的状态,由于每个学生的周期比较短,可以自行设置一个时间上线。超过这个上线就视为不存在。取1005的时候就可以A了。

就是模拟吧,用个数组把周期存起来,1表示醒,0表示睡,比较容易理解和操作。再用c数组来记录当前该学生处于周期中的位置,依题意处理就好了。

#include<iostream>#include<cstring>#include<string>#include<cstdio>using namespace std;int MAXN = 1005;int main(){int n;int cas = 1;while (scanf("%d", &n) != EOF && n){int s[11][20];memset(s, 0, sizeof(s));int a[11];int b[11];int c[11];int sum_time[11];for (int i = 0; i < n; i++){cin >> a[i] >> b[i] >> c[i];sum_time[i] = a[i] + b[i];for (int j = 1; j <= a[i]; j++)s[i][j] = 1;}/*for (int i = 0; i < n; i++){for (int j = 1; j <= sum_time[i]; j++)cout << s[i][j];cout << endl;}*/cout << "Case " << cas++ << ": ";int ok = 0;for (int step = 0; step <= MAXN; step++){int is_awake = 0, is_sleep = 0;for (int i = 0; i < n; i++){if (s[i][(c[i] - 1) % sum_time[i] + 1]) is_awake++;else is_sleep++;c[i]++;}if (is_awake == n){cout << step+1 << endl;ok = 1;break;}//cout << is_awake << " " << is_sleep << endl;if (is_awake >= is_sleep){for (int i = 0; i < n;i++)if (s[i][(c[i] - 1) % sum_time[i] + 1] == 0 && s[i][(c[i] - 1 - 1) % sum_time[i] + 1] == 1)c[i] = 1;}}if (!ok) cout << -1 << endl;}}



0 0