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(1c
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}](http://uva.onlinejudge.org/external/121/p3785.jpg)
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(1n
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
看题目都看着看着就困了Orz
之前还想着用什么算法,但发现数据其实非常小,纯模拟就足够了
把样例中的表格用数组C表示,1表示睡觉,2表示清醒,0表示还未判断(当然0只可能接在2的后面,因为每个睡觉时间最后一个1后面肯定是2),逐渐向下模拟,遇到0就判断是否全部清醒或者清醒的人数大于等于睡觉的人数,直到找到答案为止。
至于不可能的情况,人数小于10,睡觉时间小于5,10的5次方应该就是最大循环周期,故time最多枚举到10000就OK了(至于其他博客有人说枚到5000就够了,亲测错误)
#include <iostream> #include <stdio.h> #include <string.h> using namespace std; struct stu { int a; int s; int initial; }; int main() { int cnt=0,i,j,c[12][11111],n,time,awakestu,sleepstu,flag; stu p[12]; do { scanf("%d",&n); if (n==0) { break; } cnt++; memset(c,0,sizeof(c)); for (i=0;i<=n-1;i++) { scanf("%d%d%d",&p[i].a,&p[i].s,&p[i].initial); } for (i=0;i<=n-1;i++) { if (p[i].initial<=p[i].a) { for (j=0;j<=p[i].a-p[i].initial;j++) { c[i][j]=2; } } else if (p[i].initial>p[i].a) { for (j=0;j<=p[i].s+p[i].a-p[i].initial;j++) { c[i][j]=1; } for(j=p[i].s+p[i].a-p[i].initial+1;j<=p[i].s+2*p[i].a-p[i].initial;j++) { c[i][j]=2; } } } time=0; flag=-1; do { for (i=0;i<=n-1;i++) { if (c[i][time]==0) { flag=1; break; } } if (flag==1) { break; } else { time++; } }while(1); for (i=0;i<=time;i++) { flag=1; for (j=0;j<=n-1;j++) { if (c[j][i]!=2) { flag=0; break; } } if (flag==1) { time=i; goto l1; } } time--; flag=-1; do { if (time>11110) { flag=0; break; } else { sleepstu=0; awakestu=0; for (i=0;i<=n-1;i++) { if (c[i][time]==1) { sleepstu++; } else if (c[i][time]==2) { awakestu++; } } if (sleepstu==0) { flag=1; break; } for (i=0;i<=n-1;i++) { if (c[i][time+1]==0) { if (sleepstu>awakestu) { for (j=time+1;j<=time+p[i].s;j++) { c[i][j]=1; } for (j=time+p[i].s+1;j<=time+p[i].s+p[i].a;j++) { c[i][j]=2; } } else { for (j=time+1;j<=time+p[i].a;j++) { c[i][j]=2; } } } } time++; } }while(1); if(flag==0) { printf("Case %d: -1\n",cnt); } else if(flag==1) { l1: printf("Case %d: %d\n",cnt,time+1); } }while(1); return 0; }