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 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 bminutes. 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(1ca + 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.

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分钟,否则就进入睡觉状态。对于不存在全部醒的状态,由于每个学生的周期比较短,可以自行设置一个时间上线。超过这个上线就视为不存在。取1005的时候就可以A了。

就是模拟吧,用个数组把周期存起来,1表示醒,0表示睡,比较容易理解和操作。再用c数组来记录当前该学生处于周期中的位置,依题意处理就好了。

#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
using namespace std;
int MAXN = 1005;
int main()
{
	int n;
	int cas = 1;
	while (scanf("%d", &n) != EOF && n)
	{
		int s[11][20];
		memset(s, 0, sizeof(s));
		int a[11];
		int b[11];
		int c[11];
		int sum_time[11];
		for (int i = 0; i < n; i++)
		{
			cin >> a[i] >> b[i] >> c[i];
			sum_time[i] = a[i] + b[i];
			for (int j = 1; j <= a[i]; j++)
				s[i][j] = 1;
		}

		/*
		for (int i = 0; i < n; i++)
		{
			for (int j = 1; j <= sum_time[i]; j++)
				cout << s[i][j];
			cout << endl;
		}
		*/
		cout << "Case " << cas++ << ": ";
		int ok = 0;
		for (int step = 0; step <= MAXN; step++)
		{
			int is_awake = 0, is_sleep = 0;
			for (int i = 0; i < n; i++)
			{
				if (s[i][(c[i] - 1) % sum_time[i] + 1]) is_awake++;
				else is_sleep++;
				c[i]++;
			}

			if (is_awake == n)
			{
				cout << step+1 << endl;
				ok = 1;
				break;
			}
			//cout << is_awake << " " << is_sleep << endl;

			if (is_awake >= is_sleep)
			{
				for (int i = 0; i < n;i++)
				if (s[i][(c[i] - 1) % sum_time[i] + 1] == 0 && s[i][(c[i] - 1 - 1) % sum_time[i] + 1] == 1)
					c[i] = 1;
			}
		}
		if (!ok) cout << -1 << endl;
	}
}
时间: 2024-11-23 22:02:48

UVA - 12108 Extraordinarily Tired Students(模拟)的相关文章

【模拟】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

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 10142 Australian Voting(模拟)

题意:澳大利亚投票系统要求选民们将所有候选人按愿意选择的程度排序,一张选票就是一个排序.一开始,每张选票的首选项将被统计.若有候选人得票超过50%,他讲直接胜出:否则,所有并列最低的候选人出局,而那些将出局候选人排在第一位的选票将被重新统计为排名最高的未出局候选人.这一筛选过程将持续进行,直到某个候选人得到超过50%的选票,或所有候选人得票相同. #include<cstdio> #include<cstring> #include<iostream> #include

UVA - 10023 - Square root (模拟手算开方)

题目传送:UVA - 10023 思路:模拟手算开方,不想用c/c++,感觉太麻烦了,就直接用的java里的BigInteger类来写的,写了好久......Java还是得看看书呀,手算开方参考的这里 AC代码: import java.util.Scanner; import java.math.BigInteger; public class Main { static void fun(BigInteger x) { String str; str = x.toString(); str

UVA - 133 The Dole Queue(模拟链表)

点击打开链接 n的人围成一个环,然后按逆时针编号1-n,一个人从1开始逆时针数k个数,另一个人从N开始顺时针数m个数,然后 数出来的两个人出列(两个人可能一样)出列,然后继续此过程,直到全部人都出列为止. 思路是用循环链表来模拟,注意 要分情况来讨论. #include <iostream> #include <cstdio> #include <cmath> #include <vector> #include <cstring> #inclu