【模拟】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 ...
随机推荐
- angular的注入实现
angular需要对用户的传入函数进行静态分析,抽取当中的依赖,才能工作.因此用户的函数,包括控制器函数,工厂函数,服务函数与$watch回调都只是一个模板,用于取toString,真正运行的是编译后 ...
- poj 2349 Arctic Network
http://poj.org/problem?id=2349 Arctic Network Time Limit: 2000MS Memory Limit: 65536K Total Submis ...
- [C语言 - 2] C语言变量
A.变量的作用域: 1.局部变量:在函数或者代码块内部定义的变量 作用域:从定义处到代码块结束 生命周期:从定义处分配控件,代码块结束后被回收 局部变量没有默认值,要自己初始化 2.全局变量:在函 ...
- Cannot retrieve metalink for repository: epel. Please verify its path and try again
今天在测试环境使用yum安装,遇到一个问题: Error: Cannot retrieve metalink for repository: epel. Please verify its path ...
- 关于EL表达式的大小写问题。谁来帮我解答?
最近在学习ssh框架,今天遇到了一个非常奇怪的问题.我想在jsp页面中的到session中的数据.<%=s.getUserYes() %>这样写能得到数据, ${sessionScope. ...
- android开发教程(八)——环境搭建之java-ndk
目录 android ndk是android用于开发本地代码的开发工具包.它提供C/C++交叉编译工具.android内核.驱动.已有的C/C++代码,都需要ndk来支持开发. 目前支持以下平台:ar ...
- fedora21发布与新功能介绍(附fedora21安装教程与fedora21下载地址)
fedora21发布与新功能介绍(附fedora21安装教程与fedora21下载地址) 最新的Fedora 21终于正式发布了,Fedora Server 是一款强大可定制化的操作系统,包括了最好最 ...
- github 坑爹的仓库初始化设置
一段时间没有使用 github,奇妙地发现自己连仓库都不会建了,汗一个... 话说上次我在 github 上面建了一个仓库,在创建仓库的设置表单中勾上了自动生成 README.md 选项, ok,创建 ...
- Converting a .jks Key Store to a .pem Key Store
In order to convert a Java key store into a Privacy Enhanced Mail Certificate, you will need to use ...
- win7硬盘安装Ubuntu12.04 64位时显示Error 15: File not found.
安装Ubuntu12.04 -64位时,用EasyBCD建好引导文件重启电脑后出现如下错误: Error 15: File not found 原因一个是安装文件所在盘符不对,另一个是文件名.Ubun ...