HDU 2932 extraordinarily tired students (mathematics & Simulation)

Source: Internet
Author: User

Question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 2932


Problem descriptionwhen 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 strug Gles 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 cocould 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. 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. inputthe 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 shoshould not be processed. outputfor 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
32 4 11 5 21 4 331 2 11 2 21 2 30
Sample output
Case 1: 18Case 2: -1
Source2006 Asia Regional Xi-


Question:

The number of minutes that each student does not go to bed or sleep in a cycle is given. If one day, the student is supposed to be in the sleeping period, but he sees that the number of students not sleeping is more than half of the total number of students, then he will not go to bed, and vice versa;

Ask all students not to go to bed!


PS:

Simulate the process of sleeping and not sleeping, violent!


The Code is as follows:

#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 (mathematics & Simulation)

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.