【模拟】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 ...
随机推荐
- (转)关于CoreData的一个工具Mogenerator的使用
最近看到用CoreData时使用的工具Mogenerator,发现网上介绍其具体使用的不多,特此简单整理一下,关于CoreData这里就不具体说了,使用就用MagicalRecord,用起来真是太方便 ...
- POj3268 Silver Cow Party
http://poj.org/problem?id=3268 题目大意:求到x距离与从x返回和的最大值 从x点到各个点最短路好求,直接用Dijkstar,但从各个点到x点却不好求,只要把路向翻转过来也 ...
- 认识JavaScript的原型
本来打算也写一个JavaScript学习笔记的系列,不过由于笔者不太想买大部头的js数据,和网上的资料也不少,所以js系列就打算写到了算了了. 要理解JavaScript就要理解其原型,首先我们先区分 ...
- 【Java】C/C++与Java的简单比较
转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/5827273.html C/C++: 编译(不同的系统编译出不同的机器码,所以同一 ...
- Ubuntu 12.04 中安装和配置 Java JDK
先去 Oracle下载Linux下的JDK压缩包,我下载的是jdk-7u4-linux-i586.tar.gz文件,下好后直接解压 sudo mv ./jdk1.7.0_55 /usr/lib/jdk ...
- BOOL,int,float,指针变量与零值比较的if语句
1.注意这里说的是,与零值比较,而不是与零比较. 2.对于int类型,与零值比较就是: if(var == 0) //零值 3.对于bool类型,零值表示false,任何非零值表示true,因此使用: ...
- delphi 在 DragDrop 的时候,滚动 TreeView
在 DragDrop 的时候,滚动 TreeView 当高度不够的时候 procedure TForm1.TreeView1DragOver(Sender, Source: TObject; X, ...
- 归并排序的C语言实现
归并排序的核心思想是 Divide-and-Conquer 算法,即将要解决的size为n的问题,分成a个size为n/b的子问题,这些子问题的结果经过O(n^d)的时间复杂度合并,即可解决最初的问题 ...
- Fedora 下 安装 chrome
一.下载安装包,安装 1.去google 下载安装包 2.终端下 运行命令: rpm -ivh google-chrome-stable_current_i386.rpm 3. 出现如下 ...
- iOS修改声明为readonly的属性值
本文讨论的是,对于类中声明为 readonly 的属性值,我们就不可以修改其值了么?如何可以,那么如何修改呢? 为了便于说明,定义一个 ACLStudent 的类: ACLStudent.h @int ...