首页 > 代码库 > HDU 2932 Extraordinarily Tired Students(数学 & 模拟)

HDU 2932 Extraordinarily Tired Students(数学 & 模拟)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2932


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
 
Source
2006 Asia Regional Xi-An


题意:

给出每个学生在一个周期内不睡觉和睡觉的分钟数,如果在某一天,学生本来在该睡觉的时间段但是他看见不睡觉的人数大于总人数的一半,那么他就不会睡觉,反之睡觉;

求所有的学生都不睡觉的时间!


PS:

模拟学生的睡觉和不睡觉过程,暴力!


代码如下:

#include<cstdio>
#include<cstring>
const int maxn = 17;
const int MAXN = 100017;

int main()
{
    int n;
    int cas = 0;
    int a[maxn], b[maxn], c[maxn];
    while(scanf("%d",&n)&&n)
    {
        for(int i = 1; i <= n; i++)
            scanf("%d%d%d", &a[i], &b[i], &c[i]);
        int ans;
        int cont = 0;
        
        for(ans = 1; ans < MAXN; ans++)
        {
            cont = 0;
            for(int i = 1; i <= n; i++)
            {
                if(c[i] <= a[i])//检查位置是否在开始睡觉的时间之前
                    cont++;//没有睡觉的人数
            }
            if(cont == n)   //每个位置都在所给的开始睡觉的时间之前
                break;
            for(int i = 1; i <= n; i++)
            {
                if(c[i] == a[i]+b[i] || (c[i] == a[i] && cont > n-cont))
                {
                    //新的一个循环开始 || 本来应该睡觉但是此时没有睡觉的人数大于一半
                    c[i] = 0;//重新计数
                }
                c[i]++;
            }
        }
        if(ans == MAXN)
            ans = -1;

        printf("Case %d: %d\n", ++cas, ans);
    }
    return 0;
}


HDU 2932 Extraordinarily Tired Students(数学 & 模拟)