【模拟】UVa 12108 - Extraordinarily Tired Students

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>(1ca + 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 (1n10), the number of students. This is followed by n lines, each describing a student. Each of these lines 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 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<=n<=10)存在一个awake-sleep周期,当这个学生到awake的最后一刻时,他要判断当前睡觉和醒的学生的人数,如果睡觉的人数绝对大于醒着的人数,那么他要继续保持清醒a分钟,否则就进入睡觉状态。对于不存在全部醒的状态,由于每个学生的周期比较短,可以自行设置一个时间上线。超过这个上线就视为不存在。事实证明取500的时候就可以A了。水。注意状态的转换更新即可。
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstdlib>
 4 #include<cstring>
 5 using namespace std;
 6 const int maxn = 500;
 7 struct Stu
 8 {
 9     int a, b, c, period;
10     bool sleep;
11 }st[15];
12
13 int main()
14 {
15     int n, kase = 0;
16     int awake = 0, sleep = 0;
17     while(~scanf("%d", &n) && n)
18     {
19         kase++;
20         for(int i = 0; i < n; i++)
21         {
22             scanf("%d%d%d", &st[i].a, &st[i].b, &st[i].c);
23             st[i].period = st[i].a + st[i].b;
24             st[i].sleep = (st[i].c > st[i].a) ? true : false;
25         }
26         int t = 1; bool Allawake = false;
27         while(t <= maxn)
28         {
29             awake = 0; sleep=0;
30             for(int i = 0; i < n; i++)
31             {
32                 if(!st[i].sleep) awake++;
33                 else sleep++;
34             }
35             if(awake == n) {Allawake = true; break;}
36             for(int i = 0; i < n; i++)
37             {
38                 if(st[i].c == st[i].a)
39                 {
40                     if(sleep <= awake)   {st[i].sleep = false; st[i].c = 1;}
41                     else {st[i].c++; st[i].sleep = true;}
42                 }
43                 else if(st[i].c == st[i].period)
44                 {
45                     st[i].c = 1;
46                     st[i].sleep = false;
47                 }
48                 else
49                 {
50                     st[i].c++;
51                 }
52             }
53             t++;
54         }
55         if(Allawake) printf("Case %d: %d\n", kase, t);
56         else printf("Case %d: -1\n", kase);
57     }
58     return 0;
59 }
时间: 2024-11-20 03:33:45

【模拟】UVa 12108 - Extraordinarily Tired Students的相关文章

UVA - 12108 Extraordinarily Tired Students(模拟)

Extraordinarily Tired Students Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status 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 hi

Uva 12108 Extraordinarily Tired Students

题意: 课堂上有n个学生(n<=10).每个学生都有一个“清醒-睡眠”周期.其中第i个学生醒 ai 分钟后睡 bi 分钟,然后重复(1<=ai,bi<=5),初始时第i个学生处在他的周期的第 ci 分钟.每个学生在临 睡前会察看全班睡觉人数是否严格大于清醒人数,只有这个条件满足时才睡觉,否则就坚持听课 ai 分钟后再次检查这个条件.问经过多长时间后全班都清醒. 思路: 用优先队列记录每次要更改的时间和对应要更改状态的学生编号,按要更改的时间从小到大排序. cnt记录当前睡觉的人数. 这里

12108 - Extraordinarily Tired Students

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

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

题目链接: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 studen

HDU 2932 Extraordinarily Tired Students (暴力+取模还是很强大的)

题目链接:HDU 2932 Extraordinarily Tired Students 题意:给出N个学生的状态,(a,b,c).a表示a分钟这醒着,b表示b分钟睡着,c表示刚开始是重周期(a+b)分钟的第c分钟开始.求第几分钟,所有的学生都没有睡觉.其中每个学生在睡觉前看一下是否睡觉的人数(包括他自己)比醒着的人数大,若是就睡觉,反之则不睡觉. 数据很小,暴力之,假设所有学生都没睡觉的时间不超过1000000,发现还可以更小. AC代码; #include <stdio.h> #inclu

【习题 4-8 UVA - 12108】Extraordinarily Tired Students

[链接] 我是链接,点我呀:) [题意] [题解] 一个单位时间.一个单位时间地模拟就好. 然后对于每个人. 记录它所处的周期下标idx 每个单位时间都会让每个人的idx++ 注意从醒着到睡着的分界线的处理就好. 可以多循环几次..超过上限认为无解 (其他题解也提供了一种方法,就是如果状态和初始的情况相同的话.就无解了即形成了一个环. (可能如果无解一定会形成环? [代码] #include <bits/stdc++.h> #define rep1(i,a,b) for (int i = a;

UVa 12108 特别困的学生

https://vjudge.net/problem/UVA-12108 题意:给出n个学生的“清醒—睡眠”周期和初始时间点,每个学生在睡眠时需要判断全班睡觉人数是否严格大于清醒人数,否则在坚持一个清醒周期后再次判断.求出多长时间时全班都是清醒状态. 思路:水题吧.一次次的枚举下去,循环终止的条件就是和初始状态相同. 1 #include<iostream> 2 using namespace std; 3 4 struct Node 5 { 6 int A; 7 int B; 8 int C

算法竞赛入门经典 第四章

[√ ] UVA1339 古老的密码 Ancient Cipher [√ ] UVA489 刽子手的游戏 Hangman Judge [√ ] UVA133 救济金发放 The Dole Queue [√ ] UVA213 信息解码 Message Decoding [√ ] UVA512 追踪电子表格中的单元格 Spreadsheet Tracking [√ ] UVA12412 师兄帮帮忙 A Typical Homework (a.k.a Shi Xiong Bang Bang Mang)

紫书第4章 函数和递归

1  序 系统的整理下第四章的学习笔记.同上次一样,尽量在不依赖书本的情况下自己先把例题做出来.这次有许多道题代码量都比较大,在例题中我都用纯C语言编写,但由于习题的挑战性和复杂度,我最终还是决定在第五章开始前,就用C++来完成习题.不过所有的代码都是能在C++提交下AC的. 在习题中,我都习惯性的构造一个类来求解问题,从我个人角度讲,这会让我的思路清晰不少,希望自己的一些代码风格不会影响读者对解题思路的理解. 其实在第四章前,我就顾虑着是不是真的打算把题目全做了,这些题目代码量这么大,要耗费很