问题 J: Workout for a Dumbbell
时间限制: 1 Sec 内存限制: 128 MB题目描述
Jim Ratt has just joined a local fitness center. He’s especially excited about a sequence of 10 machines that he cycles through three times for his workout. He has a fixed time which he spends on each machine, as well as a fixed recovery time after using a machine. Jim’s not the brightest guy in the world, but in the absence of anything else even he would easily be able to calculate how long his workout would take.
But of course, Jim isn’t the only person who uses the fitness center and wouldn’t you know it but when Jim shows up there are always 10 other people there, each using one of the ten machines exclusively. Like Jim, each person has a fixed time they use on their machine as well as a fixed recovery time. This will sometimes cause Jim to have to wait for a particular machine, and Jim’s usage sometimes results in the other people having to wait as well (though if both Jim and another person want to start using a machine at the same time, Jim is polite enough to let the other person go first). Jim has gone to the center often enough that he has a good idea what everyone’s usage and recovery times are, but he has trouble determining how long it will take him to perform his workout. That’s where you are going to flex your programming muscles.
But of course, Jim isn’t the only person who uses the fitness center and wouldn’t you know it but when Jim shows up there are always 10 other people there, each using one of the ten machines exclusively. Like Jim, each person has a fixed time they use on their machine as well as a fixed recovery time. This will sometimes cause Jim to have to wait for a particular machine, and Jim’s usage sometimes results in the other people having to wait as well (though if both Jim and another person want to start using a machine at the same time, Jim is polite enough to let the other person go first). Jim has gone to the center often enough that he has a good idea what everyone’s usage and recovery times are, but he has trouble determining how long it will take him to perform his workout. That’s where you are going to flex your programming muscles.
输入
Input starts with a line containing twenty integers; the first two give Jim’s usage and recovery time for machine 1, the next two give Jim’s usage and recovery time for machine 2, etc. The next line contains 3 integers u r t; the first two values are the usage and recovery times for the person who is using machine 1,and t is the time when he/she first starts using the machine. The next 9 lines specify similar information for machines 2 through 10. All usage and recovery times are positive and ≤ 5 000 000 and all start times t satisfy |t| ≤ 5 000 000. You should assume that Jim is ready to use machine 1 at time 0.
输出
Display the time when Jim has finished his workout, i.e., the moment when he has finished his usage time on machine 10 for the third time (don’t count the last recovery time for that machine).
样例输入
5 5 3 3 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
2 2 1
8 3 0
1 1 0
1 1 0
1 1 0
1 1 0
1 1 0
1 1 0
1 1 0
1 1 0
样例输出
100
题意:小花想起了初中生物书上的蛋白质,于是要锻炼肌肉。当然这一段是跟题意没关系的。健身房有十台机器,现在给出了小花在每台机器的锻炼时间和休息时间,机器不是小花专属的,每个机器上还有一个捣
蛋鬼在锻炼,捣蛋鬼有自己的锻炼时间,休息时间和开始时间,在开始时间之后,就开始不停的在这台机器上锻炼休息锻炼休息循环往复,小花在0时刻从1号机器开始锻炼,一直锻炼到10号机器,要锻炼三个来
回。问锻炼完之后一共需要多少时间,最后一个锻炼的休息时间不计算在内。
做法:这道题就是模拟,场上的时候因为没有读出来小花是从1号到10号有顺序进行的,以为很复杂的可以优先选择,于是,就放弃了。这个锅有点沉,我先放一放。
我用d[],x[]分别代表小花在每台机器的锻炼时间和休息时间,dd[],xx[],st[]分别代表每台机器捣蛋鬼的锻炼时间,休息时间和开始时间,这里的st[]要不断更新,因为捣蛋鬼是循环往复锻炼的,所以每次
的开始时间都是更新的。
tim记录现在的时间,j记录现在要开始锻炼哪一台机器了,要是此时这台机器的st[]在tim之后,就代表捣蛋鬼还没开始锻炼或者处在上一轮的休息时间之中,于是,我就可以直接开始锻炼,此时st[j]就要更
新,如果我锻炼完你还没开始,那么st[j]还是不变的,如果我锻炼完已经超过了你的st[j],那么st[j]就更新成我锻炼完的时间。 如果这台机器的st[j]在tim之前,我就找到最近一次开始的时间,于是判
断一下捣蛋鬼此时是在锻炼还是在休息,如果在锻炼,我就需要先等一下再开始锻炼,如果在休息,我就可以直接开始锻炼,并且不要忘记更新下一次捣蛋鬼开始的时间。
代码如下:
#include#include using namespace std; long long d[15] , x[15]; long long dd[15] , xx[15] , st[15]; //long long flag[15]; long long tim; int main() { while( scanf("%lld%lld" , &d[1] , &x[1]) != EOF) { for(long long i=2; i<=10; i++) { scanf("%lld%lld" , &d[i] , &x[i]); } // printf(".......\n"); for(long long i=1; i<=10; i++) { scanf("%lld%lld%lld" , &dd[i] , &xx[i] , &st[i]); // flag[i] = 0; ///当st[j]>tim时 我们需要判断是还未开始训练 还是正在休息 } // printf(".......\n"); tim = 0; for(long long i=1; i<=30; i++) { long long j = i%10; ///现在要锻炼第几个器材 if(j == 0) j = 10; if(st[j]>tim ) { tim += (d[j]+x[j]); ///如果开始的时间在tim的后面 要么是捣蛋鬼还没有开始 如果捣蛋鬼已经快开始了 并且我从这里走过一遍了 那么捣蛋鬼现在一定不是在锻炼 ///因为 我上次遇见他的时候 他如果在休息 我再次遇见她 他还没与开始 那就还是在休息 如果上次遇见是在锻炼 他不锻炼完我是不会完成任务的 /// 于是我走的时候捣蛋鬼也是在休息 if(st[j] <= tim-x[j]) { st[j] = tim-x[j]; } } else if(st[j]<=tim) { while( st[j]+dd[j]+xx[j] <= tim) st[j] += (dd[j]+xx[j]); ///找到捣蛋鬼最近一次开始的时间 // printf("%lld.%lld.%lld.%lld.\n" , j , tim , st[j] , flag[j]); // flag[j] = 1; if(tim <= st[j]+dd[j]) ///如果到达的时候捣蛋鬼正在健身 { tim += st[j]+dd[j]-tim; ///等待一段时间之后可以开始锻炼了 tim += (d[j]+x[j]); ///持续将锻炼和休息做完 st[j] = max(st[j]+dd[j]+xx[j] , tim-x[j]); ///捣蛋鬼开始的时间 要么是自己的休息昨晚 要么是等jony的运动做完 就是现在的总时间减去休息时间 } else ///如果到达的时候捣蛋鬼正在休息 { tim += (d[j]+x[j]); st[j] = max(st[j]+dd[j]+xx[j] , tim-x[j]); } } // printf("%lld......%lld......%lld......\n" , j , tim , st[j]); } tim -= x[10]; printf("%lld\n" , tim); } return 0; } /* 5 5 3 3 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 1 8 3 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 */