Economic Phone Calls
InterviewTime limit1sMemory limit128 MB
Given a chronologically ordered call log with some entries marked important, keep the fewest entries so all important ones stay and the listed year-recovery rule still gives each kept call its original year.
- Level
Medium6 of 10
- Topics
- Dynamic programming, Greedy, Sorting, Implementation
- Solved
- No attempts yet
Problem
An old phone you own has a built-in memory that logs every call you receive. For each call it stores the date (month and day) and the time (hour and minute) together with the caller's number. Because memory was expensive back then, only a limited number of calls can be stored.
The log is almost full, so you want to delete some entries. When choosing which entries to remove, two rules must hold:
- Some entries are important and must be kept.
- For every entry you keep, you must still be able to recover the year of the call, even though the phone does not store it. The recovery procedure is described below.
Determine the minimum number of entries that must be kept so that both rules are satisfied.
Recovering the year
Given a chronological list of call timestamps (each a month, day, hour, and minute), the year of every call is reconstructed as follows:
- The last call in the list took place in the current year.
- Take a call with timestamp and the call immediately before it with timestamp . If , both calls happened in the same year. If , the earlier call happened in the previous year.
- Apply rule 2 repeatedly, moving backwards through the list.
This procedure is not always correct in general, but you may assume it produces the true year for the given input. After deleting entries, applying the same procedure to the shortened log must yield the same year for every remaining call.
Input
The input contains several test cases. Each test case begins with a line holding the number of entries in the log, where . Each of the next lines contains one entry.
Every entry has the form mm:dd:HH:MM number ±: the month mm, day dd, hour HH, minute MM, the caller's number (1 to 16 digits), and finally a mark, + for a call you definitely want to keep or - for any other call. The entries are given exactly as stored by the phone, i.e. sorted by the time each call was received (the last entry is the most recent).
You may assume the recovery procedure above yields the correct year for every call.
The last test case is followed by a line containing a single 0, which is not processed.
Output
For each test case, print on its own line the minimum number of entries that must be kept so that both rules hold. In particular, applying the recovery procedure to the kept entries must assign every one of them the same year it had in the original log.