【模拟】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>(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 (a, b, c) <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>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 题意:每个学生(1<=n<=10)存在一个awake-sleep周期,当这个学生到awake的最后一刻时,他要判断当前睡觉和醒的学生的人数,如果睡觉的人数绝对大于醒着的人数,那么他要继续保持清醒a分钟,否则就进入睡觉状态。
对于不存在全部醒的状态,由于每个学生的周期比较短,可以自行设置一个时间上线。超过这个上线就视为不存在。事实证明取500的时候就可以A了。
水。注意状态的转换更新即可。
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std;
const int maxn = ;
struct Stu
{
int a, b, c, period;
bool sleep;
}st[]; int main()
{
int n, kase = ;
int awake = , sleep = ;
while(~scanf("%d", &n) && n)
{
kase++;
for(int i = ; i < n; i++)
{
scanf("%d%d%d", &st[i].a, &st[i].b, &st[i].c);
st[i].period = st[i].a + st[i].b;
st[i].sleep = (st[i].c > st[i].a) ? true : false;
}
int t = ; bool Allawake = false;
while(t <= maxn)
{
awake = ; sleep=;
for(int i = ; i < n; i++)
{
if(!st[i].sleep) awake++;
else sleep++;
}
if(awake == n) {Allawake = true; break;}
for(int i = ; i < n; i++)
{
if(st[i].c == st[i].a)
{
if(sleep <= awake) {st[i].sleep = false; st[i].c = ;}
else {st[i].c++; st[i].sleep = true;}
}
else if(st[i].c == st[i].period)
{
st[i].c = ;
st[i].sleep = false;
}
else
{
st[i].c++;
}
}
t++;
}
if(Allawake) printf("Case %d: %d\n", kase, t);
else printf("Case %d: -1\n", kase);
}
return ;
}
【模拟】UVa 12108 - Extraordinarily Tired Students的更多相关文章
- uva 12108 Extraordinarily Tired Students (UVA - 12108)
算法完全转载...原博客(https://blog.csdn.net/u014800748/article/details/38407087) 题目简单叙述 题目就是一堆学生他们有清醒的时候和昏迷的时 ...
- UVA 12108 Extraordinarily Tired Students
思路: ①用结构体stu,属性有清醒时间,睡眠时间,开始处于的时间,状态(醒着还是睡着), 还有计数器. ②二维数组存表格. ③在确定接下来要进入的状态之后,就一次把表格里持续状态的数据都修改掉,比如 ...
- [刷题]算法竞赛入门经典(第2版) 4-8/UVa12108 - Extraordinarily Tired Students
书上具体所有题目:http://pan.baidu.com/s/1hssH0KO 代码:(Accepted,0 ms) //UVa12108 - Extraordinarily Tired Stude ...
- Extraordinarily Tired Students UVA - 12108
不知道叫什么,好像是模拟的方法,看懂了题就好办(英语硬伤←_←) 题意大概是当一个同学想睡觉的时候判断周围睡觉的人数,不睡的人数大于等于睡觉的话就死撑着,否则就睡觉. 一开始没有什么思路,就直接用了个 ...
- 【习题 4-8 UVA - 12108】Extraordinarily Tired Students
[链接] 我是链接,点我呀:) [题意] [题解] 一个单位时间.一个单位时间地模拟就好. 然后对于每个人. 记录它所处的周期下标idx 每个单位时间都会让每个人的idx++ 注意从醒着到睡着的分界线 ...
- [ACM_模拟] UVA 10881 Piotr's Ants[蚂蚁移动 数组映射 排序技巧]
"One thing is for certain: there is no stopping them;the ants will soon be here. And I, for one ...
- UVa 12108 特别困的学生
https://vjudge.net/problem/UVA-12108 题意:给出n个学生的“清醒—睡眠”周期和初始时间点,每个学生在睡眠时需要判断全班睡觉人数是否严格大于清醒人数,否则在坚持一个清 ...
- [ACM_模拟] UVA 12504 Updating a Dictionary [字符串处理 字典增加、减少、改变问题]
Updating a Dictionary In this problem, a dictionary is collection of key-value pairs, where keys ...
- [ACM_模拟] UVA 12503 Robot Instructions [指令控制坐标轴上机器人移动 水]
Robot Instructions You have a robot standing on the origin of x axis. The robot will be given som ...
随机推荐
- work3
HOMEWORK-03 红字部分为组员修改的意见 文件介绍 在EXE文件夹中的main.exe文件为本项目执行文件直接点击即可,CODE文件夹中的C文件夹中包含了算法部分的代码,其编译出的DLL文件在 ...
- C#的dll被其他程序调用时,获取此dll正确的物理路径
当C# dll被其他程序调用时,用Application.StartupPath获取的dll路径并不一定是此dll的物理路径,有可能是调用程序的路径. 以下方法或者能够获取dll正确的物理路径(未经过 ...
- OLEVARIANT的替代——FIREDAC的TFDJSONDataSets和TFDJSONDeltas
OLEVARIANT——这个COM的序列格式,也是DATASNAP已使用了20年的序列格式,在20年以后的今天,终于有了它的替代者:FIREDAC的TFDJSONDataSets和TFDJSONDel ...
- [iOS UI进阶 - 6.2] 核心动画CoreAnimation 练习代码
A.基本用法 1.CABasicAnimation // // ViewController.m // CoreAnimationTest // // Created by hellovoidworl ...
- Rop 文件上传解决思路
由于服务请求报文是一个文本,无法直接传送二进制的文件内容,因此必须采用某种转换机制将二进制的文件内容转换为字符串.Rop 采用如下的方式对上传文件进行编码:<fileType>@<B ...
- 关于session更新的问题
最近在学习用ssh框架做一个实习生招聘系统,已经做了大半.今天突然想到一个问题,在登录的时候我把用户的所有信息放到session中去,那么我不同用户同时登录的时候session中的信息是否会被覆盖掉( ...
- 创建类模式(二):抽象工厂(Abstract Factory)
定义 为创建一组相关或相互依赖的对象提供一个接口,而且无需指定他们的具体类. 抽象工厂模式是所有形态的工厂模式中最为抽象和最具一般性的一种形态.抽象工厂模式是指当有多个抽象角色时,使用的一种工厂模式. ...
- 关于本人遇到的nodejs的一些错误信息
window xp.win7 32位下安装node.js mongodb驱动 1.cmd->npm install mongodb 2.新建一个环境变量NODE_PATH 3.把Nodejs目录 ...
- MEF 编程指南(十):重组
有些应用程序被设计成在运行时动态地改变.例如,一个新的扩展可能被下载,或者其他原因变得不可用.MEF 依靠我们称之为重组(Composition)的技术处理,在初始化组合以后改变导入值的场景. 导 ...
- PostgreSQL中如何查询在当前的哪个数据库中
[pgsql@localhost bin]$ ./psql -d tester psql () Type "help" for help. tester=# select curr ...