码迷,mamicode.com
首页 > 其他好文 > 详细

codechef The Morning Commute 题解

时间:2014-05-07 15:37:11      阅读:406      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   class   code   tar   

The Morning Commute

The Chef commutes to work every day using the city‘s underground metro. The schedule for the trains has recently been changed and he wants to know how long it will take to travel from the station nearest to his house and the station nearest to his restaurant.

The Chef doesn‘t want to change the route he took before, so he simply has to find out how long it will take to reach his restaurant along his usual route. This route is given by a sequence of stations s0, s1, ..., sn where s0 is the station where the Chef enters the metro and sn is the station where the Chef exits the metro.

Trains are scheduled to run between every two consecutive stations si-1 and si. Such a schedule is specified by three integers xi, li, and fi. This means that the first train on this line starts operating at time xi. The time it takes this train to travel from si-1 and si is exactly li units. Finally, a train departs from station si-1 every fiminutes following the previous train. That is, a train departs at time xi, xi+fi, xi+2fi, and so on.

The Chef is very experienced at navigating the metro so the time it takes him to transfer between trains at a given station is essentially zero. Thus, if the Chef arrives at a station, say si, the moment that the train from sito si+1 is scheduled to depart, he skillfully hops on this next train. However, if the Chef arrives when no train to si+1 is scheduled to depart, he must wait until the scheduled departure time.

Help the Chef figure out how long it will take him to travel from station s0 to station sn. You may assume that the Chef is already at station s0 at time 0.

Input

The first line consists of a single integer denoting the number of test cases (at most 50). Each test case begins with a line containing a single integer n between 1 and 1000 indicating the number of lines the Chef must traverse (so there are n+1 stations s0, s1, ..., sn). The next n lines describe the train schedules between stations, one per line. The i‘th such line gives the values xi, li, and fi for the train that travels between stations si-1 and si.

The xi values will be between 0 and 1000 and the li and fi values will be between 1 and 1000.

Output

For each test case you are to output a single integer denoting the minimum time t for which the Chef can reach station sn using the given route. Remember, the Chef starts at s0 at time 0.

Example

Input:
3
2
0 4 7
0 6 5
2
0 1 2
6 2 10
2
1 2 3
0 2 3


Output:
11
8
5

这样考理解题意的题目,算法很简单,下次还是少做这样的题目了。 只不过考我们能不能看懂题目罢了。题目简单,却也十分耗时,不值得啊。

算法就是数学计算,还是加减计算呢。

#pragma once
#include <stdio.h>

class TheMorningCommute
{
	struct Schedule
	{
		int start, travel, depart;
	};
public:
	TheMorningCommute()
	{
		int T, N;
		scanf("%d", &T);
		while (T--)
		{
			scanf("%d", &N);
			Schedule sche;
			int travT = 0;
			for (int i = 0; i < N; i++)
			{
				scanf("%d %d %d", &sche.start, 
					&sche.travel, &sche.depart);
				if (travT < sche.start) travT = sche.start;
				else
				{
					int t = (travT - sche.start) / sche.depart;
					if ((travT - sche.start)%sche.depart) t++;
					travT = sche.start + t * sche.depart;
				}
				travT += sche.travel;
			}
			printf("%d\n", travT);
		}
	}
};

int theMorningCommute()
{
	TheMorningCommute();
	return 0;
}



codechef The Morning Commute 题解,布布扣,bubuko.com

codechef The Morning Commute 题解

标签:des   style   blog   class   code   tar   

原文地址:http://blog.csdn.net/kenden23/article/details/25203743

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!