UVa 12108 Extraordinarily Tired Students(特别困的学生)

Description

Download as PDF

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 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 struggles 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 could 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-sleeping-sleeping-sleeping. In this problem, we need another parameter c(1$ \le$c$ \le$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 (abc) 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.

\epsfbox{p3785.eps}

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(1$ \le$n$ \le$10) , the number of students. This is followed by n lines, each describing a student. Each of these lines contains three integers abc(1$ \le$ab$ \le$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
	
	

课堂有n个学生(n<=10),每个学生都有自己的一个“清醒--睡眠”周期,其中第i个学生醒Ai分钟后睡Bi分钟,然后重复这个周期,开始的时候第i个学生处在他周期的第Ci分钟。另外每个学生在睡觉之前会查看全班睡觉人数是不是严格大于清醒人数,满足这个条件就睡觉,不满足就坚持Ai分钟后再次检查这个条件,问经过多久全班都清醒。如果不存在全班都清醒的情况,输出-1。

这个题在想出现不存在都清醒的情况,应该是睡觉人数和清醒人数出现了规律性的循环了,但是想想这样解决很麻烦,不如试试迭代,因为这些学生的周期很短,所以迭代上限设置10000就差不多,以为每个学生都有自己的a、b、c,所以构造一个student类最好,对于迭代过程中的每个时刻,获取当前时刻清醒的人数(检查每个人的c,如果小于等于a则说明清醒),如果清醒人数等于总人数,跳出循环,输出当前时刻,否则就将所有人的c向后推移1,特别地,当某个学生处于周期的最后时刻或者想睡觉却发现不满足条件时,他的c归位到周期的起始位置。如果循环被果断终止,循环计数变量必定小于迭代上限,说明存在全班都清醒的时刻,输出该计数变量,否则说明不存在全班清醒的情况,输出-1,代码如下:

#include <iostream>

using namespace std;

int n;
const int MAX=11111;	//设置迭代上限

struct student
{
	int a,b,c,period;
	bool toRetime(int cont)	//判断是否可以将c归位,如果处于周期末尾或者想睡觉但不满足条件则归位
	{
		return (c==a&&cont>=n-cont)||(c==period);
	}
}stu[11];

istream & operator >> (istream & in,student & st)	//方便流输入,给周期period赋值
{
	cin>>st.a>>st.b>>st.c;
	st.period=st.a+st.b;
	return in;
}

int toAwaked();

int main()
{
	ios::sync_with_stdio(false);
	int k=0;
	while(k++,cin>>n,n)
	{
		int i;
		for(i=0;i<n;i++)
			cin>>stu[i];
		for(i=1;i<MAX;i++)
		{
			int cont;
			if((cont=toAwaked())==n)	//全部清醒
				break;
			for(int j=0;j<n;j++)
			{
				if(stu[j].toRetime(cont))	//符合条件,c归位
					stu[j].c=1;
				else	//否则c向后推移
					stu[j].c++;
			}
		}
		cout<<"Case "<<k<<": "<<(i<MAX?i:-1)<<endl;
	}

	return 0;
}
int toAwaked()	//获取当前清醒人数
{
	int cont=0;
	for(int i=0;i<n;i++)
		if(stu[i].c<=stu[i].a)
			cont++;
	return cont;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值