Every time, wherever I decide to take a look at CodeForces and to code & publish some of the easy problems, I am thinking that this is probably the last one, because it is too easy. However, I always find something interesting in those, thus they are worthy their time.

The problem of Borya is probably a standard queuing problem – the catch is to find a way to interpret it, if possible with only one loop, the one reading the values:

*It seems that Borya is seriously sick. He is going visit n doctors to find out the exact diagnosis. Each of the doctors needs the information about all previous visits, so Borya has to visit them in the prescribed order (i.e. Borya should first visit doctor 1, then doctor 2, then doctor 3and so on). Borya will get the information about his health from the last doctor.*

*Doctors have a strange working schedule. The doctor i goes to work on the s _{i}-th day and works every d_{i} day. So, he works on days s_{i}, s_{i} + d_{i}, s_{i} + 2d_{i}, ….*

*The doctor’s appointment takes quite a long time, so Borya can not see more than one doctor per day. What is the minimum time he needs to visit all doctors?*

*Input*

*First line contains an integer n — number of doctors (1 ≤ n ≤ 1000).*

*Next n lines contain two numbers s _{i} and d_{i} (1 ≤ s_{i}, d_{i} ≤ 1000).*

*Output*

*Output a single integer — the minimum day at which Borya can visit the last doctor.*

*I have managed to achieve something like this:*

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
class Startup { static void Main() { int n = int.Parse(Console.ReadLine()); int currentDay = 0; for (int i = 0; i < n; i++) { List<int> appointments = ReadLineAndParseToList(); int firstDay = appointments[0]; int betweenDays = appointments[1]; currentDay++; if (firstDay>=currentDay) { currentDay = firstDay; } else { int neededRepetitions = ((currentDay - firstDay) / betweenDays); int add = ((currentDay - firstDay) % betweenDays) > 0 ? 1 : 0; neededRepetitions += add; currentDay = firstDay + neededRepetitions * betweenDays; } } Console.WriteLine(currentDay); } |

The idea is that we cannot be earlier than the firstDay (the condition in the if). And if we cannot manage to get for the first day, then we should try to get the earliest next possible day. Thus, the currentDay = firstDay + neededRepetitions * betweenDays. And really, this piece of code has taken some good amount of my time today.

This is the “Cheat-Library” that I use for CodeForces – https://github.com/Vitosh/C-Sharp-Stuff/blob/master/CodeForces/Prepared.cs.