• UVA 12108


    Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu

     Status

    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
    
    
    
    
    #include <iostream>
    #include <cstdio>
    using namespace std;
    
    int n;
    int temp[1024][3], x[1024][3];
    
    bool can_sleep()
    {
    	int shui=0, xing=0;
    	for(int i=0; i<n; i++)
    	{
    		if(x[i][2]!=0 && x[i][2]<=x[i][0])
    			xing++;
    		else
    			shui++;
    	}
    	if(shui > xing)
    		return true;
    	else
    		return false;
    }
    
    bool is_quanxing()
    {
    	for(int i=0; i<n; i++)
    	{
    		if(x[i][2]==0 || (x[i][2] > x[i][0]))
    			return false;
    	}
    	return true;
    }
    
    int main()
    {
    	int kase=0;
    	while(scanf("%d",&n)!=EOF && n!=0)
    	{
    		int time = 0;
    		bool can;
    		for(int i=0; i<n; i++)
    		{
    			for(int j=0; j<3; j++)
    			{
    				scanf("%d",&x[i][j]);
    				temp[i][j] = x[i][j];
    			}
    		}
    		printf("Case %d: ",++kase);
    		can = can_sleep();
    		for(time=1; time<1024; time++)
    		{
    			if(is_quanxing())
    			{
    				printf("%d
    ",time);
    				break;
    			}
    			for(int i=0; i<n; i++)
    			{
    				if(x[i][2]==x[i][0])
    				{
    					if(can)
    						x[i][2] = (x[i][2] + 1)%(x[i][0] + x[i][1]);
    				}
    				else
    					x[i][2] = (x[i][2] + 1) % (x[i][0] + x[i][1]);
    			}
    			can = can_sleep();
    		}
    		if(time==1024)
    			printf("-1
    ");
    	}
    	return 0;
    }


  • 相关阅读:
    包装类
    项目基础架构搭建
    开发环境的搭建和Shell编程
    Linux系统概述和编程基础
    java.lang.ExceptionInInitializerError
    mybatis中文文档
    如何造轮子
    Application Server was not connected before run configuration stop, reason: Unable to ping server at localhost:1099
    JDK 1.7与JDK 1.8版本的完美切换
    JDBC连接步骤
  • 原文地址:https://www.cnblogs.com/kunsoft/p/5312803.html
Copyright © 2020-2023  润新知