Passport
InterviewTime limit2sMemory limit256 MB
Given an arrival time and n windows with opening hours and service durations, find the earliest time the visitor finishes all windows in order, or report that it is impossible.
- Level
Medium4 of 10
- Topics
- Simulation, Implementation, Greedy, Math
- Solved
- No attempts yet
Problem
Many people know the situation: to obtain a document such as a passport, you must visit several places in a strictly defined order, and at each place you must do something: get a certificate, write an application, have a photocopy certified, and so on. For the convenience of citizens, such places were merged into single centers, where each place becomes a separate window. The problem remains, though: each window has its own working hours.
A person plans to arrive for their passport at hh hours mm minutes. They know they must visit exactly n windows in a defined order, and they know the working hours of each. When service at the last window finishes, they receive the passport. The person wants to know whether they will manage to get the passport before the end of the day, given that there are no other visitors in the center besides them.
For each window, its opening time and closing time are known. It is also known how many minutes it takes to serve one visitor at each window.
A visitor is considered to approach a window at the beginning of some minute. A window's opening time is the first minute during which it is already working, and its closing time is the first minute when it is no longer working. For example, if a window opens at 12:00 and closes at 20:00, and service takes 11 minutes, then if the person approaches the window between 12:00 and 19:49 inclusive, they are served immediately; if at 11:59 or earlier, service begins at 12:00; and if at 19:50 or later, they are no longer served.
The person moves between windows instantly. Thus, for example, if service at some window takes 10 minutes and the person approaches it at 12:45, then service at the next window can begin for them at 12:55 or later.
All windows open no earlier than 00:00 and close no later than 23:00. A window does not serve a visitor if less time remains until the end of the window's working hours than is required for service.
You must determine whether the person will manage to get the passport, and if so, the earliest moment at which they can leave the center with the passport.
Input
The first line contains the time at which the person plans to arrive at the center, in the format hh:mm. The second line contains a single integer n, the number of windows to visit (1 ≤ n ≤ 100).
The next n lines describe the working hours of all windows in the order in which they must be visited. The line describing window number i contains the opening time of that window in the format hh:mm, then a space, the closing time in the same format, then a space, and a single integer ti, the number of minutes service of a visitor at this window takes (1 ≤ ti ≤ 1440). The closing time of each window is strictly greater than its opening time.
Output
If the person will manage to get the passport, output Yes on the first line, and on the second line the time when they can leave the center, in the format hh:mm. Otherwise output No on the first line.