特别困的学生(Extraordinarily Tired Students)

Extraordinarily Tired Students

Time limit: 3.000 seconds

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 parameterc (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 (a, b, c) 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 integern (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 integersa, b, c (1$ \le$a,b$ \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

【分析】

       思路源于 csdn博客LEII66的文章:原文地址

以下是我用java语言编写的程序代码(其中注释包含了我的理解):

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		int n, a, b, c, kase = 0;
		Student[] s = new Student[20];
		while(input.hasNext()) {
			n = input.nextInt();
			
			if(n == 0)
				break;
			
			int sn = 0;//睡觉人数
			for(int i = 0; i < n; i++) {
				a = input.nextInt();
				b = input.nextInt();
				c = input.nextInt();
				s[i] = new Student(a, b, c);
				if(c > a && c <= (a + b))
					sn++;
			}
			
			if(sn == 0) {
				System.out.println("Case " + (++kase) + ": " + 1);
				continue;
			}
			
			int x;
			for(x = 2; x <= 5000; x++) {
				for(int j = 0; j < n; j++) {
					//利用第x-1分钟时的状态判断现在第x分钟所应该处的状态(过了第x分钟所应该处的状态)
					//当将要睡觉时,判断睡觉人数是否严格大于清醒人数。
					if(s[j].initCon == s[j].awake && sn < n / 2 + 1) {
						s[j].initCon = 1;
					}
					else
						s[j].initCon++;
					if(s[j].initCon > s[j].period)
						s[j].initCon = 1;
					
					//s[j].initCon = (s[j].initCon + 1) % s[j].period;
				}
				
				//更新睡觉人数
				sn = 0;
				for(int j = 0; j < n; j++) {
					if(s[j].initCon > s[j].awake)
						sn++;
				}
				
				if(sn == 0)
					break;
			}
			
			System.out.print("Case " + (++kase) + ": ");
			if(x > 5000)
				System.out.println("-1");
			else
				System.out.println(x);
		}
	}
	
	static class Student {
		int awake;//清醒时间
		int sleep;//睡觉时间
		int initCon;//所处在其周期的时刻
		int period;//“睡眠-清醒”周期长
		
		public Student(int a, int b, int c) {
			awake = a;
			sleep = b;
			initCon = c;
			period = a + b;
		}
	}
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值