problems / 137

Waiting for a Higher Tick

medium monotonic-stack stack parsing hashing 1000 ms · 256 MB

Waiting for a Higher Tick

Alice runs the overnight review of a trading desk. The exchange feed for one day is a log of trades, one per line, many symbols interleaved, in the order the trades happened. For every trade she wants to know: after this trade, how long did it take until the same symbol traded at a strictly higher price?

Formally, let the trades be numbered 1..n in input order. For trade i, find the smallest j > i such that trade j has the same symbol as trade i and a strictly greater price. The answer for trade i is t_j − t_i, the difference of their timestamps in milliseconds, or -1 if no such j exists. Note that j is chosen by position in the log, and that two trades can share a timestamp, so an answer can be 0.

Input

The first line contains an integer n (1 ≤ n ≤ 2·10^5), the number of trades.

Each of the next n lines describes one trade and consists of three fields:

TIME SYMBOL PRICE

Fields are separated by one or more space characters. A line may also start and end with any number of spaces (including none). No other whitespace appears inside a line, and no line is longer than 100 characters.

  • TIME has the form HH:MM:SS.mmm: exactly two digits of hours 00..23, a colon, exactly two digits of minutes 00..59, a colon, exactly two digits of seconds 00..59, a dot, and exactly three digits of milliseconds 000..999. All times belong to the same day, and they are non-decreasing from line to line.
  • SYMBOL is 1 to 10 characters. Its first character is a letter (A..Z or a..z); the others are letters, digits 0..9, dots . or underscores _. Symbols are case-sensitive: abc and ABC are different symbols.
  • PRICE is a non-negative decimal number: an integer part of 1 to 20 digits, optionally followed by a dot and a fractional part of 1 to 4 digits. The integer part may have leading zeros and the fractional part may have trailing zeros, so 007.50, 7.5 and 7.5000 are the same price. The value of every price is less than 10^14.

Output

Print n lines. The i-th line must contain the answer for trade i: the number of milliseconds until the next strictly higher trade of the same symbol, or -1 if there is none.

Notes

In the first example, the ACME trades have prices 10, 9.5, 10, 10.25. The first one waits for the fourth one (09:30:02.000 − 09:30:00.000 = 2000 ms), because the third trade has an equal, not higher, price. ZETA trades only once and gets -1.

In the second example, prices are compared exactly: 99999999999999.0001 is lower than 99999999999999.0002, 0100 equals 100.0000 but is lower than 100.0001, and LO.W is a different symbol from lo.w.

Example 1

Input

5
09:30:00.000 ACME 10
09:30:00.500 ZETA 3.1
09:30:01.000 ACME 9.5
09:30:01.000 ACME 10.00
09:30:02.000 ACME 10.25

Output

2000
-1
0
1000
-1

Example 2

Input

7
  12:00:00.001   BIG  99999999999999.0002
12:00:00.002 BIG 99999999999999.0001
12:00:00.010 lo.w 0100
12:00:00.010 LO.W 5
12:00:00.500 lo.w 100.0000
12:00:01.010   BIG 99999999999999.0002
23:59:59.999 lo.w 100.0001

Output

-1
1008
43199989
-1
43199499
-1
-1

Hints · reveal one at a time if you're stuck

Hint 1

For each symbol separately, the question is "next strictly greater value later in the log", which is a classic stack pattern. Think about which earlier trades of a symbol can still be waiting for an answer when a new trade arrives.

Hint 2

Keep one stack per symbol and pop while the new price is strictly higher than the top's price. Parse prices exactly, because doubles cannot separate prices near 10^14 with four decimals, so scale each price to an integer number of ten-thousandths and pad the fraction. Parse times into milliseconds.

report an issue Prefer your own editor? sooks prep 137 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.