Stock Prices
Time limit1sMemory limit128 MB
Simulate an order book with buy and sell orders, matching bids and asks at the ask price, and report ask, bid, and last trade price after each order.
- Level
Medium5 of 10
- Topics
- Simulation, Implementation, Queue
- Solved
- No attempts yet
Problem
In this problem you simulate how a stock price is formed inside a simple order book. Three prices matter:
- The ask price is the lowest price at which someone is currently willing to sell one share.
- The bid price is the highest price at which someone is currently willing to buy one share.
- The stock price is the price at which the most recent deal was made.
Whenever the bid price is greater than or equal to the ask price, a deal is made: a buy order offering the bid price is matched with a sell order asking the ask price, and shares change hands at the ask price until either the buy order or the sell order (or both) is completely filled. This matching keeps repeating while the highest bid is at least the lowest ask. You are given a sequence of orders, each either a buy or a sell; after processing every order, report the current ask price, bid price, and stock price.
Input
The first line contains a positive integer, the number of test cases (at most ). Each test case is given as:
- One line with an integer (): the number of orders.
- lines, each of the form
buy x shares at yorsell x shares at y. Herebuymeans a buy order andsella sell order, () is the number of shares, and () is the desired price.
Output
For each test case output lines. The -th line contains three values a b s separated by single spaces: the current ask price, bid price, and stock price after the -th order has been processed and every possible deal has been made. If a price is not defined, print - in its place. Every test case starts with all three prices undefined.